forked from joelittlejohn/jsonschema2pojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration to add interfaces to all generated classes.
No interfaces are added to enums. This depends on joelittlejohngh-917 in order to know if a rule is applied top-level or sub-level. Contains integration tests. Introduces new config option `additionalInterfaces`.
- Loading branch information
Showing
10 changed files
with
154 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
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
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
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
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
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 |
---|---|---|
|
@@ -122,6 +122,11 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JPackage _pa | |
|
||
ruleFactory.getPropertiesRule().apply(nodeName, node.get("properties"), node, jclass, schema); | ||
|
||
if (parent == null) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jrehwaldt
Author
Owner
|
||
for (String _interface : ruleFactory.getGenerationConfig().getAdditionalInterfaces()) { | ||
jclass._implements(resolveType(jclass._package(), _interface)); | ||
} | ||
} | ||
if (node.has("javaInterfaces")) { | ||
addInterfaces(jclass, node.get("javaInterfaces")); | ||
} | ||
|
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
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
83 changes: 83 additions & 0 deletions
83
...on-tests/src/test/java/org/jsonschema2pojo/integration/config/AdditionalInterfacesIT.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,83 @@ | ||
/** | ||
* Copyright © 2010-2017 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.config; | ||
|
||
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Method; | ||
|
||
import static org.hamcrest.Matchers.arrayWithSize; | ||
import static org.hamcrest.Matchers.emptyArray; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.Matchers.typeCompatibleWith; | ||
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class AdditionalInterfacesIT { | ||
|
||
|
||
@Rule public Jsonschema2PojoRule schemaRule = new Jsonschema2PojoRule(); | ||
|
||
@Test | ||
public void noInterfacesAddedByDefault() throws ClassNotFoundException { | ||
|
||
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example"); | ||
|
||
Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties"); | ||
|
||
assertThat(generatedType.getInterfaces(), is(emptyArray())); | ||
} | ||
|
||
@Test | ||
public void interfacesAddedToObject() throws ClassNotFoundException { | ||
|
||
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", | ||
config("additionalInterfaces", new String[] { Serializable.class.getName() })); | ||
|
||
Class<?> generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties"); | ||
|
||
assertThat(generatedType.getInterfaces(), arrayWithSize(1)); | ||
assertThat(generatedType, typeCompatibleWith(Serializable.class)); | ||
} | ||
|
||
@Test | ||
public void interfacesNotAddedToEnum() throws ClassNotFoundException { | ||
|
||
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/enumWithEmptyString.json", "com.example", | ||
config("additionalInterfaces", new String[] { Serializable.class.getName() })); | ||
|
||
Class<?> generatedType = resultsClassLoader.loadClass("com.example.EnumWithEmptyString"); | ||
|
||
assertThat(generatedType.getInterfaces(), is(emptyArray())); | ||
} | ||
|
||
@Test | ||
public void interfacesAreNotAddedToObjectInSubHierarchy() throws ClassNotFoundException, NoSuchMethodException { | ||
|
||
ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/type/types.json", "com.example", | ||
config("additionalInterfaces", new String[] { Serializable.class.getName() })); | ||
|
||
Class<?> generatedType = resultsClassLoader.loadClass("com.example.Types"); | ||
Method objectPropertyGetter = generatedType.getMethod("getObjectProperty"); | ||
|
||
assertThat(objectPropertyGetter.getReturnType(), not(typeCompatibleWith(Serializable.class))); | ||
} | ||
} |
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
I think this check should be removed. I don't understand what the value is in adding these interfaces only to the first type generated.