Skip to content

Commit

Permalink
Add configuration to add interfaces to all top-level generated classes.
Browse files Browse the repository at this point in the history
This depends on joelittlejohngh-917 in order to know if a rule is applied top-level or sub-level.
Alternatively, if joelittlejohngh-906 is fixed by providing sub-schemas for sub-levels
this could be used to check nesting.
  • Loading branch information
jrehwaldt committed Sep 17, 2018
1 parent 9e1de34 commit 3f4c309
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private boolean includeDynamicBuilders = false;

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

private String dateTimeType;

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

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

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

@Override
public String[] getBaseClassInterfaces() {
return baseClassInterfaces;
}

@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 @@ -188,6 +188,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">baseClassInterfaces</td>
<td valign="top">Interfaces to add to top-level 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 @@ -186,6 +187,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 = { "-in", "--base-class-interface" }, description = "Include given interface on top-level generated classes.")
private List<String> baseClassInterfaces = Collections.emptyList();

@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 @@ -460,6 +464,11 @@ public boolean isIncludeDynamicBuilders() {
return includeDynamicBuilders;
}

@Override
public List<String> getBaseClassInterfaces() {
return baseClassInterfaces;
}

@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 @@ -349,6 +351,14 @@ public boolean isIncludeDynamicBuilders() {
return false;
}

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

@Override
public String getDateTimeType() {
return null;
Expand Down Expand Up @@ -427,4 +437,4 @@ public Language getTargetLanguage() {
return Language.JAVA;
}

}
}
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 @@ -422,6 +423,13 @@ public interface GenerationConfig {
*/
boolean isIncludeDynamicBuilders();

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

/**
* Gets the `dateTimeType` configuration option.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContai

schema.setJavaTypeIfEmpty(_enum);

if (parent == null) {
for (String _interface : ruleFactory.getGenerationConfig().getBaseClassInterfaces()) {
_enum._implements(resolveType(_enum._package(), _interface));
}
}
if (node.has("javaInterfaces")) {
addInterfaces(_enum, node.get("javaInterfaces"));
}
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().getBaseClassInterfaces()) {
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 All @@ -44,6 +45,7 @@ public class EnumRuleTest {
private Annotator annotator = mock(Annotator.class);
private RuleFactory ruleFactory = mock(RuleFactory.class);
private TypeRule typeRule = mock(TypeRule.class);
private GenerationConfig config = mock(GenerationConfig.class);

private EnumRule rule = new EnumRule(ruleFactory);

Expand All @@ -52,6 +54,7 @@ public void wireUpConfig() {
when(ruleFactory.getNameHelper()).thenReturn(nameHelper);
when(ruleFactory.getAnnotator()).thenReturn(annotator);
when(ruleFactory.getTypeRule()).thenReturn(typeRule);
when(ruleFactory.getGenerationConfig()).thenReturn(config);
}

@Test
Expand All @@ -61,6 +64,7 @@ public void applyGeneratesUniqueEnumNamesForMultipleEnumNodesWithSameName() {
when(nameHelper.getFieldName(anyString(), Matchers.any(JsonNode.class))).thenAnswer(firstArgAnswer);
when(nameHelper.replaceIllegalCharacters(anyString())).thenAnswer(firstArgAnswer);
when(nameHelper.normalizeName(anyString())).thenAnswer(firstArgAnswer);
when(config.getBaseClassInterfaces()).thenReturn(new String[0]);

JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class JsonSchemaExtension implements GenerationConfig {
boolean includeDynamicGetters
boolean includeDynamicSetters
boolean includeDynamicBuilders
String[] baseClassInterfaces
boolean includeConstructors
boolean constructorsRequiredPropertiesOnly
boolean includeHashcodeAndEquals
Expand Down Expand Up @@ -137,6 +138,7 @@ public class JsonSchemaExtension implements GenerationConfig {
includeDynamicGetters = false
includeDynamicSetters = false
includeDynamicBuilders = false
baseClassInterfaces = [] as String[]
formatDates = false
formatTimes = false
formatDateTimes = false
Expand Down Expand Up @@ -239,6 +241,7 @@ public class JsonSchemaExtension implements GenerationConfig {
|includeDynamicGetters = ${includeDynamicGetters}
|includeDynamicSetters = ${includeDynamicSetters}
|includeDynamicBuilders = ${includeDynamicBuilders}
|baseClassInterfaces = ${baseClassInterfaces}
|formatDates = ${formatDates}
|formatTimes = ${formatTimes}
|formatDateTimes = ${formatDateTimes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,14 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
*/
private boolean includeDynamicBuilders = false;

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

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

@Override
public String[] getBaseClassInterfaces() {
return baseClassInterfaces;
}

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

0 comments on commit 3f4c309

Please sign in to comment.