Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if default is present, field counts not required #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/main/java/com/github/fge/avro/translators/RecordTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,27 @@
import org.codehaus.jackson.map.ObjectMapper;

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

final class RecordTranslator
extends NamedAvroTypeTranslator
{
extends NamedAvroTypeTranslator {
private static final ObjectMapper OLD_MAPPER = new ObjectMapper();

private static final AvroTranslator INSTANCE = new RecordTranslator();

private RecordTranslator()
{
private RecordTranslator() {
super(Schema.Type.RECORD);
}

public static AvroTranslator getInstance()
{
public static AvroTranslator getInstance() {
return INSTANCE;
}

@Override
protected void doTranslate(final Schema avroSchema,
final MutableTree jsonSchema, final ProcessingReport report)
throws ProcessingException
{
final MutableTree jsonSchema, final ProcessingReport report)
throws ProcessingException {
final List<Schema.Field> fields = avroSchema.getFields();

if (fields.isEmpty()) {
Expand All @@ -66,14 +63,12 @@ protected void doTranslate(final Schema avroSchema,

final JsonPointer pwd = jsonSchema.getPointer();

if (avroSchema.getDoc() != null)
if (avroSchema.getDoc() != null) {
jsonSchema.getCurrentNode().put("description", avroSchema.getDoc());
}

jsonSchema.setType(NodeType.OBJECT);

final ArrayNode required = FACTORY.arrayNode();
jsonSchema.getCurrentNode().put("required", required);

jsonSchema.getCurrentNode().put("additionalProperties", false);

final ObjectNode properties = FACTORY.objectNode();
Expand All @@ -91,12 +86,15 @@ protected void doTranslate(final Schema avroSchema,
* FIXME: "default" and readers'/writers' schema? Here, even with a
* default value, the record field is marked as required.
*/
for (final Schema.Field field: fields) {
final List<String> requiredFields = new LinkedList<String>();
for (final Schema.Field field : fields) {
fieldName = field.name();
fieldSchema = field.schema();
fieldType = fieldSchema.getType();
translator = AvroTranslators.getTranslator(fieldType);
required.add(fieldName);
if (field.defaultValue() == null) {
requiredFields.add(fieldName);
}
ptr = JsonPointer.of("properties", fieldName);
propertyNode = FACTORY.objectNode();
properties.put(fieldName, propertyNode);
Expand All @@ -105,14 +103,22 @@ protected void doTranslate(final Schema avroSchema,
translator.translate(fieldSchema, jsonSchema, report);
jsonSchema.setPointer(pwd);
}

if (requiredFields.size() > 0) {
final ArrayNode required = FACTORY.arrayNode();
for (String requiredFieldName : requiredFields) {
required.add(requiredFieldName);
}
jsonSchema.getCurrentNode().put("required", required);
}
}

private static void injectDefault(final ObjectNode propertyNode,
final Schema.Field field)
{
final Schema.Field field) {
final JsonNode value = field.defaultValue();
if (value == null)
if (value == null) {
return;
}

/*
* Write the value to a string using a 1.8 writer, and read it from that
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/com/github/fge/avro/AvroTranslationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.testng.Assert.*;

Expand Down Expand Up @@ -79,20 +80,17 @@ public final Iterator<Object[]> testData()
}

@Test(
dataProvider = "testData",
invocationCount = 10,
threadPoolSize = 4
dataProvider = "testData"
)
public final void conversionIsCorrectlyPerformed(final JsonNode avroSchema,
final JsonNode jsonSchema)
throws ProcessingException
{
final ValueHolder<JsonTree> input
= ValueHolder.<JsonTree>hold(new SimpleJsonTree(avroSchema));

JsonTree tree = new SimpleJsonTree(avroSchema);
final ValueHolder<JsonTree> input = ValueHolder.hold(tree);
final ValueHolder<SchemaTree> output = PROCESSOR.process(report, input);
assertTrue(output.getValue().getBaseNode().equals(jsonSchema));

assertTrue(VALIDATOR.schemaIsValid(jsonSchema));
assertTrue(output.getValue().getBaseNode().equals(jsonSchema));
}
}
3 changes: 1 addition & 2 deletions src/test/resources/avro/fromSource.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"record:org.apache.avro.FooBarSpecificRecord": {
"type": "object",
"required": [ "id", "relatedids", "typeEnum" ],
"required": [ "id", "relatedids" ],
"additionalProperties": false,
"properties": {
"id": {
Expand Down Expand Up @@ -428,7 +428,6 @@
"definitions": {
"record:org.apache.avro.mapreduce.TextStats": {
"type": "object",
"required": [ "name", "count" ],
"additionalProperties": false,
"properties": {
"name": {
Expand Down
13 changes: 7 additions & 6 deletions src/test/resources/avro/record.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"fields": [
{
"name": "value",
"type": "long"
"type": "long",
"default": 1
},
{
"name": "next",
Expand All @@ -21,12 +22,13 @@
"record:LongList": {
"type": "object",
"additionalProperties": false,
"required": [ "value", "next" ],
"required": [ "next" ],
"properties": {
"value": {
"type": "integer",
"minimum": -9223372036854775808,
"maximum": 9223372036854775807
"maximum": 9223372036854775807,
"default": 1
},
"next": {
"oneOf": [
Expand Down Expand Up @@ -57,22 +59,21 @@
"type": "record",
"name": "bar",
"fields": [
{ "name": "moo", "type": "int", "default": -1 }
{ "name": "moo", "type": "int", "default": 1 }
]
},
"jsonSchema": {
"$ref": "#/definitions/record:bar",
"definitions": {
"record:bar": {
"type": "object",
"required": [ "moo" ],
"additionalProperties": false,
"properties": {
"moo": {
"type": "integer",
"minimum": -2147483648,
"maximum": 2147483647,
"default": -1
"default": 1
}
}
}
Expand Down