-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: Add explicit JSON parser for ConfigResponse #9952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.iceberg.rest.responses; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.io.IOException; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.util.JsonUtil; | ||
|
|
||
| public class ConfigResponseParser { | ||
|
|
||
| private static final String DEFAULTS = "defaults"; | ||
| private static final String OVERRIDES = "overrides"; | ||
|
|
||
| private ConfigResponseParser() {} | ||
|
|
||
| public static String toJson(ConfigResponse response) { | ||
| return toJson(response, false); | ||
| } | ||
|
|
||
| public static String toJson(ConfigResponse response, boolean pretty) { | ||
| return JsonUtil.generate(gen -> toJson(response, gen), pretty); | ||
| } | ||
|
|
||
| public static void toJson(ConfigResponse response, JsonGenerator gen) throws IOException { | ||
| Preconditions.checkArgument(null != response, "Invalid config response: null"); | ||
|
|
||
| gen.writeStartObject(); | ||
|
|
||
| JsonUtil.writeStringMap(DEFAULTS, response.defaults(), gen); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. existing tests in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just change this to not send back the key in the response in the value is null (makes for a cleaner response? I don't really think that's a behavior change since in the end a client needs to check that the value is not null.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NVM these two fields are spec'd out so it seems like servers should always send it even though it may be null |
||
| JsonUtil.writeStringMap(OVERRIDES, response.overrides(), gen); | ||
|
|
||
| gen.writeEndObject(); | ||
| } | ||
|
|
||
| public static ConfigResponse fromJson(String json) { | ||
| return JsonUtil.parse(json, ConfigResponseParser::fromJson); | ||
| } | ||
|
|
||
| public static ConfigResponse fromJson(JsonNode json) { | ||
| Preconditions.checkArgument(null != json, "Cannot parse config response from null object"); | ||
|
|
||
| ConfigResponse.Builder builder = ConfigResponse.builder(); | ||
|
|
||
| if (json.hasNonNull(DEFAULTS)) { | ||
| builder.withDefaults(JsonUtil.getStringMapNullableValues(DEFAULTS, json)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems to specifically require a Map where the value can be null (see also #4184 (comment)) and there are also tests in |
||
| } | ||
|
|
||
| if (json.hasNonNull(OVERRIDES)) { | ||
| builder.withOverrides(JsonUtil.getStringMapNullableValues(OVERRIDES, json)); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.relocated.com.google.common.io.BaseEncoding; | ||
|
|
||
| public class JsonUtil { | ||
|
|
@@ -206,6 +207,25 @@ public static Map<String, String> getStringMap(String property, JsonNode node) { | |
| return builder.build(); | ||
| } | ||
|
|
||
| public static Map<String, String> getStringMapNullableValues(String property, JsonNode node) { | ||
| Preconditions.checkArgument(node.has(property), "Cannot parse missing map: %s", property); | ||
| JsonNode pNode = node.get(property); | ||
| Preconditions.checkArgument( | ||
| pNode != null && !pNode.isNull() && pNode.isObject(), | ||
| "Cannot parse string map from non-object value: %s: %s", | ||
| property, | ||
| pNode); | ||
|
|
||
| Map<String, String> map = Maps.newHashMap(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't use ImmutableMap here as it doesn't support null values |
||
| Iterator<String> fields = pNode.fieldNames(); | ||
| while (fields.hasNext()) { | ||
| String field = fields.next(); | ||
| map.put(field, getStringOrNull(field, pNode)); | ||
| } | ||
|
|
||
| return map; | ||
| } | ||
|
|
||
| public static String[] getStringArray(JsonNode node) { | ||
| Preconditions.checkArgument( | ||
| node != null && !node.isNull() && node.isArray(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.iceberg.rest.responses; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.util.Map; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TestConfigResponseParser { | ||
|
|
||
| @Test | ||
| public void nullAndEmptyCheck() { | ||
| assertThatThrownBy(() -> ConfigResponseParser.toJson(null)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("Invalid config response: null"); | ||
|
|
||
| assertThatThrownBy(() -> ConfigResponseParser.fromJson((JsonNode) null)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("Cannot parse config response from null object"); | ||
|
|
||
| ConfigResponse actual = ConfigResponseParser.fromJson("{}"); | ||
| ConfigResponse expected = ConfigResponse.builder().build(); | ||
| // ConfigResponse doesn't implement hashCode/equals | ||
| assertThat(actual.defaults()).isEqualTo(expected.defaults()).isEmpty(); | ||
| assertThat(actual.overrides()).isEqualTo(expected.overrides()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void unknownFields() { | ||
| ConfigResponse actual = ConfigResponseParser.fromJson("{\"x\": \"val\", \"y\": \"val2\"}"); | ||
| ConfigResponse expected = ConfigResponse.builder().build(); | ||
| // ConfigResponse doesn't implement hashCode/equals | ||
| assertThat(actual.defaults()).isEqualTo(expected.defaults()).isEmpty(); | ||
| assertThat(actual.overrides()).isEqualTo(expected.overrides()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void defaultsOnly() { | ||
| Map<String, String> defaults = Maps.newHashMap(); | ||
| defaults.put("a", "1"); | ||
| defaults.put("b", null); | ||
| defaults.put("c", "2"); | ||
| defaults.put("d", null); | ||
|
|
||
| ConfigResponse response = ConfigResponse.builder().withDefaults(defaults).build(); | ||
| String expectedJson = | ||
| "{\n" | ||
| + " \"defaults\" : {\n" | ||
| + " \"a\" : \"1\",\n" | ||
| + " \"b\" : null,\n" | ||
| + " \"c\" : \"2\",\n" | ||
| + " \"d\" : null\n" | ||
| + " },\n" | ||
| + " \"overrides\" : { }\n" | ||
| + "}"; | ||
|
|
||
| String json = ConfigResponseParser.toJson(response, true); | ||
| assertThat(json).isEqualTo(expectedJson); | ||
| assertThat(ConfigResponseParser.toJson(ConfigResponseParser.fromJson(json), true)) | ||
| .isEqualTo(expectedJson); | ||
| } | ||
|
|
||
| @Test | ||
| public void overridesOnly() { | ||
| Map<String, String> overrides = Maps.newHashMap(); | ||
| overrides.put("a", "1"); | ||
| overrides.put("b", null); | ||
| overrides.put("c", "2"); | ||
| overrides.put("d", null); | ||
|
|
||
| ConfigResponse response = ConfigResponse.builder().withOverrides(overrides).build(); | ||
| String expectedJson = | ||
| "{\n" | ||
| + " \"defaults\" : { },\n" | ||
| + " \"overrides\" : {\n" | ||
| + " \"a\" : \"1\",\n" | ||
| + " \"b\" : null,\n" | ||
| + " \"c\" : \"2\",\n" | ||
| + " \"d\" : null\n" | ||
| + " }\n" | ||
| + "}"; | ||
|
|
||
| String json = ConfigResponseParser.toJson(response, true); | ||
| assertThat(json).isEqualTo(expectedJson); | ||
| assertThat(ConfigResponseParser.toJson(ConfigResponseParser.fromJson(json), true)) | ||
| .isEqualTo(expectedJson); | ||
| } | ||
|
|
||
| @Test | ||
| public void roundTripSerde() { | ||
| Map<String, String> defaults = Maps.newHashMap(); | ||
| defaults.put("key1", "1"); | ||
| defaults.put("key2", null); | ||
|
|
||
| Map<String, String> overrides = Maps.newHashMap(); | ||
| overrides.put("key3", "23"); | ||
| overrides.put("key4", null); | ||
|
|
||
| ConfigResponse response = | ||
| ConfigResponse.builder().withDefaults(defaults).withOverrides(overrides).build(); | ||
| String expectedJson = | ||
| "{\n" | ||
| + " \"defaults\" : {\n" | ||
| + " \"key1\" : \"1\",\n" | ||
| + " \"key2\" : null\n" | ||
| + " },\n" | ||
| + " \"overrides\" : {\n" | ||
| + " \"key3\" : \"23\",\n" | ||
| + " \"key4\" : null\n" | ||
| + " }\n" | ||
| + "}"; | ||
|
|
||
| String json = ConfigResponseParser.toJson(response, true); | ||
| assertThat(json).isEqualTo(expectedJson); | ||
| assertThat(ConfigResponseParser.toJson(ConfigResponseParser.fromJson(json), true)) | ||
| .isEqualTo(expectedJson); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.