-
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
6 changed files
with
210 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
92 changes: 92 additions & 0 deletions
92
...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,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))); | ||
} | ||
|
||
} |
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" | ||
} | ||
} | ||
} | ||
} | ||
} |
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" | ||
} | ||
} | ||
} |