Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Add option for using BigDecimal for number type (#42)
Browse files Browse the repository at this point in the history
We are using generated clients to transfer monetary data for fields that
are declared as type:number. Before this commit Double would be
generated for number types which was not adequate for transfering
monetary data.  This commit exposes the option provided by
jsonschema2pojo to generate BigDecimals for numbers.

NOTE: raml-spec does not define arbitrary precision numbers but only
provides the option to select from fixed precision formats. Despite this
we think that using BigDecimals on clients is reasonable to represent &
transfer such information. Their use is supported by various tools such
as serializers/deserializers eg jackson and code generators for other
specification eg openapi-generator-maven-plugin.
  • Loading branch information
gaganis authored and machaval committed Jul 12, 2019
1 parent 6c2e5db commit 2e5f2bb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class CodeGenConfig {

private boolean useJava8Optional = false;

private boolean useBigDecimals = false;

private String targetVersion = "1.6";

public CodeGenConfig() {
Expand Down Expand Up @@ -41,6 +43,15 @@ public CodeGenConfig setUseJava8Dates(boolean useJava8Dates) {
return this;
}

public boolean getUseBigDecimals() {
return useBigDecimals;
}

public CodeGenConfig setUseBigDecimals(boolean useBigDecimals) {
this.useBigDecimals = useBigDecimals;
return this;
}

public String getTargetVersion() {
return targetVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,11 @@ public String getTimeType() {
return codeGenConfig.getUseJava8Dates() ? "java.time.LocalTime" : null;
}

@Override
public boolean isUseBigDecimals() {
return codeGenConfig.getUseBigDecimals();
}

@Override
public String getTargetVersion() {
return codeGenConfig.getTargetVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private void runGenerator(String projectName, OutputVersion outputVersion) throw
codeGenConfig.setUseJava8Dates(Boolean.parseBoolean(properties.getProperty("java8Dates", "false")));
codeGenConfig.setUseJava8Optional(Boolean.parseBoolean(properties.getProperty("optionals", "false")));
codeGenConfig.setIncludeAdditionalProperties(Boolean.parseBoolean(properties.getProperty("additionalProperties", "false")));
codeGenConfig.setUseBigDecimals(Boolean.parseBoolean(properties.getProperty("useBigDecimals", "false")));
}
new RamlJavaClientGenerator(projectName, actualTarget, outputVersion, codeGenConfig).generate(resource);
assert resource != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class RamlJavaClientGeneratorMojo extends AbstractMojo {
@Parameter(defaultValue = "true")
private Boolean includeAdditionalProperties;

@Parameter(defaultValue = "false")
private Boolean useBigDecimals;

@Parameter(defaultValue = "false")
private Boolean useOptionalForGetters;

Expand Down Expand Up @@ -75,7 +78,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
codeGenConfig
.setUseJava8Dates(useJava8Dates)
.setIncludeAdditionalProperties(includeAdditionalProperties)
.setUseJava8Optional(useOptionalForGetters);
.setUseJava8Optional(useOptionalForGetters)
.setUseBigDecimals(useBigDecimals);

final RamlJavaClientGenerator ramlJavaClientGenerator = new RamlJavaClientGenerator(basePackage, new File(outputDir), outputVersion, codeGenConfig);
ramlJavaClientGenerator.generate(ramlUrl);
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ There is also a maven plugin that allows you to generate the client code during
<includeAdditionalProperties>true</includeAdditionalProperties>
<!--False by default -->
<useOptionalForGetters>false</useOptionalForGetters>
<!--False by default -->
<useBigDecimals>false</useBigDecimals>
</configuration>
</execution>
</executions>
Expand Down

0 comments on commit 2e5f2bb

Please sign in to comment.