POJO generated from jsonschema2pojo generating class that should not exist

nikel picture nikel · Jun 13, 2015 · Viewed 8.3k times · Source

I am using jsonschema2pojo from command line to create POJO's for the following json's :

book_store.json

book_details.json

{
	"type": "object",
	"$schema": "http://json-schema.org/draft-04/schema",
	"description": "attributes of a book",
	"properties": {
		"id": {
			"type": "string",
			"description": "ID of the parameteror category or channel or file"
		},

		"code": {
			"type": "string",
			"description": "key of the parameter or category or channel or file"
		},

		"description": {
			"type": "string",
			"description": "description of the parameter or category or channel or file"
		}
	}
}

Now the generated Java POJO's look like this :

BookStore.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
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 org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
 * Books that I have
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "soft-copies",
    "hard-copies"
})
public class BookStore {

    @JsonProperty("soft-copies")
    private List<SoftCopy> softCopies = new ArrayList<SoftCopy>();
    @JsonProperty("hard-copies")
    private List<SoftCopy> hardCopies = new ArrayList<SoftCopy>();
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * 
     * @return
     *     The softCopies
     */
    @JsonProperty("soft-copies")
    public List<SoftCopy> getSoftCopies() {
        return softCopies;
    }

    /**
     * 
     * @param softCopies
     *     The soft-copies
     */
    @JsonProperty("soft-copies")
    public void setSoftCopies(List<SoftCopy> softCopies) {
        this.softCopies = softCopies;
    }

    /**
     * 
     * @return
     *     The hardCopies
     */
    @JsonProperty("hard-copies")
    public List<SoftCopy> getHardCopies() {
        return hardCopies;
    }

    /**
     * 
     * @param hardCopies
     *     The hard-copies
     */
    @JsonProperty("hard-copies")
    public void setHardCopies(List<SoftCopy> hardCopies) {
        this.hardCopies = hardCopies;
    }

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

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

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

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(softCopies).append(hardCopies).append(additionalProperties).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof BookStore) == false) {
            return false;
        }
        BookStore rhs = ((BookStore) other);
        return new EqualsBuilder().append(softCopies, rhs.softCopies).append(hardCopies, rhs.hardCopies).append(additionalProperties, rhs.additionalProperties).isEquals();
    }

}

SoftCopy.java

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
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 org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
 * attributes of a book
 * 
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "id",
    "code",
    "description"
})
public class SoftCopy {

    /**
     * ID of the parameteror category or channel or file
     * 
     */
    @JsonProperty("id")
    private String id;
    /**
     * key of the parameter or category or channel or file
     * 
     */
    @JsonProperty("code")
    private String code;
    /**
     * description of the parameter or category or channel or file
     * 
     */
    @JsonProperty("description")
    private String description;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * ID of the parameteror category or channel or file
     * 
     * @return
     *     The id
     */
    @JsonProperty("id")
    public String getId() {
        return id;
    }

    /**
     * ID of the parameteror category or channel or file
     * 
     * @param id
     *     The id
     */
    @JsonProperty("id")
    public void setId(String id) {
        this.id = id;
    }

    /**
     * key of the parameter or category or channel or file
     * 
     * @return
     *     The code
     */
    @JsonProperty("code")
    public String getCode() {
        return code;
    }

    /**
     * key of the parameter or category or channel or file
     * 
     * @param code
     *     The code
     */
    @JsonProperty("code")
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * description of the parameter or category or channel or file
     * 
     * @return
     *     The description
     */
    @JsonProperty("description")
    public String getDescription() {
        return description;
    }

    /**
     * description of the parameter or category or channel or file
     * 
     * @param description
     *     The description
     */
    @JsonProperty("description")
    public void setDescription(String description) {
        this.description = description;
    }

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

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

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

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id).append(code).append(description).append(additionalProperties).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof SoftCopy) == false) {
            return false;
        }
        SoftCopy rhs = ((SoftCopy) other);
        return new EqualsBuilder().append(id, rhs.id).append(code, rhs.code).append(description, rhs.description).append(additionalProperties, rhs.additionalProperties).isEquals();
    }

}

Whereas i was not even expecting SoftCopy.java object...I expected to have "BookStore.java" & "BookDetails.java" . SoftCopy and HardCopy should be lists of the type BookDetails.java

Answer

nikel picture nikel · Jun 13, 2015

Fixed this issue by using the extension "javaType" - https://github.com/joelittlejohn/jsonschema2pojo/issues/164