Hi Tony,
If you change the order of elements in the author field, it is fine:
@Test
public void testUnion() {
Schema schema = SchemaBuilder
.unionOf()
.record("Author")
.fields()
.name("name").type().stringType().noDefault()
.name("birthday").type().longType().noDefault()
.endRecord().and()
.record("Book")
.fields()
.name("title").type().stringType().noDefault()
.name("author").type().unionOf()
.stringType().and()
.type("Author")
.endUnion()
.noDefault()
.endRecord().endUnion();
}
The specification doesn't say anything about the order of types in a union,
so I guess it shouldn't limit it, I think you're right, your code should
compile. Maybe this is a bug in the SchemaBuilder?
Nandor
On Thu, Jul 6, 2017 at 10:14 AM, Tony Imbault <[email protected]> wrote:
> Hi everybody,
>
>
>
> i am trying to build the followong JSON schema:
>
>
>
> [{
>
> "type": "record",
>
> "name": "Author",
>
> "fields": [{
>
> "name": "name",
>
> "type": "string"
>
> },
>
> {
>
> "name": "birthday",
>
> "type": "long"
>
> }]
>
> },
>
> {
>
> "type": "record",
>
> "name": "Book",
>
> "fields": [{
>
> "name": "title",
>
> "type": "string"
>
> },
>
> {
>
> "name": "author",
>
> "type": ["Author", "string"]
>
> }]
>
> }]
>
>
>
> In my humble opinion this JSON schema is valid.
>
>
>
> But it seems that the SchemaBuilder API does not permit to build this
> schema.
>
> The corresponding code should be something like that:
>
>
>
> Schema schema = SchemaBuilder
>
> .unionOf()
>
> .record("Author")
>
> .fields()
>
> .name("name").type().stringType().noDefault()
>
> .name("birthday").type().
> longType().noDefault()
>
> .endRecord().and()
>
> .record("Book")
>
> .fields()
>
> .name("title").type().stringType().noDefault()
>
> .name("author").type().unionOf()
>
> .type("Author").and()
>
> .stringType().endUnion()
>
> .noDefault()
>
> .endRecord().endUnion();
>
>
>
> But after a call to unionOf() the type(name) method is not available.
>
>
>
> Is there any reason for this limitation?
>
>
>
> Thank you very much
>
>
>
> Tony Imbault
>