joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Not getting the Attributes Datatype as expected #568

Closed nttHarsha closed 8 years ago

nttHarsha commented 8 years ago

JsonSchema:

{
    "models": {
        "Foo": {
            "id": "Foo",
            "properties": {
                "statusId": {
                    "type": "string"
                }
            }
        }
    }
}

java code written convert json schema to pojo:

 JCodeModel codeModel = new JCodeModel();
    try {

        URL source = new URL("file:///c:/Foo.json");

        GenerationConfig config = new DefaultGenerationConfig() {
            @Override
            public boolean isGenerateBuilders() { // set config option by
                                                    // overriding method
                return true;
            }           

             @Override
                public SourceType getSourceType() {
                    return SourceType.JSON;
                }
        };
        SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(), new SchemaStore()),
                new SchemaGenerator());
        mapper.generate(codeModel, "CancelRegAPI", "com.gst", source);
        codeModel.build(new File("D:\\Projects"));

The output is:

 CancelRegAPI.java:
 public class CancelRegAPI {
 @JsonProperty("models")
 private Models models;
 private Map additionalProperties = new HashMap();
 getters and setters...

Models.java:

 public class Models {
 @JsonProperty("Foo")
 private com.gst.Foo Foo;
 private Map additionalProperties = new HashMap();
 getters and setters...

StatusId .java

 public class StatusId {
 @JsonProperty("type")
 private String type;
 private Map additionalProperties = new HashMap();
 getters and setters...

Foo.java:

 public class Foo {
 @JsonProperty("id")
 private String id;
 @JsonProperty("properties")
 private Properties properties;
 private Map additionalProperties = new HashMap();
 getters and setters...

Properties.java:

 public class Properties {
 @JsonProperty("statusId")
 private StatusId statusId;
 private Map additionalProperties = new HashMap();
 getters and setters...

Expected output somewhat or similar to below in the Properties.java:

 public class Properties {
 @JsonProperty("statusId")
 private String statusId;
 private Map additionalProperties = new HashMap();

I don't want any extra class as StatusId.java and additionalProperties in each java class.

Please let me know any solution for the above issue. Thanks in advance.

joelittlejohn commented 8 years ago

The problem here is that you have mixed plain JSON and JSON Schema together. When you use SourceType.JSON then this plugin doesn't expect you to use schema rules like "type": "string", it expects you to give an example of what the data will look like.

You need to either use SourceType.JSONSCHEMA and rewrite your example so that it is a schema, or continue to use SourceType.JSON and don't mix schema rules into your document, e.g.:

{
    "models": {
        "Foo": {
            "statusId": "example"
        }
    }
}