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

Vivek M :

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"
        }
      }
    },
Vivek M :

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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=77304&siteId=1