Json schema to Pojo generator in Java using library jsonschema2pojo-core

Viewed 29

I am trying to convert json schema to pojo using library jsonschema2pojo-core version 1.0.2 but I am getting error "trying to create the same field twice: additionalProperties". I am also attaching the code that I am using with Library. Can you guys let me know what I doing wrong here.

class JsonToPojo {


    public static void main(String[] args) throws IOException {
        JCodeModel codeModel = new JCodeModel();
        URL source = JsonToPojo.class.getResource("/test.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(config), new SchemaStore()), new SchemaGenerator());
        mapper.generate(codeModel, "JsonToPojo", "com.controller", source);
        File file = Files.createTempDirectory("required").toFile();
        codeModel.build(file);
        System.out.println("path is " + file);
    }

Json -

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    }
  }
}

Expected Pojo-

*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstName",
"lastName",
"age"
})
@Generated("jsonschema2pojo")
public class Example {

/**
* The person's first name.
*
*/
@JsonProperty("firstName")
@JsonPropertyDescription("The person's first name.")
private String firstName;
/**
* The person's last name.
*
*/
@JsonProperty("lastName")
@JsonPropertyDescription("The person's last name.")
private String lastName;
/**
* Age in years which must be equal to or greater than zero.
*
*/
@JsonProperty("age")
@JsonPropertyDescription("Age in years which must be equal to or greater than zero.")
private Integer age;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* The person's first name.
*
*/
@JsonProperty("firstName")
public String getFirstName() {
return firstName;
}

/**
* The person's first name.
*
*/
@JsonProperty("firstName")
public void setFirstName(String firstName) {
this.firstName = firstName;
}

/**
* The person's last name.
*
*/
@JsonProperty("lastName")
public String getLastName() {
return lastName;
}

/**
* The person's last name.
*
*/
@JsonProperty("lastName")
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
* Age in years which must be equal to or greater than zero.
*
*/
@JsonProperty("age")
public Integer getAge() {
return age;
}

/**
* Age in years which must be equal to or greater than zero.
*
*/
@JsonProperty("age")
public void setAge(Integer age) {
this.age = age;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

0 Answers
Related