Skip to content
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

[#14909] Fix for duplicate key isue when diffing catalogs with arrays #14922

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.airbyte.commons.json.JsonSchemas;
import io.airbyte.commons.json.JsonSchemas.FieldNameOrList;
import io.airbyte.commons.json.Jsons;
import io.airbyte.commons.util.MoreIterators;
import io.airbyte.commons.util.MoreLists;
Expand All @@ -33,6 +32,8 @@
*/
public class CatalogHelpers {

private static final String ITEMS_KEY = "items";

public static AirbyteCatalog createAirbyteCatalog(final String streamName, final Field... fields) {
return new AirbyteCatalog().withStreams(Lists.newArrayList(createAirbyteStream(streamName, fields)));
}
Expand Down Expand Up @@ -222,7 +223,7 @@ protected static List<Pair<List<String>, JsonNode>> getFullyQualifiedFieldNamesW
final Set<List<String>> fieldNamesThatAreOneOfs = new HashSet<>();

return JsonSchemas.traverseJsonSchemaWithCollector(jsonSchema, (node, basicPath) -> {
final List<String> fieldName = basicPath.stream().filter(fieldOrList -> !fieldOrList.isList()).map(FieldNameOrList::getFieldName).toList();
final List<String> fieldName = basicPath.stream().map(fieldOrList -> fieldOrList.isList() ? ITEMS_KEY : fieldOrList.getFieldName()).toList();
return Pair.of(fieldName, node);
})
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void testGetFieldNames() throws IOException {
final JsonNode node = Jsons.deserialize(MoreResources.readResource("valid_schema.json"));
final Set<String> actualFieldNames = CatalogHelpers.getAllFieldNames(node);
final List<String> expectedFieldNames =
List.of("CAD", "DKK", "HKD", "HUF", "ISK", "PHP", "date", "nestedkey", "somekey", "something", "something2", "文");
List.of("CAD", "DKK", "HKD", "HUF", "ISK", "PHP", "date", "nestedkey", "somekey", "something", "something2", "文", "someArray", "items", "oldName");

// sort so that the diff is easier to read.
assertEquals(expectedFieldNames.stream().sorted().toList(), actualFieldNames.stream().sorted().toList());
Expand All @@ -109,7 +109,17 @@ void testGetCatalogDiff() throws IOException {
FieldTransform.createRemoveFieldTransform(List.of("HKD"), schema1.get("properties").get("HKD")),
FieldTransform.createUpdateFieldTransform(List.of("CAD"), new UpdateFieldSchemaTransform(
schema1.get("properties").get("CAD"),
schema2.get("properties").get("CAD")))))))
schema2.get("properties").get("CAD"))),
FieldTransform.createUpdateFieldTransform(List.of("someArray"), new UpdateFieldSchemaTransform(
schema1.get("properties").get("someArray"),
schema2.get("properties").get("someArray"))),
FieldTransform.createUpdateFieldTransform(List.of("someArray", "items"), new UpdateFieldSchemaTransform(
schema1.get("properties").get("someArray").get("items"),
schema2.get("properties").get("someArray").get("items"))),
FieldTransform.createRemoveFieldTransform(List.of("someArray", "items", "oldName"),
schema1.get("properties").get("someArray").get("items").get("properties").get("oldName")),
FieldTransform.createAddFieldTransform(List.of("someArray", "items", "newName"),
schema2.get("properties").get("someArray").get("items").get("properties").get("newName"))))))
.sorted(STREAM_TRANSFORM_COMPARATOR)
.toList();
assertEquals(expectedDiff, actualDiff.stream().sorted(STREAM_TRANSFORM_COMPARATOR).toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@
}
}
]
},
"someArray": {
"type": [
"array",
"null"
],
"items": {
"type": [
"object",
"null"
],
"properties": {
"oldName": {
"type": [
"string",
"null"
],
"maxLength": 100
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@
"patternProperties": {
".+": {}
}
},
"someArray": {
"type": [
"array",
"null"
],
"items": {
"type": [
"object",
"null"
],
"properties": {
"newName": {
"type": [
"string",
"null"
],
"maxLength": 100
}
}
}
}
}
}