Skip to content

Recursive schemas with relative paths can generate the same class twice with "__1" in the name #1077

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

Merged
Merged
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 @@ -55,7 +55,7 @@ public synchronized Schema create(URI id, String refFragmentPathDelimiters) {

if (!schemas.containsKey(id)) {

URI baseId = removeFragment(id);
URI baseId = removeFragment(id).normalize();
JsonNode baseContent = contentResolver.resolve(baseId);
Schema baseSchema = new Schema(baseId, baseContent, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;

import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JType;

Expand Down Expand Up @@ -62,6 +63,26 @@ public void createWithRelativePath() throws URISyntaxException {

}

@Test
public void createWithRelativeSegmentsInPath() throws URISyntaxException {

//Get to the directory of the module jsonschema2pojo-core
Path basePath = Paths.get(getClass().getResource("/schema/person.json").toURI()).getParent().getParent().getParent().getParent();

//Now load the resource with a relative path segment
File relativePath = new File(Paths.get(basePath.toString(),"target", "..", "src", "test", "resources", "schema", "person.json").toString());
//Now load the resource with the same path minus the relative segment
File nonRelativePath = new File(Paths.get(basePath.toString(), "src", "test", "resources", "schema", "person.json").toString());

SchemaStore schemaStore = new SchemaStore();

Schema schemaWithRelativeSegment = schemaStore.create(relativePath.toURI(), "#/.");
Schema schemaWithoutRelativeSegment = schemaStore.create(nonRelativePath.toURI(), "#/.");

//Both schema objects should have the same Id value since their URI's point to the same resource
assertThat(schemaWithoutRelativeSegment.getId(), is(schemaWithRelativeSegment.getId()));
}

@Test
public void createWithSelfRef() throws URISyntaxException {

Expand Down
25 changes: 25 additions & 0 deletions jsonschema2pojo-core/src/test/resources/schema/person.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://example.com/person.json",
"type": "object",
"title": "Person",
"properties": {
"firstName": {
"type": "string",
"minLength": 1
},
"lastName": {
"type": "string",
"minLength": 1
},
"middleInitial": {
"type": "string"
},
"suffix": {
"type": "string"
},
"children": {
"$ref":"person.json"
}
}
}