Skip to content

Commit

Permalink
refactor(avro schema): SchemaFormat can hold Avro, AvroUnion, Reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed Apr 20, 2024
1 parent 474edd0 commit f7cd205
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.asyncapi.v3.schema.avro.v1._9_0.jackson;

import com.asyncapi.v3.Reference;
import com.asyncapi.v3.schema.avro.v1._9_0.AvroSchema;
import com.asyncapi.v3.schema.avro.v1._9_0.AvroSchemaUnion;
import com.fasterxml.jackson.core.JsonParser;
Expand All @@ -10,6 +11,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;

Expand All @@ -31,7 +33,7 @@ private Object chooseKnownPojo(JsonNode jsonNode, final ObjectCodec objectCodec)
case ARRAY:
return readAsUnion((ArrayNode) jsonNode, objectCodec);
case OBJECT:
return jsonParser.readValueAs(AvroSchema.class);
return readAvroSchema((ObjectNode) jsonNode, objectCodec);
case STRING:
return jsonParser.readValueAs(String.class);
case BOOLEAN:
Expand All @@ -56,4 +58,14 @@ private AvroSchemaUnion readAsUnion(ArrayNode arrayNode, ObjectCodec objectCodec
return avroSchemaUnion;
}

private Object readAvroSchema(ObjectNode objectNode, ObjectCodec objectCodec) throws IOException {
try (JsonParser jsonParser = objectNode.traverse(objectCodec)) {
if (objectNode.size() == 1 && objectNode.has("$ref")) {
return jsonParser.readValueAs(Reference.class);
}

return jsonParser.readValueAs(AvroSchema.class);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,29 @@ public AvroFormatSchema(
super(schemaFormat(schemaFormat), schema);
}

/**
* Schema <b>MUST</b> be one of:
* <ul>
* <li>{@link com.asyncapi.v3.schema.avro.v1._9_0.AvroSchema}</li>
* <li>{@link com.asyncapi.v3.schema.avro.v1._9_0.AvroSchemaUnion}</li>
* <li>{@link com.asyncapi.v3.Reference}</li>
* </ul>
*
* @param schema Avro Schema or Reference
*/
@Override
public void setSchema(@NotNull Object schema) {
super.setSchema(schema);
}

/**
* Schema:
* <ul>
* <li>{@link com.asyncapi.v3.schema.avro.v1._9_0.AvroSchema}</li>
* <li>{@link com.asyncapi.v3.schema.avro.v1._9_0.AvroSchemaUnion}</li>
* <li>{@link com.asyncapi.v3.Reference}</li>
* </ul>
*/
@NotNull
public Object getSchema() {
return super.getSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import com.asyncapi.v3.binding.channel.kafka.KafkaChannelTopicCleanupPolicy
import com.asyncapi.v3.binding.channel.kafka.KafkaChannelTopicConfiguration
import com.asyncapi.v3.binding.operation.kafka.KafkaOperationBinding
import com.asyncapi.v3.binding.server.kafka.KafkaServerBinding
import com.asyncapi.v3.schema.multiformat.MultiFormatSchema
import com.asyncapi.v3.schema.AsyncAPISchema
import com.asyncapi.v3.schema.multiformat.AvroFormatSchema
import com.asyncapi.v3.security_scheme.SecurityScheme
import org.junit.jupiter.api.Disabled

@Disabled("Implement Avro Schema")
class AdeoKafkaRequestReplyAsyncAPI: AbstractExampleValidationTest() {

override fun specificationLocation(): String = "/examples/v3.0.0/adeo-kafka-request-reply-asyncapi.yml"
Expand Down Expand Up @@ -253,9 +251,9 @@ class AdeoKafkaRequestReplyAsyncAPI: AbstractExampleValidationTest() {
))
.build()
)
.payload(MultiFormatSchema(
.payload(AvroFormatSchema(
"application/vnd.apache.avro;version=1.9.0",
mapOf(Pair("\$ref", "https://www.asyncapi.com/resources/casestudies/adeo/CostingRequestPayload.avsc"))
Reference("https://www.asyncapi.com/resources/casestudies/adeo/CostingRequestPayload.avsc")
))
.build()
),
Expand Down Expand Up @@ -289,9 +287,9 @@ class AdeoKafkaRequestReplyAsyncAPI: AbstractExampleValidationTest() {
))
.build()
)
.payload(MultiFormatSchema(
.payload(AvroFormatSchema(
"application/vnd.apache.avro;version=1.9.0",
mapOf(Pair("\$ref", "https://www.asyncapi.com/resources/casestudies/adeo/CostingResponsePayload.avsc"))
Reference("https://www.asyncapi.com/resources/casestudies/adeo/CostingResponsePayload.avsc")
))
.build()
)
Expand Down

0 comments on commit f7cd205

Please sign in to comment.