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

Generation of uncompilable code #1059

Open sonOfRa opened 4 years ago

sonOfRa commented 4 years ago

The following schema is an excerpt of a larger schema, but I narrowed it down as much as I could so that the faulty behaviour still exists:

{
  "title": "SSCC schema",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "Version 2.00",
  "definitions": {
    "payment_type": {
      "enum": [
        "Cash",
        "Electronic"
      ]
    }
  },
  "type": "object",
  "properties": {
    "payment_types": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "$ref": "#/definitions/payment_type"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

Generating code from this schema leads to code that does not compile:


package com.example.schema;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonValue;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "type",
    "name"
})
public class PaymentType {

    @JsonProperty("type")
    private PaymentType.PaymentType type;
    @JsonProperty("name")
    private String name;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("type")
    public PaymentType.PaymentType getType() {
        return type;
    }

    @JsonProperty("type")
    public void setType(PaymentType.PaymentType type) {
        this.type = type;
    }

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

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

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

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(PaymentType.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
        sb.append("type");
        sb.append('=');
        sb.append(((this.type == null)?"<null>":this.type));
        sb.append(',');
        sb.append("name");
        sb.append('=');
        sb.append(((this.name == null)?"<null>":this.name));
        sb.append(',');
        sb.append("additionalProperties");
        sb.append('=');
        sb.append(((this.additionalProperties == null)?"<null>":this.additionalProperties));
        sb.append(',');
        if (sb.charAt((sb.length()- 1)) == ',') {
            sb.setCharAt((sb.length()- 1), ']');
        } else {
            sb.append(']');
        }
        return sb.toString();
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = ((result* 31)+((this.name == null)? 0 :this.name.hashCode()));
        result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
        result = ((result* 31)+((this.type == null)? 0 :this.type.hashCode()));
        return result;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof PaymentType) == false) {
            return false;
        }
        PaymentType rhs = ((PaymentType) other);
        return ((((this.name == rhs.name)||((this.name!= null)&&this.name.equals(rhs.name)))&&((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)&&this.additionalProperties.equals(rhs.additionalProperties))))&&((this.type == rhs.type)||((this.type!= null)&&this.type.equals(rhs.type))));
    }

    public enum PaymentType {

        CASH("Cash"),
        ELECTRONIC("Electronic");
        private final String value;
        private final static Map<String, PaymentType.PaymentType> CONSTANTS = new HashMap<String, PaymentType.PaymentType>();

        static {
            for (PaymentType.PaymentType c: values()) {
                CONSTANTS.put(c.value, c);
            }
        }

        private PaymentType(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return this.value;
        }

        @JsonValue
        public String value() {
            return this.value;
        }

        @JsonCreator
        public static PaymentType.PaymentType fromValue(String value) {
            PaymentType.PaymentType constant = CONSTANTS.get(value);
            if (constant == null) {
                throw new IllegalArgumentException(value);
            } else {
                return constant;
            }
        }

    }

}

The internal enum for the PaymentType class uses the same name as the enclosing class, which is invalid syntax in Java.

The reason seems to be that the same names get derived for the payment_types array property, as well as the payment_type enum definition in the schema. Changing the schema is currently not really an option, as the schema in question is not under our control, we are just generating JSON according to the schema we are given.

giordanolucas commented 4 years ago

Same thing is happening to me. Is there a workaround for this?

sonOfRa commented 4 years ago

@giordanolucas well my workaround was transcribing the entire ~1200 line json schema by hand. Not for everyone I admit, but it does work (and faster than one would think).

@joelittlejohn do you have any idea on a solution for this?

joelittlejohn commented 4 years ago

You could probably work around this right now by adding an enum name suffix of 'Enum'. There's a config property to allow you to add this suffix.

To fix this in general I think we need to introduce an extra check here so that the enum is never given the same name as its enclosing class. It could be disambiguated with an underscore.

ddcruver commented 4 years ago

Normally I would suggest adding a prefix of "Type" to the enumeration but since it ends in "Type" already you get the weird duplicate "TypeType" suffix. While this generation should not happen maybe you should change the name of enclosing class to differentiate it like PaymentMethod or PaymentCategory.

giordanolucas commented 4 years ago

Hi! In my example, I can not alter the source Json. Is there a way of configuring a global suffix for all Enums using the Java API? That would work for me.