-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore jsonPath and fix it (#12325)
This restore the Json traversal library. A bug was introduce in the Json path library, the PR fix it. In a json schema we can define an enum without specifying a "type" attribute. It wasn't handle in the previous implemantation. We now return a right type in the getType method and process it the same way than the an integer/boolean/string type.
- Loading branch information
1 parent
bb2da42
commit e8813ee
Showing
43 changed files
with
1,232 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
airbyte-commons/src/test/java/io/airbyte/commons/json/JsonSchemasTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (c) 2021 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.commons.json; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import io.airbyte.commons.resources.MoreResources; | ||
import java.io.IOException; | ||
import java.util.function.BiConsumer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mockito; | ||
|
||
class JsonSchemasTest { | ||
|
||
@Test | ||
void testMutateTypeToArrayStandard() { | ||
final JsonNode expectedWithoutType = Jsons.deserialize("{\"test\":\"abc\"}"); | ||
final JsonNode actualWithoutType = Jsons.clone(expectedWithoutType); | ||
JsonSchemas.mutateTypeToArrayStandard(expectedWithoutType); | ||
assertEquals(expectedWithoutType, actualWithoutType); | ||
|
||
final JsonNode expectedWithArrayType = Jsons.deserialize("{\"test\":\"abc\", \"type\":[\"object\"]}"); | ||
final JsonNode actualWithArrayType = Jsons.clone(expectedWithArrayType); | ||
JsonSchemas.mutateTypeToArrayStandard(actualWithArrayType); | ||
assertEquals(expectedWithoutType, actualWithoutType); | ||
|
||
final JsonNode expectedWithoutArrayType = Jsons.deserialize("{\"test\":\"abc\", \"type\":[\"object\"]}"); | ||
final JsonNode actualWithStringType = Jsons.deserialize("{\"test\":\"abc\", \"type\":\"object\"}"); | ||
JsonSchemas.mutateTypeToArrayStandard(actualWithStringType); | ||
assertEquals(expectedWithoutArrayType, actualWithStringType); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
void testTraverse() throws IOException { | ||
final JsonNode jsonWithAllTypes = Jsons.deserialize(MoreResources.readResource("json_schemas/json_with_all_types.json")); | ||
final BiConsumer<JsonNode, String> mock = mock(BiConsumer.class); | ||
|
||
JsonSchemas.traverseJsonSchema(jsonWithAllTypes, mock); | ||
final InOrder inOrder = Mockito.inOrder(mock); | ||
inOrder.verify(mock).accept(jsonWithAllTypes, JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("name"), "$.name"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("name").get("properties").get("first"), "$.name.first"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("name").get("properties").get("last"), "$.name.last"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("company"), "$.company"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("pets"), "$.pets"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("pets").get("items"), "$.pets[*]"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("pets").get("items").get("properties").get("type"), "$.pets[*].type"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("pets").get("items").get("properties").get("number"), "$.pets[*].number"); | ||
inOrder.verifyNoMoreInteractions(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@ValueSource(strings = { | ||
"anyOf", | ||
"oneOf", | ||
"allOf" | ||
}) | ||
@ParameterizedTest | ||
void testTraverseComposite(final String compositeKeyword) throws IOException { | ||
final String jsonSchemaString = MoreResources.readResource("json_schemas/composite_json_schema.json") | ||
.replaceAll("<composite-placeholder>", compositeKeyword); | ||
final JsonNode jsonWithAllTypes = Jsons.deserialize(jsonSchemaString); | ||
final BiConsumer<JsonNode, String> mock = mock(BiConsumer.class); | ||
|
||
JsonSchemas.traverseJsonSchema(jsonWithAllTypes, mock); | ||
|
||
final InOrder inOrder = Mockito.inOrder(mock); | ||
inOrder.verify(mock).accept(jsonWithAllTypes, JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(0), JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(1), JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(1).get("properties").get("prop1"), "$.prop1"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(2), JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(2).get("items"), "$[*]"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(3).get(compositeKeyword).get(0), JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(3).get(compositeKeyword).get(1), JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(3).get(compositeKeyword).get(1).get("items"), "$[*]"); | ||
inOrder.verifyNoMoreInteractions(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
void testTraverseMultiType() throws IOException { | ||
final JsonNode jsonWithAllTypes = Jsons.deserialize(MoreResources.readResource("json_schemas/json_with_array_type_fields.json")); | ||
final BiConsumer<JsonNode, String> mock = mock(BiConsumer.class); | ||
|
||
JsonSchemas.traverseJsonSchema(jsonWithAllTypes, mock); | ||
final InOrder inOrder = Mockito.inOrder(mock); | ||
inOrder.verify(mock).accept(jsonWithAllTypes, JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("properties").get("company"), "$.company"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("items"), "$[*]"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("items").get("properties").get("user"), "$[*].user"); | ||
inOrder.verifyNoMoreInteractions(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Test | ||
void testTraverseMultiTypeComposite() throws IOException { | ||
final String compositeKeyword = "anyOf"; | ||
final JsonNode jsonWithAllTypes = Jsons.deserialize(MoreResources.readResource("json_schemas/json_with_array_type_fields_with_composites.json")); | ||
final BiConsumer<JsonNode, String> mock = mock(BiConsumer.class); | ||
|
||
JsonSchemas.traverseJsonSchema(jsonWithAllTypes, mock); | ||
|
||
final InOrder inOrder = Mockito.inOrder(mock); | ||
inOrder.verify(mock).accept(jsonWithAllTypes, JsonPaths.empty()); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(0).get("properties").get("company"), "$.company"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get(compositeKeyword).get(1).get("properties").get("organization"), "$.organization"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("items"), "$[*]"); | ||
inOrder.verify(mock).accept(jsonWithAllTypes.get("items").get("properties").get("user"), "$[*].user"); | ||
inOrder.verifyNoMoreInteractions(); | ||
} | ||
|
||
} |
Oops, something went wrong.