Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to treat a "double" field as "BigDecimal" while generating java code from a swagger?

I am generating pojos from a swagger. The swagger is provided from a third party and can't be changed. I want the "double" fields to be generated as "BigDecimal". How do I customise my code generator in order to achieve this?

        <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>generateSquiree</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/schema/sample.swagger.json</inputSpec>
                        <configOptions>
                            <modelPackage>${basepackage}.model</modelPackage>
                            <apiPackage>${basepackage}.api</apiPackage>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <language>spring</language>
                <configOptions>
                    <serializableModel>true</serializableModel>
                    <java8>false</java8>
                    <javaVersion>${java.version}</javaVersion>
                    <jdk8>true</jdk8>
                    <dateLibrary>joda</dateLibrary>
                    <useTags>true</useTags>
                    <sourceFolder>src/main/java</sourceFolder>
                    <interfaceOnly>true</interfaceOnly>
                </configOptions>
            </configuration>
        </plugin>

Below is a snippet from the swagger which needs to be generated as "BigDecimal"

    "Quantity": {
      "description": "Represent a quantity",
      "required": [
        "Amount"
      ],
      "type": "object",
      "properties": {
        "Amount": {
          "format": "double",
          "description": "Amount",
          "type": "number"
        }
      }
    },
like image 667
Vivek Mecharla Avatar asked Apr 12 '19 15:04

Vivek Mecharla


People also ask

How does swagger define BigDecimal?

BigDecimal is a Java thing. In a Swagger specification it would simply translate to type 'number'. You received this message because you are subscribed to the Google Groups "Swagger" group. To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...

Why use BigDecimal instead of double in Java?

A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001 ) could result in the 0.001 being dropped altogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen.

Can BigDecimal be double?

The doubleValue() method of Java BigDecimal class is used to convert the BigDecimal value into a double type. If BigDecimal has very big value represented as a double, it will be converted to Double. NEGATIVE_INFINITY or Double.

How much slower is BigDecimal than double?

But now I have some code where performance is an issue, and BigDecimal is more than 1000 times (!) slower than double primitives. The calculations are very simple: what the system does is calculating a = (1/b) * c many many times (where a , b and c are fixed-point values).


1 Answers

Found an answer to my question at

https://github.com/swagger-api/swagger-codegen/issues/5587#issuecomment-368805748

Posting the solution below in case anyone has the same question

        <configuration>
            ....
            <typeMappings>
                <typeMapping>Double=java.math.BigDecimal</typeMapping>
            </typeMappings>
        </configuration>
like image 101
Vivek Mecharla Avatar answered Sep 28 '22 12:09

Vivek Mecharla