-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate objects from subschemas defined in 'definitions'/'$defs' sec…
…tion Closes #1123
- Loading branch information
Showing
7 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...schema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/SchemaIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
}; | ||
} | ||
|
||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...integration-tests/src/test/resources/schema/definitions/referencedDefinitionsStorage.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ojo-integration-tests/src/test/resources/schema/definitions/schemaDefinitionsStorage.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ests/src/test/resources/schema/definitions/schemaDefinitionsStorageWithCustomSection.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...chema2pojo-integration-tests/src/test/resources/schema/definitions/schemaDefsStorage.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...integration-tests/src/test/resources/schema/definitions/schemaWithDefinitionsAndDefs.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |