Skip to content

Commit

Permalink
If schema items is an array of schemas, it will be parsed as a list o…
Browse files Browse the repository at this point in the history
…f Schemas.

Updated tests.
  • Loading branch information
jaydeepk committed Oct 12, 2023
1 parent fa0386c commit d0030a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SchemaItemsDeserializer extends JsonDeserializer<Object> {

Expand All @@ -20,11 +23,21 @@ public Object deserialize(JsonParser jsonParser, DeserializationContext deserial
if (nodeType == JsonNodeType.OBJECT) {
return readAsSchema(node, objectCodec);
}
// TODO: Implement handling of scenario when items is an array of schemas. Maybe return a List<Schema>?
if (nodeType == JsonNodeType.ARRAY) {
return readAsListOfSchemas((ArrayNode) node, objectCodec);
}
return readAsObject(node, objectCodec);
}

private Object readAsSchema(JsonNode jsonNode, ObjectCodec objectCodec) throws IOException {
private List<Schema> readAsListOfSchemas(ArrayNode arrayNode, ObjectCodec objectCodec) throws IOException {
List<Schema> schemaList = new ArrayList<>();
for (JsonNode childNode : arrayNode) {
schemaList.add(readAsSchema(childNode, objectCodec));
}
return schemaList;
}

private Schema readAsSchema(JsonNode jsonNode, ObjectCodec objectCodec) throws IOException {
try (JsonParser parser = jsonNode.traverse(objectCodec)) {
return parser.readValueAs(Schema.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ class MessageWithArrayPayloadTest {
}

@Test
@DisplayName("Test array items property is parsed as an array list")
fun testArrayItemsPropertyIsParsedAsArrayListWhenItIsAnArrayOfSchemas() {
@DisplayName("Test array items property is parsed as list of schemas")
fun testArrayItemsPropertyIsParsedAsArrayListOfSchemasWhenItIsAnArrayOfSchemas() {
val model = ClasspathUtils.readAsString("/json/2.6.0/model/channel/message/messageWithArrayPayloadArrayOfSchemas.json")
val schema = objectMapper.readValue(model, Message::class.java).payload as Schema
assertTrue(
schema.items is ArrayList<*>
)
assertTrue(schema.items is ArrayList<*> && (schema.items as ArrayList<*>).all { it is Schema })
}
}

0 comments on commit d0030a1

Please sign in to comment.