diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Schema.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Schema.java index f849e35bf..225c74c95 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Schema.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Schema.java @@ -30,7 +30,7 @@ public class Schema { private JsonNode content; private JType javaType; - protected Schema(URI id, JsonNode content) { + public Schema(URI id, JsonNode content) { this.id = id; this.content = content; } diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaStore.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaStore.java index cd728dad8..e2c1733ff 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaStore.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaStore.java @@ -80,15 +80,24 @@ public Schema create(Schema parent, String path) { if (path.equals("#")) { return parent; } - + path = stripEnd(path, "#?&/"); URI id = (parent == null || parent.getId() == null) ? URI.create(path) : parent.getId().resolve(path); + if (selfReferenceWithoutParentFile(parent, path)) { + schemas.put(id, new Schema(id, fragmentResolver.resolve(parent.getContent(), path))); + return schemas.get(id); + } + return create(id); } + private boolean selfReferenceWithoutParentFile(Schema parent, String path) { + return parent != null && parent.getId() == null && path.startsWith("#/"); + } + public synchronized void clearCache() { schemas.clear(); } diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/FragmentRefIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/FragmentRefIT.java index 1bdec0400..1eeeeb280 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/FragmentRefIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/ref/FragmentRefIT.java @@ -16,13 +16,22 @@ package org.jsonschema2pojo.integration.ref; -import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*; import static org.hamcrest.Matchers.*; +import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*; import static org.junit.Assert.*; +import java.io.IOException; + +import org.jsonschema2pojo.Schema; +import org.jsonschema2pojo.rules.RuleFactory; import org.junit.BeforeClass; import org.junit.Test; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.codemodel.JCodeModel; +import com.sun.codemodel.JPackage; + public class FragmentRefIT { private static Class fragmentRefsClass; @@ -53,5 +62,14 @@ public void refToFragmentOfAnotherSchemaIsReadSuccessfully() throws NoSuchMethod assertThat(aClass.getName(), is("com.example.AdditionalPropertyValue")); } + + @Test + public void selfRefWithoutParentFile() throws IOException { + JCodeModel codeModel = new JCodeModel(); + JsonNode schema = new ObjectMapper().readTree("{\"type\":\"object\", \"properties\":{\"a\":{\"$ref\":\"#/b\"}}, \"b\":\"string\"}"); + + JPackage p = codeModel._package("com.example"); + new RuleFactory().getSchemaRule().apply("Example", schema, p, new Schema(null, schema)); + } } \ No newline at end of file