Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add includeNoArgsConstructor option #1622

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

boolean includeCopyConstructor = false;

boolean includeNoArgsConstructor = true;

private boolean includeAdditionalProperties = true;

private boolean includeGetters = true;
Expand Down Expand Up @@ -192,7 +194,7 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {
private SourceSortOrder sourceSortOrder = SourceSortOrder.OS;

private Map<String, String> formatTypeMapping = new HashMap<>();

private boolean includeGeneratedAnnotation = true;

private boolean useJakartaValidation = false;
Expand Down Expand Up @@ -342,6 +344,20 @@ public void setIncludeCopyConstructor(boolean includeCopyConstructor) {
this.includeCopyConstructor = includeCopyConstructor;
}

/**
* Sets the 'includeNoArgsConstructor' configuration option. This property works in collaboration with the {@link
* #isIncludeConstructors()} configuration option, and will have no effect if {@link #isIncludeConstructors()}
* is not set to true. If {@link #isIncludeConstructors()} is set to true then this configuration determines
* whether the resulting object should include the no-args constructor. This option is compatible with any
* of the other constructor related options, but will be ignored if no other constructor is to be generated -
* this is because java will always infer the no-args constructor if no other constructors are present.
*
* @param includeNoArgsConstructor controls whether the resulting class will include the no-args constructor
*/
public void setIncludeNoArgsConstructor(boolean includeNoArgsConstructor) {
this.includeNoArgsConstructor = includeNoArgsConstructor;
}

/**
* Sets the 'generateBuilders' property of this class.
*
Expand Down Expand Up @@ -1209,6 +1225,11 @@ public boolean isIncludeCopyConstructor() {
return includeCopyConstructor;
}

@Override
public boolean isIncludeNoArgsConstructor() {
return includeNoArgsConstructor;
}

@Override
public boolean isIncludeAdditionalProperties() {
return includeAdditionalProperties;
Expand Down Expand Up @@ -1313,7 +1334,7 @@ public String getRefFragmentPathDelimiters() {
public SourceSortOrder getSourceSortOrder() {
return sourceSortOrder;
}

@Override
public Map<String, String> getFormatTypeMapping() {
return formatTypeMapping;
Expand Down
13 changes: 13 additions & 0 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,19 @@ <h3>Parameters</h3>
</td>
<td align="center" valign="top">No (default <code>false</code>)</td>
</tr>
<tr>
<td valign="top">includeNoArgsConstructor</td>
<td valign="top">The 'includeNoArgsConstructor' configuration option
works in collaboration with the {@link #isIncludeConstructors()} configuration option and is
incompatible with {@link #isConstructorsRequiredPropertiesOnly()}, and will have no effect if
{@link #isIncludeConstructors()} is not set to true. If {@link #isIncludeConstructors()} is
set to true then this configuration determines whether the resulting object should include the
no-args constructor. This option is compatible with any of the other constructor related options,
but will be ignored if no other constructor is to be generated - this is because java will
always infer the no-args constructor if no other constructors are present.
</td>
<td align="center" valign="top">No (default <code>true</code>)</td>
</tr>
<tr>
<td valign="top">includeHashcodeAndEquals</td>
<td valign="top">Whether to use include <code>hashCode</code> and <code>equals</code> methods in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "--constructors-include-copy-constructor" }, description = "Generate constructors with a copy oriented parameter")
private boolean includeCopyConstructor = false;

@Parameter(names = { "--constructors-include-no-args-constructor" }, description = "Generate constructor with no arguments")
private boolean includeNoArgsConstructor = false;

@Parameter(names = { "-P", "--use-primitives" }, description = "Use primitives instead of wrapper types for bean properties")
private boolean usePrimitives = false;

Expand Down Expand Up @@ -503,6 +506,9 @@ public boolean isPrintLogLevels() {
@Override
public boolean isIncludeCopyConstructor() { return includeCopyConstructor; }

@Override
public boolean isIncludeNoArgsConstructor() { return includeNoArgsConstructor; }

@Override
public boolean isIncludeAdditionalProperties() {
return isIncludeAdditionalProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ public boolean isConstructorsRequiredPropertiesOnly() {
@Override
public boolean isIncludeCopyConstructor() { return false; }

/**
* @return <code>true</code>
*/
@Override
public boolean isIncludeNoArgsConstructor() { return true; }

/**
* @return <code>true</code>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@ public interface GenerationConfig {
*/
boolean isIncludeCopyConstructor();

/**
* Gets the 'includeNoArgsConstructor' configuration option. This property works in collaboration with the {@link
* #isIncludeConstructors()} configuration option, and will have no effect if {@link #isIncludeConstructors()}
* is not set to true. If {@link #isIncludeConstructors()} is set to true then this configuration determines
* whether the resulting object should include the default constructor. This option is compatible with any
* of the other constructor related options, but will be ignored if no other constructor is to be generated -
* this is because java will always infer the default constructor if no other constructors are present.
*
* @return whether the resulting object should not include the default a constructor.
*/
boolean isIncludeNoArgsConstructor();

/**
* Gets the 'includeAdditionalProperties' configuration option.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ private void handleLegacyConfiguration(JsonNode node, JDefinedClass instanceClas

// Only generate the constructors if there are actually properties to put in them
if (!requiredClassProperties.isEmpty() || !requiredCombinedSuperProperties.isEmpty()) {
final GenerationConfig generationConfig = ruleFactory.getGenerationConfig();

// Generate the no arguments constructor - we'll need this even if there is a property
// constructor available, because it is used by the serialization and deserialization
generateNoArgsConstructor(instanceClass);
// If there are no properties to put on the constructor than we don't need
// the no-args constructor as java infers it, so we can make this computation
// only if the required properties constructor is being generated
if (generationConfig.isIncludeNoArgsConstructor()) {
generateNoArgsConstructor(instanceClass);
}

// Generate the actual constructor taking in only the required properties
addFieldsConstructor(instanceClass, requiredClassProperties, requiredCombinedSuperProperties);
Expand Down Expand Up @@ -125,9 +129,12 @@ private void handleMultiChoiceConstructorConfiguration(JsonNode node, JDefinedCl

// Only generate the constructors if there are actually properties to put in them
if (requiresConstructors) {
// Generate the no arguments constructor - we'll need this even if there is a property
// constructor available, because it is used by the serialization and deserialization
generateNoArgsConstructor(instanceClass);
// If there are no other constructors to generate then the no-args constructor is
// not needed as java infers it, so we can make this computation only if the required
// properties constructor is being generated
if (generationConfig.isIncludeNoArgsConstructor()) {
generateNoArgsConstructor(instanceClass);
}

if (includeCopyConstructor) {
addCopyConstructor(instanceClass, classProperties, combinedSuperProperties);
Expand Down
3 changes: 3 additions & 0 deletions jsonschema2pojo-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ jsonSchema2Pojo {
// This property is irrelevant if constructorsRequiredPropertiesOnly = true
includeCopyConstructor = false

// Whether to *add* the no-args constructor, alongside other constructors.
includeNoArgsConstructor = false

// Whether to make the generated types Parcelable for Android
parcelable = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class JsonSchemaExtension implements GenerationConfig {
boolean includeRequiredPropertiesConstructor;
boolean includeAllPropertiesConstructor;
boolean includeCopyConstructor;
boolean includeNoArgsConstructor;
boolean includeHashcodeAndEquals
boolean includeJsr303Annotations
boolean includeJsr305Annotations
Expand Down Expand Up @@ -117,6 +118,7 @@ public class JsonSchemaExtension implements GenerationConfig {
constructorsRequiredPropertiesOnly = false
includeRequiredPropertiesConstructor = false
includeAllPropertiesConstructor = true
includeNoArgsConstructor = true
includeToString = true
toStringExcludes = [] as String[]
annotationStyle = AnnotationStyle.JACKSON
Expand Down Expand Up @@ -247,6 +249,7 @@ public class JsonSchemaExtension implements GenerationConfig {
|includeRequiredPropertiesConstructor = ${includeRequiredPropertiesConstructor}
|includeAllPropertiesConstructor = ${includeAllPropertiesConstructor}
|includeCopyConstructor = ${includeCopyConstructor}
|includeNoArgsConstructor = ${includeNoArgsConstructor}
|includeToString = ${includeToString}
|toStringExcludes = ${Arrays.toString(toStringExcludes)}
|annotationStyle = ${annotationStyle.toString().toLowerCase()}
Expand Down
Loading