Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method (getter/setter) name not produce as javaName when reference definitions #678

Open
bearx3f opened this issue Jan 15, 2017 · 8 comments

Comments

@bearx3f
Copy link

bearx3f commented Jan 15, 2017

I'm using jsonschema2pojo gradle's plugin v0.4.29 and here's configure, schema, output java

build.gradle Configure

jsonSchema2Pojo { 
	generateBuilders = false
	usePrimitives = true
	source = files("${sourceSets.main.output.resourcesDir}/json")
	targetDirectory = file("${projectDir}/src/main/resources")
	targetPackage = ""
	propertyWordDelimiters = [] as char[]
	useLongIntegers = false
	useDoubleNumbers = true
	useBigDecimals = false
	includeHashcodeAndEquals = true
	includeToString = true
	annotationStyle = 'jackson2'
	customAnnotator = 'org.jsonschema2pojo.NoopAnnotator'
	includeJsr303Annotations = false
	sourceType = 'jsonschema'
	removeOldOutput = false
	outputEncoding = 'UTF-8'
	useJodaDates = false
	useCommonsLang3 = true
	initializeCollections = true
	classNamePrefix = ""
	classNameSuffix = ""
	fileExtensions = [] as String[]
	includeConstructors = false
	parcelable = false
	serializable = true
}

JSON Schema

{
	"definitions": {
		"HR": {
			"javaType": "com.company.department.HR",
			"type": "object",
			"properties": {
				"e": {
					"javaName": "employee",
					"$ref": "#/definitions/Employee"
				}
			}
		},
		"Employee": {
			"javaType": "com.company.department.Employee",
			"type": "object",
			"properties": {
				"name": {
					"type": "string"
				}
			}
		}
	},
	"$ref": "#/definitions/HR"
}

Generated Java

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "e"
})
public class HR implements Serializable
{
    @JsonProperty("e")
    private Employee employee;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    private final static long serialVersionUID = -3217667021245126803L;

    @JsonProperty("e")
    public Employee getE() { //here problem
        return employee;
    }

    @JsonProperty("e")
    public void setE(Employee employee) { //here too.
        this.employee = employee;
    }
}
@sjohnr
Copy link

sjohnr commented Feb 8, 2017

Any thoughts on this issue? I think I'm running into the same problem. Rough example (hand edited, so may not be 100% accurate):

{
  "_links": {
    "type": "object",
    "javaName": "Links",
    "javaType": "java.util.Map<String, Object>"
  }
}

Generated Java:

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "_links"
})
public class Example {

    @JsonProperty("_links")
    private Map<String, Object> links;
    @JsonIgnore
    private Map<java.lang.String, Object> additionalProperties = new HashMap<java.lang.String, Object>();

    /**
     * 
     * @return
     *     The _links
     */
    @JsonProperty("_links")
    public Map<String, Object> get_links() {
        return links;
    }

    /**
     * 
     * @param _links
     *     The _links
     */
    @JsonProperty("_links")
    public void set_links(Map<String, Object> links) {
        this.links = links;
    }

    public Example withLinks(Map<String, Object> links) {
        this.links = links;
        return this;
    }

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

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

    public Example withAdditionalProperty(java.lang.String name, Object value) {
        this.additionalProperties.put(name, value);
        return this;
    }

}

I find it odd that it only appears to affect gradle. Doesn't seem to be an issue with the maven plugin.

@sjohnr
Copy link

sjohnr commented Feb 8, 2017

Actually, I just spotted my issue in your example. I had the propertyWordDelimiters = [] as char[] set, so changed it to propertyWordDelimiters = ['_'] as char[].

@bearx3f
Copy link
Author

bearx3f commented Feb 8, 2017

In my case I use $ref to defined schema, I thoughts when "javaName":"Employee" should generate getter/setter to

Employee getEmployee() { ... }

and

void setEmployee(Employee e) { ... }

@joelittlejohn
Copy link
Owner

joelittlejohn commented Feb 8, 2017

Hi @X3STeNLiTE. When you use a $ref, the entire node is replaced by the ref contents. No values are kept from the properties that reside alongside the ref. This is how JSON Schema is specified:

Any time a subschema is expected, a schema may instead use an object containing a "$ref" property. The value of the $ref is a URI Reference. Resolved against the current URI base, it identifies the URI of a schema to use. All other properties in a "$ref" object MUST be ignored.

-- http://json-schema.org/latest/json-schema-core.html#rfc.section.7

In this case we could make a special case for javaName, I can see it's useful, but this at least explains what you're seeing here and why.

@bearx3f
Copy link
Author

bearx3f commented Feb 8, 2017

Thank you for you're explanation, looking forward for the next build : )

@sjohnr
Copy link

sjohnr commented Feb 8, 2017

That actually explains the other part of my issue too. Thanks!

@tmoreira2020
Copy link

Hey @joelittlejohn, your last explanation makes sense, BUT the tool is generating the field name correct, e.g. respecting the javaName directive, so is it a bug?
For me makes a lot of sense have getters/setters and field name derived from javaName even thought the user uses $ref

@erikvanderwerf
Copy link

erikvanderwerf commented May 2, 2024

Resurrecting from the grave, I am currently also experiencing this issue. I am using a $ref with javaName, which does rename the field as expected but does not alter the getter or setter method names.

"foo": {
    "$ref": "other.json",
    "javaName": "foobar"
}

Generates:

@JsonProperty("foo")
@JsonPropertyDescription("...")
private FooBar foobar;

@JsonProperty("foo")
public FooBar getFoo() {
    return foobar;
}

When it would have conceptually made more sense to have getFooBar/setFooBar methods. This is especially useful when the JSON property is using an abbreviated spelling but I want to use the longer or expanded form in Java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants