Impossible to start an union with a named type

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Impossible to start an union with a named type

Tony Imbault

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

Reply | Threaded
Open this post in threaded view
|

Re: Impossible to start an union with a named type

Nandor Kollar
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 <[hidden email]> 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


Reply | Threaded
Open this post in threaded view
|

RE: Impossible to start an union with a named type

Tony Imbault

Thank you Nandor,

 

I had already done this test, but in my context it is very important to respect the specified order.

 

In my opinion the class SchemaBuilder.UnionFieldTypeBuilder should provide the methods type(name), type (name, namespace) and type(schemaType).

 

I will check if a jira already exists for this issue.

 

Thank you

 

Tony

 

 

De : Nandor Kollar [mailto:[hidden email]]
Envoyé : jeudi 6 juillet 2017 10:43
À : [hidden email]
Objet : Re: Impossible to start an union with a named type

 

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 <[hidden email]> 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