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

Generate Method Names in "$ref" Properties Using a "javaName" #1609

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.jsonschema2pojo.rules;

import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.JsonPointerUtils;
import org.jsonschema2pojo.Schema;
Expand Down Expand Up @@ -214,6 +216,10 @@ private JsonNode resolveRefs(JsonNode node, Schema parent) {
if (node.has("$ref")) {
Schema refSchema = ruleFactory.getSchemaStore().create(parent, node.get("$ref").asText(), ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());
JsonNode refNode = refSchema.getContent();
if (node.has("javaName") && (refNode.getNodeType().equals(JsonNodeType.OBJECT))) {
// A shallow copy would also work but this object does not implement `clone``.
refNode = ((ObjectNode) refNode).deepCopy().set("javaName", node.get("javaName"));
}
return resolveRefs(refNode, refSchema);
} else {
return node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public static void generateAndCompileClass() {

}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Test
public void propertiesHaveCorrectNames() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
public void propertiesHaveCorrectNames() throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException {

ClassLoader javaNameClassLoader = schemaRule.generateAndCompile("/schema/javaName/javaName.json", "com.example.javaname");
Class<?> classWithJavaNames = javaNameClassLoader.loadClass("com.example.javaname.JavaName");
Expand All @@ -65,6 +66,9 @@ public void propertiesHaveCorrectNames() throws IllegalAccessException, Instanti
assertThat(instance, hasProperty("enumWithoutJavaName"));
assertThat(instance, hasProperty("javaObject"));
assertThat(instance, hasProperty("objectWithoutJavaName"));
assertThat(instance, hasProperty("javaReference"));
instance.getClass().getMethod("getJavaReference");
assertThat(instance, hasProperty("referenceWithoutJavaName"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
},
"objectWithoutJavaName" : {
"type" : "object"
},
"referenceWithJavaName": {
"$ref": "../properties/primitiveProperties.json",
"javaName": "javaReference"
},
"referenceWithoutJavaName": {
"$ref": "../properties/primitiveProperties.json"
}
}
}