ISSUE-876 Fixed array types to oneof for js support (#880)
This commit is contained in:
parent
3d537f2bf1
commit
3c05b60313
3 changed files with 29 additions and 31 deletions
|
@ -67,6 +67,10 @@ public class AvroJsonSchemaConverter implements JsonSchemaConverter<Schema> {
|
|||
}
|
||||
|
||||
private FieldSchema createUnionSchema(Schema schema, Map<String, FieldSchema> definitions) {
|
||||
|
||||
final boolean nullable = schema.getTypes().stream()
|
||||
.anyMatch(t -> t.getType().equals(Schema.Type.NULL));
|
||||
|
||||
final Map<String, FieldSchema> fields = schema.getTypes().stream()
|
||||
.filter(t -> !t.getType().equals(Schema.Type.NULL))
|
||||
.map(f -> Tuples.of(
|
||||
|
@ -80,7 +84,16 @@ public class AvroJsonSchemaConverter implements JsonSchemaConverter<Schema> {
|
|||
Tuple2::getT2
|
||||
));
|
||||
|
||||
return new ObjectFieldSchema(fields, Collections.emptyList(), true);
|
||||
if (nullable) {
|
||||
return new OneOfFieldSchema(
|
||||
List.of(
|
||||
new SimpleFieldSchema(new SimpleJsonType(JsonType.Type.NULL)),
|
||||
new ObjectFieldSchema(fields, Collections.emptyList())
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return new ObjectFieldSchema(fields, Collections.emptyList());
|
||||
}
|
||||
}
|
||||
|
||||
private FieldSchema createObjectSchema(String name, Schema schema,
|
||||
|
|
|
@ -15,18 +15,11 @@ import reactor.util.function.Tuples;
|
|||
public class ObjectFieldSchema implements FieldSchema {
|
||||
private final Map<String, FieldSchema> properties;
|
||||
private final List<String> required;
|
||||
private final boolean nullable;
|
||||
|
||||
public ObjectFieldSchema(Map<String, FieldSchema> properties,
|
||||
List<String> required) {
|
||||
this(properties, required, false);
|
||||
}
|
||||
|
||||
public ObjectFieldSchema(Map<String, FieldSchema> properties,
|
||||
List<String> required, boolean nullable) {
|
||||
this.properties = properties;
|
||||
this.required = required;
|
||||
this.nullable = nullable;
|
||||
}
|
||||
|
||||
public Map<String, FieldSchema> getProperties() {
|
||||
|
@ -46,18 +39,9 @@ public class ObjectFieldSchema implements FieldSchema {
|
|||
Tuple2::getT2
|
||||
));
|
||||
final ObjectNode objectNode = mapper.createObjectNode();
|
||||
if (this.nullable) {
|
||||
objectNode.set(
|
||||
"type",
|
||||
mapper.createArrayNode()
|
||||
.add(JsonType.Type.OBJECT.getName())
|
||||
.add(JsonType.Type.NULL.getName())
|
||||
);
|
||||
} else {
|
||||
objectNode.setAll(
|
||||
new SimpleJsonType(JsonType.Type.OBJECT).toJsonNode(mapper)
|
||||
);
|
||||
}
|
||||
objectNode.setAll(
|
||||
new SimpleJsonType(JsonType.Type.OBJECT).toJsonNode(mapper)
|
||||
);
|
||||
objectNode.set("properties", mapper.valueToTree(nodes));
|
||||
if (!required.isEmpty()) {
|
||||
objectNode.set("required", mapper.valueToTree(required));
|
||||
|
|
|
@ -87,13 +87,14 @@ public class AvroJsonSchemaConverterTest {
|
|||
+ "{\"$ref\":\"#/definitions/RecordInnerMessage\"}},"
|
||||
+ "\"required\":[\"record\"],\"definitions\":"
|
||||
+ "{\"RecordInnerMessage\":{\"type\":\"object\",\""
|
||||
+ "properties\":{\"long_text\":{\"type\":[\"object\", \"null\"],"
|
||||
+ "\"properties\":{\"string\":{\"type\":\"string\"}}},"
|
||||
+ "\"array\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},"
|
||||
+ "\"id\":{\"type\":\"integer\"},\"text\":{\"type\":\"string\"},"
|
||||
+ "\"map\":{\"type\":\"object\",\"additionalProperties\":"
|
||||
+ "{\"type\":\"integer\"}},\"order\":{\"type\":\"string\","
|
||||
+ "\"enum\":[\"SPADES\",\"HEARTS\",\"DIAMONDS\",\"CLUBS\"]}},"
|
||||
+ "properties\":{\"long_text\":{\"oneOf\":[{\"type\":\"null\"},"
|
||||
+ "{\"type\":\"object\",\"properties\":{\"string\":"
|
||||
+ "{\"type\":\"string\"}}}]},\"array\":{\"type\":\"array\",\"items\":"
|
||||
+ "{\"type\":\"string\"}},\"id\":{\"type\":\"integer\"},\"text\":"
|
||||
+ "{\"type\":\"string\"},\"map\":{\"type\":\"object\","
|
||||
+ "\"additionalProperties\":{\"type\":\"integer\"}},"
|
||||
+ "\"order\":{\"enum\":[\"SPADES\",\"HEARTS\",\"DIAMONDS\",\"CLUBS\"],"
|
||||
+ "\"type\":\"string\"}},"
|
||||
+ "\"required\":[\"id\",\"text\",\"order\",\"array\",\"map\"]}}}";
|
||||
|
||||
final JsonSchema convertRecord = converter.convert(basePath, recordSchema);
|
||||
|
@ -151,10 +152,10 @@ public class AvroJsonSchemaConverterTest {
|
|||
"{\"$id\":\"http://example.com/Message\","
|
||||
+ "\"$schema\":\"https://json-schema.org/draft/2020-12/schema\","
|
||||
+ "\"type\":\"object\",\"properties\":{\"text\":"
|
||||
+ "{\"type\":[\"object\", \"null\"],\"properties\":{\"string\":"
|
||||
+ "{\"type\":\"string\"}}},\"value\":{\"type\":[\"object\", \"null\"],"
|
||||
+ "\"properties\":{\"string\":{\"type\":\"string\"},"
|
||||
+ "\"long\":{\"type\":\"integer\"}}}}}";
|
||||
+ "{\"oneOf\":[{\"type\":\"null\"},{\"type\":\"object\","
|
||||
+ "\"properties\":{\"string\":{\"type\":\"string\"}}}]},\"value\":"
|
||||
+ "{\"oneOf\":[{\"type\":\"null\"},{\"type\":\"object\","
|
||||
+ "\"properties\":{\"string\":{\"type\":\"string\"},\"long\":{\"type\":\"integer\"}}}]}}}";
|
||||
|
||||
final JsonSchema convert = converter.convert(basePath, recordSchema);
|
||||
Assertions.assertEquals(
|
||||
|
|
Loading…
Add table
Reference in a new issue