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

Add support for $ref #4

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.jsonschema.core.util.ValueHolder;
import com.github.fge.jsonschema2avro.writers.ArrayWriter;
import com.github.fge.jsonschema2avro.writers.EnumWriter;
import com.github.fge.jsonschema2avro.writers.MapWriter;
import com.github.fge.jsonschema2avro.writers.RecordWriter;
import com.github.fge.jsonschema2avro.writers.SimpleTypeWriter;
import com.github.fge.jsonschema2avro.writers.SimpleUnionWriter;
import com.github.fge.jsonschema2avro.writers.TypeUnionWriter;
import com.github.fge.jsonschema2avro.writers.*;
import org.apache.avro.Schema;

import static com.github.fge.jsonschema2avro.predicates.AvroPredicates.*;
Expand All @@ -50,6 +44,7 @@ public AvroWriterProcessor()
.when(record()).then(new RecordWriter())
.when(simpleUnion()).then(SimpleUnionWriter.getInstance())
.when(typeUnion()).then(TypeUnionWriter.getInstance())
.when(typeRef()).then(RefWriter.getInstance())
.getProcessor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ public boolean apply(final AvroPayload input)
};
}

public static Predicate<AvroPayload> typeRef()
{
return new Predicate<AvroPayload>()
{
@Override
public boolean apply(final AvroPayload input)
{
return schemaNode(input).path("$ref").isTextual();
}
};
}

private static JsonNode schemaNode(final AvroPayload payload)
{
return payload.getTree().getNode();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2014, Francis Galiegue ([email protected])
*
* This software is dual-licensed under:
*
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
* later version;
* - the Apache Software License (ASL) version 2.0.
*
* The text of both licenses is available under the src/resources/ directory of
* this project (under the names LGPL-3.0.txt and ASL-2.0.txt respectively).
*
* Direct link to the sources:
*
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
*/

package com.github.fge.jsonschema2avro.writers;

import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jsonschema.core.exceptions.JsonReferenceException;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.ref.JsonRef;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.jsonschema.core.util.ValueHolder;
import com.github.fge.jsonschema2avro.AvroWriterProcessor;
import org.apache.avro.Schema;

public final class RefWriter
extends AvroWriter
{
private static final AvroWriter INSTANCE = new RefWriter();

private RefWriter()
{
}

public static AvroWriter getInstance()
{
return INSTANCE;
}

@Override
protected Schema generate(final AvroWriterProcessor writer,
final ProcessingReport report, final SchemaTree tree)
throws ProcessingException
{
JsonPointer ptr = extractJsonRef(tree).getPointer();
ValueHolder<SchemaTree> holder = ValueHolder.hold("schema", tree.setPointer(ptr));
return writer.process(report, holder).getValue();
}

private static JsonRef extractJsonRef(SchemaTree tree) {
String $ref = tree.getNode().get("$ref").textValue();
try {
return JsonRef.fromString($ref);
} catch (JsonReferenceException e) {
throw new IllegalStateException("Could not parse $ref " + $ref);
}
}
}
31 changes: 31 additions & 0 deletions src/test/java/com/github/fge/jsonschema2avro/RefTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2014, Francis Galiegue ([email protected])
*
* This software is dual-licensed under:
*
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
* later version;
* - the Apache Software License (ASL) version 2.0.
*
* The text of both licenses is available under the src/resources/ directory of
* this project (under the names LGPL-3.0.txt and ASL-2.0.txt respectively).
*
* Direct link to the sources:
*
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
*/

package com.github.fge.jsonschema2avro;

import java.io.IOException;

public final class RefTest
extends AvroWriterProcessorTest
{
public RefTest()
throws IOException
{
super("ref");
}
}
25 changes: 25 additions & 0 deletions src/test/resources/jsonschema2avro/ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{
"jsonSchema": {
"type": "object",
"definitions": {
"customNumber": {
"type": "number"
}
},
"properties": {
"someNumber": {
"$ref": "#/definitions/customNumber"
}
},
"additionalProperties": false
},
"avroSchema": {
"type": "record",
"name": "record0",
"fields": [
{ "name": "someNumber", "type": "double" }
]
}
}
]