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

Wrong default value for BigDecimal #535

Closed
JureVI opened this issue Mar 21, 2016 · 2 comments
Closed

Wrong default value for BigDecimal #535

JureVI opened this issue Mar 21, 2016 · 2 comments
Milestone

Comments

@JureVI
Copy link

JureVI commented Mar 21, 2016

It seems that if useBigDecimals is set to true and a numeric field has default property set, the generated Java file has the field initialized with Double value instead of BigDecimal.

Json schema:

...
"amount": {
  "type": "number",
  "default": 0,
  ...
}
...

Java file:

...
@SerializedName("amount")
@Expose
private BigDecimal amount = 0.0D;
...

This is my gradle configuration:

jsonSchema2Pojo {
    generateBuilders = false
    usePrimitives = false
    source = ...
    targetDirectory = ...
    targetPackage = ...
    propertyWordDelimiters = [] as char[]
    useLongIntegers = false
    useBigDecimals = true
    includeHashcodeAndEquals = true
    includeToString = true
    includeJsr303Annotations = false
    sourceType = 'jsonschema'
    removeOldOutput = true
    outputEncoding = 'UTF-8'
    useJodaDates = false
    useCommonsLang3 = false
    initializeCollections = false
}
@joelittlejohn joelittlejohn added this to the 0.4.23 milestone Mar 21, 2016
@thachhoang
Copy link
Collaborator

This could be fixed in DefaultRule.getDefaultValue. Should we parse BigDecimal like this?

JInvocation newBigDecimal = JExpr._new(fieldType);
newBigDecimal.arg(JExpr.lit(Double.parseDouble(node.asText())));
return newBigDecimal;

@joelittlejohn
Copy link
Owner

I think it might be better to use the String constructor of BigDecimal, since it avoids a multitude of numeric representation issues and will create a BigDecimal that matches number given in the JSON Schema exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants