jsonschema2pojo icon indicating copy to clipboard operation
jsonschema2pojo copied to clipboard

Generated attribute takes first type on types list.

Open Jummartinezro opened this issue 3 years ago • 4 comments
trafficstars

Consider the following jsonSchema:

  {
        "type" : "object",
        "properties" : {
            "foo" : {
                "type" : ["boolean", "string","number","integer"]
            }
        }
    }

In this case generated attribute foo will have as type Boolean:

package com.example;

import javax.annotation.Generated;

@Generated("jsonschema2pojo")
public class Example {

public Boolean foo;

}

I noticed that, if type is not specified in jsonSchema, foo takes the type Object:

  {
        "type" : "object",
        "properties" : {
            "foo" : {
            }
        }
    }

Result:

package com.example;

import javax.annotation.Generated;

@Generated("jsonschema2pojo")
public class Example {

public Object foo;

}

There's a way of having the last result (without changing the jsonSchema) when a list of types is passed ?

Jummartinezro avatar Aug 05 '22 12:08 Jummartinezro

Maybe related to #676 ?

Jummartinezro avatar Aug 05 '22 12:08 Jummartinezro

Yes, it's directly related and the answer will be the same

eirnym avatar Aug 05 '22 12:08 eirnym

Often people use union types like: ["boolean", null], where the result is an optional value. In this case, it's obviously desirable to just take the first item. It would probably be better if use we use the following logic for union types:

  1. Remove null values.
  2. If one value remains, use this as the type; If multiple values remain, use Object as the type.

joelittlejohn avatar Aug 06 '22 11:08 joelittlejohn

Logic of taking first type should be changed, as a user can pass ["null", "boolean"] and should have the same result

eirnym avatar Aug 07 '22 19:08 eirnym