Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Avro fields with default values, or unions that include the null type…
Browse files Browse the repository at this point in the history
…, should not be included in required properties of JSON schema

Combined fixes in the following PRs from the forked repository:
fge#2
fge#3
  • Loading branch information
snagafritz committed Mar 16, 2018
1 parent 89bbec9 commit a9659d5
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ protected void doTranslate(final Schema avroSchema,
fieldSchema = field.schema();
fieldType = fieldSchema.getType();
translator = AvroTranslators.getTranslator(fieldType);
required.add(fieldName);
if (isRequiredField(field)) {
required.add(fieldName);
}
ptr = JsonPointer.of("properties", fieldName);
propertyNode = FACTORY.objectNode();
properties.put(fieldName, propertyNode);
Expand All @@ -107,6 +109,23 @@ protected void doTranslate(final Schema avroSchema,
}
}

private boolean isRequiredField(Schema.Field field) {
if (field.defaultValue() != null) {
return false;
}

Schema fieldSchema = field.schema();
if (fieldSchema.getType() == Schema.Type.UNION) {
for (Schema typeSchema : fieldSchema.getTypes()) {
if (typeSchema.getType() == Schema.Type.NULL) {
return false;
}
}
}

return true;
}

private static void injectDefault(final ObjectNode propertyNode,
final Schema.Field field)
{
Expand Down

0 comments on commit a9659d5

Please sign in to comment.