Skip to content

Commit

Permalink
Use generated item type for array item if it is present
Browse files Browse the repository at this point in the history
Closes #1376
  • Loading branch information
unkish authored and joelittlejohn committed Jan 21, 2023
1 parent 648eb80 commit eedf0ff
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public SchemaMapper() {
* @param schemaUrl
* location of the schema to be used as input
* @return The top-most type generated from the given file
* @throws IOException
* if the schema content cannot be read
*/
public JType generate(JCodeModel codeModel, String className, String packageName, URL schemaUrl) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ public JClass apply(String nodeName, JsonNode node, JsonNode parent, JPackage jp
pathToItems = "#" + schema.getId().getFragment() + "/items";
}
Schema itemsSchema = ruleFactory.getSchemaStore().create(schema, pathToItems, ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());
itemType = ruleFactory.getSchemaRule().apply(makeSingular(nodeName), node.get("items"), node, jpackage, itemsSchema);
itemsSchema.setJavaTypeIfEmpty(itemType);
if (itemsSchema.isGenerated()) {
itemType = itemsSchema.getJavaType();
} else {
itemType = ruleFactory.getSchemaRule().apply(makeSingular(nodeName), node.get("items"), node, jpackage, itemsSchema);
itemsSchema.setJavaTypeIfEmpty(itemType);
}
} else {
itemType = jpackage.owner().ref(Object.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void selfRefUsedForAdditionalPropertiesIsReadSuccessfully() throws NoSuch
assertThat(mapEntryClass.getName(), is("com.example.SelfRefs"));

}

@Test
public void nestedSelfRefsInStringContentWithoutParentFile() throws NoSuchMethodException, ClassNotFoundException, IOException {

Expand All @@ -114,7 +114,18 @@ public void nestedSelfRefsInStringContentWithoutParentFile() throws NoSuchMethod
assertThat(thingClass.getMethod("getNamespace").getReturnType().getSimpleName(), equalTo("String"));
assertThat(thingClass.getMethod("getName").getReturnType().getSimpleName(), equalTo("String"));
assertThat(thingClass.getMethod("getVersion").getReturnType().getSimpleName(), equalTo("String"));

}

}

@Test
public void selfRefUsedInArrayItemIsReadSuccessfully() throws ReflectiveOperationException {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/ref/selfReferencingArrayItem.json", "com.example");

Class<?> generatedType = resultsClassLoader.loadClass("com.example.SelfReferencingArrayItem");
Type listOfAType = generatedType.getMethod("getSelfReferencingArrayItems").getGenericReturnType();
Class<?> listEntryClass = (Class<?>) ((ParameterizedType) listOfAType).getActualTypeArguments()[0];

assertThat(listEntryClass.getName(), is("com.example.SelfReferencingArrayItem"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "array",
"items": {
"type": "object",
"properties": {
"selfReferencingArrayItems": {
"$ref": "#"
}
},
"additionalProperties": false
}
}

0 comments on commit eedf0ff

Please sign in to comment.