joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc

Home Page:http://www.jsonschema2pojo.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Duplicate constructors generated when all properties are required; and both includeAllPropertiesConstructor and includeRequiredPropertiesConstructor are true

brianfelder opened this issue · comments

Given a schema, where all properties are marked as required:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "javaType": "com.example.Person",
  "properties": {
    "first": {
      "type": "string"
    },
    "last": {
      "type": "string"
    },
    "email": {
      "type": "string"
    }
  },
  "required": [
    "first",
    "last",
    "email"
  ]
}

When building with the jsonschema2pojo maven plugin with these properties set:

    <includeConstructors>true</includeConstructors>
    <includeAllPropertiesConstructor>true</includeAllPropertiesConstructor>
    <includeRequiredPropertiesConstructor>true</includeRequiredPropertiesConstructor>

Then it results in a pojo with a duplicate constructor with identical arguments, which results in a compilation error on the resulting class:

    public Person(String first, String last, String email) {
        super();
        this.first = first;
        this.last = last;
        this.email = email;
    }

    public Person(String first, String last, String email) {
        super();
        this.first = first;
        this.last = last;
        this.email = email;
    }

Expected behavior: When detecting that all properties are required, generate the constructor only once.

Note: The reason this is an issue is that some schemas in our project mark all properties as required; while other schemas mark only some properties as required. For schemas with only some properties as required, we want constructors for both the all and the required cases.

Possible fix location: I believe a fix could be achieved the ConstructorRule.handleMultiChoiceConstructorConfiguration(), by suppressing one of the constructor generations if classProperties and requiredClassProperties are identical.