Skip to content

Commit

Permalink
Generate objects from subschemas defined in 'definitions'/'$defs' sec…
Browse files Browse the repository at this point in the history
…tion

Closes #1123
  • Loading branch information
unkish committed May 22, 2023
1 parent 98c85ce commit aa2547c
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.Map;

import org.jsonschema2pojo.Jsonschema2Pojo;
import org.jsonschema2pojo.Schema;
Expand Down Expand Up @@ -84,9 +86,43 @@ public JType apply(String nodeName, JsonNode schemaNode, JsonNode parent, JClass
}
schema.setJavaTypeIfEmpty(javaType);

processDefinitions(schemaNode, generatableType, schema);

return javaType;
}

private void processDefinitions(JsonNode schemaNode, JClassContainer generatableType, Schema parent) {
final String definitionsNodeName = getDefinitionsNodeName(schemaNode);
if (null != definitionsNodeName) {
Iterator<Map.Entry<String, JsonNode>> definitions = schemaNode.get(definitionsNodeName).fields();
while (definitions.hasNext()) {
Map.Entry<String, JsonNode> definition = definitions.next();

final Schema schema = ruleFactory.getSchemaStore().create(
parent,
"#/" + definitionsNodeName + "/" + definition.getKey(),
ruleFactory.getGenerationConfig().getRefFragmentPathDelimiters());
if (schema.isGenerated()) {
continue;
}

apply(definition.getKey(), definition.getValue(), schemaNode, generatableType, schema);
}
}
}

private String getDefinitionsNodeName(JsonNode schemaNode) {
final String definitionsNodeName;
if (schemaNode.has("definitions")) {
definitionsNodeName = "definitions";
} else if (schemaNode.has("$defs")) {
definitionsNodeName = "$defs";
} else {
definitionsNodeName = null;
}
return definitionsNodeName;
}

private String nameFromRef(String ref) {

if ("#".equals(ref)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright © 2010-2020 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.integration;

import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
import org.junit.Rule;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThrows;

public class SchemaIT {

@Rule
public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule();

@Test
public void typesDefinedInDefinitionsAreGenerated() throws ReflectiveOperationException {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/definitions/schemaDefinitionsStorage.json", "com.example");

Class<?> schemaDefinitionsStorage = resultsClassLoader.loadClass("com.example.SchemaDefinitionsStorage");
Class<?> referencedDefinitionsStorageType = resultsClassLoader.loadClass("com.example.ReferencedDefinitionsStorage");
assertThat(referencedDefinitionsStorageType.getDeclaredField("name"), is(notNullValue()));

assertThat(schemaDefinitionsStorage, is(notNullValue()));
assertThat(referencedDefinitionsStorageType, is(notNullValue()));
assertInlinePropertyTypes(resultsClassLoader);
}

@Test
public void typesDefinedInDefsAreGenerated() throws ReflectiveOperationException {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/definitions/schemaDefsStorage.json", "com.example");

Class<?> schemaDefsStorageType = resultsClassLoader.loadClass("com.example.SchemaDefsStorage");
Class<?> referencedDefinitionsStorageType = resultsClassLoader.loadClass("com.example.ReferencedDefinitionsStorage");
assertThat(referencedDefinitionsStorageType.getDeclaredField("name"), is(notNullValue()));

assertThat(schemaDefsStorageType, is(notNullValue()));
assertThat(referencedDefinitionsStorageType, is(notNullValue()));
assertInlinePropertyTypes(resultsClassLoader);
}

@Test
public void defsNotProcessed_when_schemaContainsDefinitionsAndDefs() throws ReflectiveOperationException {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/definitions/schemaWithDefinitionsAndDefs.json", "com.example");

Class<?> schemaCollectionType = resultsClassLoader.loadClass("com.example.SchemaWithDefinitionsAndDefs");
Class<?> inlineType = resultsClassLoader.loadClass("com.example.Inline");

assertThat(schemaCollectionType, is(notNullValue()));
assertThat(inlineType, is(notNullValue()));
assertThat(inlineType.getDeclaredField("inlineProperty"), is(notNullValue()));

assertThrows(ClassNotFoundException.class, () -> resultsClassLoader.loadClass("com.example.Unexpected"));
}

private void assertInlinePropertyTypes(ClassLoader resultsClassLoader) throws ReflectiveOperationException {
Class<?> referencedInlineType = resultsClassLoader.loadClass("com.example.Inline");
Class<?> inlineType = resultsClassLoader.loadClass("com.example.Inline__1");

assertThat(inlineType, is(not(equalTo(referencedInlineType))));

assertThat(referencedInlineType.getDeclaredField("inlineProperty"), is(notNullValue()));
assertThat(referencedInlineType.getDeclaredField("inlineProperty").getType(), is(equalTo(Boolean.class)));

assertThat(inlineType.getDeclaredField("inlineProperty"), is(notNullValue()));
assertThat(inlineType.getDeclaredField("inlineProperty").getType(), is(equalTo(String.class)));

Class<?> selfReferenceType = resultsClassLoader.loadClass("com.example.SelfReference");
assertThat(selfReferenceType.getDeclaredField("selfRefProperty"), is(notNullValue()));
assertThat(selfReferenceType.getDeclaredField("selfRefProperty").getType(), is(equalTo(inlineType)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"definitions": {
"inline": {
"type": "object",
"properties": {
"inlineProperty": {
"type": "boolean"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "object",
"definitions": {
"reference": {
"$ref": "referencedDefinitionsStorage.json"
},
"self-reference": {
"properties": {
"selfRefProperty": {
"$ref": "#/definitions/inline"
}
}
},
"inline": {
"type": "object",
"properties": {
"inlineProperty": {
"type": "string"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "object",
"$defs": {
"reference": {
"$ref": "referencedDefinitionsStorage.json"
},
"self-reference": {
"properties": {
"selfRefProperty": {
"$ref": "#/$defs/inline"
}
}
},
"inline": {
"type": "object",
"properties": {
"inlineProperty": {
"type": "string"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "object",
"definitions": {
"inline": {
"type": "object",
"properties": {
"inlineProperty": {
"type": "string"
}
}
}
},
"$defs": {
"unexpected": {
"type": "object"
}
}
}

0 comments on commit aa2547c

Please sign in to comment.