Skip to content

Commit

Permalink
Add configuration to add interfaces to all generated classes.
Browse files Browse the repository at this point in the history
No interfaces are added to enums.

This depends on gh-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
jrehwaldt committed Sep 18, 2018
1 parent 02c6d4b commit c52d393
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private boolean includeDynamicBuilders = false;

private String[] additionalInterfaces = new String[0];

private String dateTimeType;

private String timeType;
Expand Down Expand Up @@ -776,6 +778,16 @@ public void setIncludeDynamicBuilders(boolean includeDynamicBuilders) {
this.includeDynamicBuilders = includeDynamicBuilders;
}

/**
* Sets the 'additionalInterfaces' property of this class
*
* @param additionalInterfaces
* Interfaces added to generated classes.
*/
public void setAdditionalInterfaces(String[] additionalInterfaces) {
this.additionalInterfaces = additionalInterfaces;
}

/**
* Sets the 'formatDateTimes' property of this class
*
Expand Down Expand Up @@ -1113,6 +1125,11 @@ public boolean isIncludeDynamicBuilders() {
return includeDynamicBuilders;
}

@Override
public String[] getAdditionalInterfaces() {
return additionalInterfaces;
}

@Override
public String getDateTimeType() {
return dateTimeType;
Expand Down
5 changes: 5 additions & 0 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ <h3>Parameters</h3>
<td valign="top">Whether to include dynamic builders or to omit these methods.</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">additionalInterfaces</td>
<td valign="top">Interfaces added generated classes.</td>
<td align="center" valign="top">No (default <code>""</code> (none))</td>
</tr>
<tr>
<td valign="top">includeConstructors</td>
<td valign="top">Whether to generate constructors for generated Java types</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -189,6 +190,9 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "-idb", "--include-dynamic-builders" }, description = "Include dynamic builder support on generated types.")
private boolean includeDynamicBuilders = false;

@Parameter(names = { "-ai", "--additional-interface" }, description = "Add given interfaces to generated classes.")
private String[] additionalInterfaces = new String[0];

@Parameter(names = { "-fd", "--format-dates" }, description = "Whether the fields of type `date` are formatted during serialization with a default pattern of `yyyy-MM-dd`")
private boolean formatDates = false;

Expand Down Expand Up @@ -468,6 +472,11 @@ public boolean isIncludeDynamicBuilders() {
return includeDynamicBuilders;
}

@Override
public String[] getAdditionalInterfaces() {
return additionalInterfaces;
}

@Override
public String getDateTimeType() {
return dateTimeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.jsonschema2pojo.rules.RuleFactory;

Expand Down Expand Up @@ -357,6 +359,14 @@ public boolean isIncludeDynamicBuilders() {
return false;
}

/**
* @return <code>{}</code>
*/
@Override
public String[] getAdditionalInterfaces() {
return new String[] {};
}

@Override
public String getDateTimeType() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.FileFilter;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.jsonschema2pojo.rules.RuleFactory;

Expand Down Expand Up @@ -427,6 +428,13 @@ public interface GenerationConfig {
*/
boolean isIncludeDynamicBuilders();

/**
* Gets the `additionalInterfaces` configuration option.
*
* @return a list of interfaces added to each generated class
*/
String[] getAdditionalInterfaces();

/**
* Gets the `dateTimeType` configuration option.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
for (String _interface : ruleFactory.getGenerationConfig().getAdditionalInterfaces()) {
jclass._implements(resolveType(jclass._package(), _interface));
}
}
if (node.has("javaInterfaces")) {
addInterfaces(jclass, node.get("javaInterfaces"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.mockito.Mockito.*;

import org.jsonschema2pojo.Annotator;
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.util.NameHelper;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class JsonSchemaExtension implements GenerationConfig {
boolean includeDynamicGetters
boolean includeDynamicSetters
boolean includeDynamicBuilders
String[] additionalInterfaces
boolean includeConstructors
boolean constructorsRequiredPropertiesOnly
boolean includeHashcodeAndEquals
Expand Down Expand Up @@ -139,6 +140,7 @@ public class JsonSchemaExtension implements GenerationConfig {
includeDynamicGetters = false
includeDynamicSetters = false
includeDynamicBuilders = false
additionalInterfaces = [] as String[]
formatDates = false
formatTimes = false
formatDateTimes = false
Expand Down Expand Up @@ -246,6 +248,7 @@ public class JsonSchemaExtension implements GenerationConfig {
|includeDynamicGetters = ${includeDynamicGetters}
|includeDynamicSetters = ${includeDynamicSetters}
|includeDynamicBuilders = ${includeDynamicBuilders}
|additionalInterfaces = ${additionalInterfaces}
|formatDates = ${formatDates}
|formatTimes = ${formatTimes}
|formatDateTimes = ${formatDateTimes}
Expand Down
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,14 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
*/
private boolean includeDynamicBuilders = false;

/**
* Interfaces to add to all generated classes.
*
* @parameter property="jsonschema2pojo.additionalInterfaces"
* default-value=""
*/
private String[] additionalInterfaces = new String[0];

/**
* The project being built.
*
Expand Down Expand Up @@ -1076,6 +1084,11 @@ public boolean isIncludeDynamicBuilders() {
return includeDynamicBuilders;
}

@Override
public String[] getAdditionalInterfaces() {
return additionalInterfaces;
}

@Override
public String getDateTimeType() {
return dateTimeType;
Expand Down

0 comments on commit c52d393

Please sign in to comment.