joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Generate with type BigDecimal for properties with `minimum` and `maximum` #1645

Closed frjonsen closed 1 week ago

frjonsen commented 2 weeks ago

We encountered the same issue as have been mentioned elsewhere, where @DecimalMin and @DecimalMax annotations are not added when using "type": "number".

https://github.com/joelittlejohn/jsonschema2pojo/issues/1583 https://github.com/joelittlejohn/jsonschema2pojo/issues/1418

Reading through the threads above, my take is that the solution is to use BigDecimal instead. Exactly how to do this wasn't quite specified, but I got it to work using, for example:

"state_of_charge": {
      "type": "number",
      "description": "State of charge as ratio (from 0 to 1)",
      "javaType": "java.math.BigDecimal",
      "minimum": 0,
      "maximum": 1
}

While this is kind of annoying, I understand that it's a limitation of javax, and not of this library. However, it's specified in several places in the documentation of this library that using minimum and maximum for numbers is supported, but the limitation of only being supported for BigDecimal isn't mentioned. My suggestion would be to either generate these fields as BigDecimal by default (though this might be confusing as the type changes if the constraints are removed), or at least update the documentation to mention how this workaround works. When doing this I had to puzzle it together from reading various other issues.

unkish commented 1 week ago

Hi

Thank you for reporting. Added clarification to wiki.