Skip to content

Commit

Permalink
Derive child schemas for objects and arrays (as the JavaDoc of Schema…
Browse files Browse the repository at this point in the history
…Rule already suggests).

The relevant bits are in `SchemaRule#L79`, where a child schema is obtained and passed down
the rule tree as the schema for the relevant section. This seems to be the expected behavior
by `NotRequiredRule`, among others.

Changes `Schema` to keep reference to its parent `Schema` instead of parent `JsonNode`.
This means `Schema(id, JsonNode content, JsonNode parentContent)` is replaced by
`Schema(id, JsonNode content, Schema parent)`.

`Schema#getParent` still returns _self_ as did `#getParentContent` previously.

Fixes joelittlejohngh-906 (JSR305 bug) and depends on joelittlejohngh-921 (integration tests for JSR305 bug)
  • Loading branch information
jrehwaldt committed Sep 18, 2018
1 parent d5b754d commit 59d48bf
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 19 deletions.
18 changes: 13 additions & 5 deletions jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public class Schema {

private final URI id;
private final JsonNode content;
private final JsonNode parentContent;
private final Schema parent;
private JType javaType;

public Schema(URI id, JsonNode content, JsonNode parentContent) {
public Schema(URI id, JsonNode content, Schema parent) {
this.id = id;
this.content = content;
this.parentContent = parentContent;
this.parent = parent != null ? parent : this;
}

public JType getJavaType() {
Expand All @@ -59,12 +59,20 @@ public JsonNode getContent() {
return content;
}

public JsonNode getParentContent() {
return parentContent;
public Schema getParent() {
return parent;
}

public boolean isGenerated() {
return javaType != null;
}

public Schema derive(JsonNode content) {
if (content != this.content) {
return new Schema(id, content, this);
} else {
return this;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public JType generate(JCodeModel codeModel, String className, String packageName

ObjectNode schemaNode = readSchema(schemaUrl);

return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, schemaNode));
return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, null));

}

Expand Down Expand Up @@ -118,7 +118,7 @@ public JType generate(JCodeModel codeModel, String className, String packageName
JsonNode schemaNode = objectMapper().readTree(json);

return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage,
new Schema(schemaLocation, schemaNode, schemaNode));
new Schema(schemaLocation, schemaNode, null));
}

public JType generate(JCodeModel codeModel, String className, String packageName, String json) throws IOException {
Expand All @@ -133,7 +133,7 @@ public JType generate(JCodeModel codeModel, String className, String packageName
schemaNode = objectMapper().readTree(json);
}

return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, schemaNode));
return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, null));
}

private ObjectMapper objectMapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ public synchronized Schema create(URI id, String refFragmentPathDelimiters) {

if (!schemas.containsKey(id)) {

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

if (id.toString().contains("#")) {
JsonNode childContent = fragmentResolver.resolve(content, '#' + id.getFragment(), refFragmentPathDelimiters);
schemas.put(id, new Schema(id, childContent, content));
JsonNode childContent = fragmentResolver.resolve(baseContent, '#' + id.getFragment(), refFragmentPathDelimiters);
schemas.put(id, new Schema(id, childContent, baseSchema));
} else {
schemas.put(id, new Schema(id, content, content));
schemas.put(id, baseSchema);
}
}

Expand Down Expand Up @@ -121,8 +123,10 @@ public Schema create(Schema parent, String path, String refFragmentPathDelimiter
}

if (selfReferenceWithoutParentFile(parent, path) || substringBefore(stringId, "#").isEmpty()) {
schemas.put(id, new Schema(id, fragmentResolver.resolve(parent.getParentContent(), path, refFragmentPathDelimiters), parent.getParentContent()));
return schemas.get(id);
JsonNode parentContent = parent.getParent().getContent();
Schema schema = new Schema(id, fragmentResolver.resolve(parentContent, path, refFragmentPathDelimiters), parent.getParent());
schemas.put(id, schema);
return schema;
}

return create(id, refFragmentPathDelimiters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public JType apply(String nodeName, JsonNode schemaNode, JsonNode parent, JClass
return apply(nameFromRef != null ? nameFromRef : nodeName, schemaNode, parent, generatableType, schema);
}

schema = schema.derive(schemaNode);

JType javaType;
if (schemaNode.has("enum")) {
javaType = ruleFactory.getEnumRule().apply(nodeName, schemaNode, parent, generatableType, schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public void arrayWithUniqueItemsProducesSet() {
propertyNode.set("uniqueItems", BooleanNode.TRUE);
propertyNode.set("items", itemsNode);

JClass propertyType = rule.apply("fooBars", propertyNode, null, jpackage, mock(Schema.class));
Schema schema = mock(Schema.class);
when(schema.derive(any())).thenReturn(schema);

JClass propertyType = rule.apply("fooBars", propertyNode, null, jpackage, schema);

assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(Set.class)));
Expand All @@ -79,6 +82,7 @@ public void arrayWithNonUniqueItemsProducesList() {

Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
when(schema.derive(any())).thenReturn(schema);
when(config.isUseDoubleNumbers()).thenReturn(true);

JClass propertyType = rule.apply("fooBars", propertyNode, null, jpackage, schema);
Expand All @@ -104,6 +108,7 @@ public void arrayOfPrimitivesProducesCollectionOfWrapperTypes() {

Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
when(schema.derive(any())).thenReturn(schema);
when(config.isUsePrimitives()).thenReturn(true);
when(config.isUseDoubleNumbers()).thenReturn(true);

Expand All @@ -130,6 +135,7 @@ public void arrayDefaultsToNonUnique() {

Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/defaultArray"));
when(schema.derive(any())).thenReturn(schema);

JClass propertyType = rule.apply("fooBars", propertyNode, null, jpackage, schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void shouldUpdateJavaDoc() throws JClassAlreadyExistsException {
ObjectMapper mapper = new ObjectMapper();
ArrayNode requiredNode = mapper.createArrayNode().add("fooBar");

rule.apply("Class", requiredNode, null, jclass, new Schema(null, requiredNode, requiredNode));
rule.apply("Class", requiredNode, null, jclass, new Schema(null, requiredNode, null));

JDocComment fooBarJavaDoc = jclass.fields().get("fooBar").javadoc();
JDocComment fooJavaDoc = jclass.fields().get("foo").javadoc();
Expand All @@ -79,7 +79,7 @@ public void shouldUpdateAnnotations() throws JClassAlreadyExistsException {
ObjectMapper mapper = new ObjectMapper();
ArrayNode requiredNode = mapper.createArrayNode().add("foo_bar");

rule.apply("Class", requiredNode, null, jclass, new Schema(null, requiredNode, requiredNode));
rule.apply("Class", requiredNode, null, jclass, new Schema(null, requiredNode, null));

Collection<JAnnotationUse> fooBarAnnotations = jclass.fields().get("fooBar").annotations();
Collection<JAnnotationUse> fooAnnotations = jclass.fields().get("foo").annotations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void enumAsRootIsGeneratedCorrectly() throws JClassAlreadyExistsException

Schema schema = mock(Schema.class);
when(schema.getContent()).thenReturn(schemaContent);
when(schema.derive(any())).thenReturn(schema);
schema.setJavaTypeIfEmpty(jclass);

EnumRule enumRule = mock(EnumRule.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void selfRefWithoutParentFile() throws IOException {
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, null, p, new Schema(null, schema, schema));
new RuleFactory().getSchemaRule().apply("Example", schema, null, p, new Schema(null, schema, null));
}

@Test
Expand Down Expand Up @@ -101,7 +101,7 @@ public void refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile() throw
"}");

JPackage p = codeModel._package("com.example");
new RuleFactory().getSchemaRule().apply("Example", schema, null, p, new Schema(null, schema, schema));
new RuleFactory().getSchemaRule().apply("Example", schema, null, p, new Schema(null, schema, null));
}

}

0 comments on commit 59d48bf

Please sign in to comment.