From eedf0ff4eaaefcdef6a8c9d3115cd0e1157fa3df Mon Sep 17 00:00:00 2001 From: unkish <3533269+unkish@users.noreply.github.com> Date: Thu, 29 Dec 2022 21:50:29 +0200 Subject: [PATCH] Use generated item type for array item if it is present Closes #1376 --- .../java/org/jsonschema2pojo/SchemaMapper.java | 2 -- .../org/jsonschema2pojo/rules/ArrayRule.java | 8 ++++++-- .../integration/ref/SelfRefIT.java | 17 ++++++++++++++--- .../schema/ref/selfReferencingArrayItem.json | 12 ++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/ref/selfReferencingArrayItem.json diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java index 4cf1d91d3..03522363e 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java @@ -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) { diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ArrayRule.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ArrayRule.java index 1915fc5df..d83b64014 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ArrayRule.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ArrayRule.java @@ -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); } diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/SelfRefIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/SelfRefIT.java index f66b2d09f..b854e8be1 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/SelfRefIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/SelfRefIT.java @@ -92,7 +92,7 @@ public void selfRefUsedForAdditionalPropertiesIsReadSuccessfully() throws NoSuch assertThat(mapEntryClass.getName(), is("com.example.SelfRefs")); } - + @Test public void nestedSelfRefsInStringContentWithoutParentFile() throws NoSuchMethodException, ClassNotFoundException, IOException { @@ -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")); + } } \ No newline at end of file diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/selfReferencingArrayItem.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/selfReferencingArrayItem.json new file mode 100644 index 000000000..2d5491df0 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/ref/selfReferencingArrayItem.json @@ -0,0 +1,12 @@ +{ + "type": "array", + "items": { + "type": "object", + "properties": { + "selfReferencingArrayItems": { + "$ref": "#" + } + }, + "additionalProperties": false + } +} \ No newline at end of file