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 Aug 2, 2023
1 parent 6c8602a commit 311e95c
Show file tree
Hide file tree
Showing 7 changed files with 284 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 definitionsNodePath = getDefinitionsNodePath(schemaNode);
if (null != definitionsNodePath) {
final Iterator<Map.Entry<String, JsonNode>> definitions = schemaNode.at(definitionsNodePath).fields();
while (definitions.hasNext()) {
final Map.Entry<String, JsonNode> definition = definitions.next();

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

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

protected String getDefinitionsNodePath(JsonNode schemaNode) {
final String definitionsNodePath;
if (schemaNode.has("definitions")) {
definitionsNodePath = "/definitions";
} else if (schemaNode.has("$defs")) {
definitionsNodePath = "/$defs";
} else {
definitionsNodePath = null;
}
return definitionsNodePath;
}

private String nameFromRef(String ref) {

if ("#".equals(ref)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* 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 com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JClassContainer;
import com.sun.codemodel.JType;
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
import org.jsonschema2pojo.rules.RuleFactory;
import org.jsonschema2pojo.rules.SchemaRule;
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.jsonschema2pojo.integration.util.CodeGenerationHelper.config;
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"));
}

@Test
public void definitionsFromCustomPathProcessed_when_definitionsNodePathIsOverridden() throws ReflectiveOperationException {
ClassLoader resultsClassLoader = schemaRule.generateAndCompile(
"/schema/definitions/schemaDefinitionsStorageWithCustomSection.json",
"com.example",
config("customRuleFactory", CustomRuleFactory.class.getName()));

Class<?> statusTypeRaw = resultsClassLoader.loadClass("com.example.Status");
assertThat(statusTypeRaw.isEnum(), is(true));
@SuppressWarnings("unchecked")
Class<Enum<?>> statusType = (Class<Enum<?>>) statusTypeRaw;
assertThat(statusType.getEnumConstants()[0].name(), is("ACTIVE"));
assertThat(statusType.getEnumConstants()[1].name(), is("INACTIVE"));

Class<?> userType = resultsClassLoader.loadClass("com.example.User");
assertThat(userType.getDeclaredField("id"), is(notNullValue()));
assertThat(userType.getDeclaredField("id").getType(), is(equalTo(Integer.class)));
assertThat(userType.getDeclaredField("name"), is(notNullValue()));
assertThat(userType.getDeclaredField("name").getType(), is(equalTo(String.class)));

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)));
}

public static class CustomRuleFactory extends RuleFactory {

@Override
public org.jsonschema2pojo.rules.Rule<JClassContainer, JType> getSchemaRule() {
return new SchemaRule(this) {

@Override
protected String getDefinitionsNodePath(JsonNode schemaNode) {
return "/components/schemas";
}

};
}

}

}
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,29 @@
{
"definitions": {
"unexpected": {
"type": "object"
}
},
"components": {
"schemas": {
"user": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
}
},
"status": {
"type": "string",
"enum": [
"active",
"inactive"
]
}
}
}
}
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 311e95c

Please sign in to comment.