Skip to content

[Java] Using BigDecimal for string+decimal (while having DTO suffix) #23615

@i23098

Description

@i23098

I need some help to understand how to configure openapi-generator to use BigDecimal for string+decimal (while having DTO suffix).

On package.json I have:

    "@openapitools/openapi-generator-cli": "2.31.1",

On my yaml file I specified a type:

components:
  schemas:
    BankAccount:
      type: object
      required:
        - id
        - name
        - balance
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        balance:
          type: string
          format: decimal

For Java, I want it to generate BankAccountDTO and use java.math.BigDecimal for the balance.

I'm using gradle, but it's a simple wrapper to call node command:

val generateServerAPITask = tasks.register<NpxTask>("generateServerAPI") {
    dependsOn("npmInstall")
    dependsOn(createApiOutputDirTask)

    workingDir.set(apiOutputDir)

    val outputDir = apiOutputDir.get().dir("server")

    command.set("@openapitools/openapi-generator-cli")
    args.set(listOf(
        "generate",
        "-i", apiSpecFile.absolutePath,
        "-g", "jaxrs-spec",
        "-o", outputDir.asFile.absolutePath,
        "--model-name-suffix", "DTO",
        "--api-package", "com.ralmeida.txc2.web.ws.api",
        "--model-package", "com.ralmeida.txc2.web.ws.dto",
        "--type-mappings", "string+decimal=BigDecimal",
        "--import-mappings", "BigDecimal=java.math.BigDecimal",
        "--language-specific-primitives", "java.math.BigDecimal",
        "--global-property", listOf(
            "models",
            "apis",
            "supportingFiles=false" // Avoids README and other noise
        ).joinToString(","),
        "--additional-properties", listOf(
            "interfaceOnly=true",
            "useJakartaEe=true",
            "dateLibrary=java8",
            "returnResponse=true",
            "serializationLibrary=jackson",
            "generatePom=false",
            "useSwaggerAnnotations=false",
            "openApiNullable=false",
        ).joinToString(",")
    ))

    inputs.file(apiSpecFile)
    outputs.file(apiOutputDir.get().file("openapitools.json"))
    outputs.dir(outputDir)
}

I've been playing with the arguments but can never make it understand that BigDecimal is part of java... it always seem to try to use a non-existant BigDecimalDTO

> Compilation failed; see the compiler output below.
  /home/ralmeida/development/txc2/web/build/openapi/server/src/gen/java/com/ralmeida/txc2/web/ws/dto/BankAccountDTO.java:4: error: cannot find symbol
  import com.ralmeida.txc2.web.ws.dto.BigDecimalDTO;
                                     ^
    symbol:   class BigDecimalDTO
    location: package com.ralmeida.txc2.web.ws.dto
  /home/ralmeida/development/txc2/web/build/openapi/server/src/gen/java/com/ralmeida/txc2/web/ws/dto/BankAccountDTO.java:21: error: cannot find symbol
    private BigDecimalDTO balance;
            ^

What arguments am I missing? Or could it be a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions