Skip to content

Commit 156e829

Browse files
committed
Write and test JSON Schema to avro map
Signed-off-by: Francis Galiegue <[email protected]>
1 parent 25846a8 commit 156e829

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

src/main/java/com/github/fge/jsonschema2avro/AvroWriterProcessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.github.fge.jsonschema.util.JsonLoader;
1313
import com.github.fge.jsonschema.util.ValueHolder;
1414
import com.github.fge.jsonschema2avro.writers.ArrayWriter;
15+
import com.github.fge.jsonschema2avro.writers.MapWriter;
1516
import com.github.fge.jsonschema2avro.writers.SimpleTypeWriter;
1617
import org.apache.avro.Schema;
1718

@@ -29,6 +30,7 @@ public AvroWriterProcessor()
2930
processor = new ProcessorSelector<AvroPayload, ValueHolder<Schema>>()
3031
.when(simpleType()).then(SimpleTypeWriter.getInstance())
3132
.when(array()).then(ArrayWriter.getInstance())
33+
.when(map()).then(MapWriter.getInstance())
3234
.getProcessor();
3335
}
3436

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.fge.jsonschema2avro.writers;
2+
3+
import com.github.fge.jsonschema.exceptions.ProcessingException;
4+
import com.github.fge.jsonschema.jsonpointer.JsonPointer;
5+
import com.github.fge.jsonschema.processors.data.SchemaHolder;
6+
import com.github.fge.jsonschema.report.ProcessingReport;
7+
import com.github.fge.jsonschema.tree.SchemaTree;
8+
import com.github.fge.jsonschema2avro.AvroWriterProcessor;
9+
import org.apache.avro.Schema;
10+
11+
public final class MapWriter
12+
extends AvroWriter
13+
{
14+
private static final JsonPointer POINTER
15+
= JsonPointer.of("additionalProperties");
16+
17+
private static final AvroWriter INSTANCE = new MapWriter();
18+
19+
private MapWriter()
20+
{
21+
}
22+
23+
public static AvroWriter getInstance()
24+
{
25+
return INSTANCE;
26+
}
27+
28+
@Override
29+
protected Schema generate(final AvroWriterProcessor writer,
30+
final ProcessingReport report, final SchemaTree tree)
31+
throws ProcessingException
32+
{
33+
final SchemaHolder input = new SchemaHolder(tree.append(POINTER));
34+
final Schema valueSchema = writer.process(report, input).getValue();
35+
return Schema.createMap(valueSchema);
36+
}
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.fge.jsonschema2avro;
2+
3+
import java.io.IOException;
4+
5+
public final class MapTest
6+
extends AvroWriterProcessorTest
7+
{
8+
public MapTest()
9+
throws IOException
10+
{
11+
super("map");
12+
}
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[
2+
{
3+
"jsonSchema": {
4+
"type": "object",
5+
"additionalProperties": { "type": "string" }
6+
},
7+
"avroSchema": {
8+
"type": "map",
9+
"values": "string"
10+
}
11+
},
12+
{
13+
"jsonSchema": {
14+
"type": "object",
15+
"additionalProperties": {
16+
"type": "array",
17+
"items": { "type": "number" }
18+
}
19+
},
20+
"avroSchema": {
21+
"type": "map",
22+
"values": { "type": "array", "items": "double" }
23+
}
24+
}
25+
]

0 commit comments

Comments
 (0)