From d0030a17811a70f9497d96ab8184eefa2ae403b4 Mon Sep 17 00:00:00 2001 From: jaydeep Date: Thu, 12 Oct 2023 18:33:45 +0530 Subject: [PATCH] If schema items is an array of schemas, it will be parsed as a list of Schemas. Updated tests. --- .../v2/jackson/SchemaItemsDeserializer.java | 17 +++++++++++++++-- .../message/MessageWithArrayPayloadTest.kt | 8 +++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/SchemaItemsDeserializer.java b/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/SchemaItemsDeserializer.java index 8cdcc77b..4b90358f 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/SchemaItemsDeserializer.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v2/jackson/SchemaItemsDeserializer.java @@ -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 { @@ -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? + if (nodeType == JsonNodeType.ARRAY) { + return readAsListOfSchemas((ArrayNode) node, objectCodec); + } return readAsObject(node, objectCodec); } - private Object readAsSchema(JsonNode jsonNode, ObjectCodec objectCodec) throws IOException { + private List readAsListOfSchemas(ArrayNode arrayNode, ObjectCodec objectCodec) throws IOException { + List 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); } diff --git a/asyncapi-core/src/test/kotlin/com/asyncapi/v2/_6_0/model/channel/message/MessageWithArrayPayloadTest.kt b/asyncapi-core/src/test/kotlin/com/asyncapi/v2/_6_0/model/channel/message/MessageWithArrayPayloadTest.kt index 373b0e06..ea7f4820 100644 --- a/asyncapi-core/src/test/kotlin/com/asyncapi/v2/_6_0/model/channel/message/MessageWithArrayPayloadTest.kt +++ b/asyncapi-core/src/test/kotlin/com/asyncapi/v2/_6_0/model/channel/message/MessageWithArrayPayloadTest.kt @@ -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 }) } } \ No newline at end of file