13

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 Mecharla
  • 491
  • 1
  • 4
  • 10
  • Can you share an example of your swagger.json – Helder Sepulveda Apr 14 '19 at 13:27
  • Also what codegen tool are you using? – Helder Sepulveda Apr 14 '19 at 13:35
  • I am using swagger-codegen-maven-plugin. I have added more details in the description of the question. I can't share the complete swagger.json but I've added a snippet. – Vivek Mecharla Apr 15 '19 at 08:40
  • Your swagger.json looks good, this could very well be a bug, I would post this question directly on the project: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin – Helder Sepulveda Apr 15 '19 at 13:26
  • This is not a bug. The field is generated as double because the swagger contains "format": "double", My aim is to generate the field "Amount" as "BigDecimal" even though the format is specified as double. Is there a way to customize this? – Vivek Mecharla Apr 15 '19 at 14:30
  • I thought you had templates on your config... Maybe I mixed questions... You should be able to do that with a custom template: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin#custom-generator – Helder Sepulveda Apr 16 '19 at 00:33
  • Wait a minute... I just checked the history and you did have `templateDirectory` in your configuration but you remove it... – Helder Sepulveda Apr 16 '19 at 00:35
  • Yeah..sorry for the confusion. The initial code was created by my colleague who had copy pasted everything from an existing project. I just removed all the configuration that were unnecessary for my project. I also updated the code here to make the viewers understand my question easily. – Vivek Mecharla Apr 16 '19 at 07:45
  • The templateDirectory had two mustache files. I am not familiar with mustache. I would like to know how to use a mustache template to achieve my goal (double --> BigDecimal). – Vivek Mecharla Apr 16 '19 at 07:48

1 Answers1

26

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>
Vivek Mecharla
  • 491
  • 1
  • 4
  • 10