OpenAPITools / OpenAPITools/openapi-generator

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

Open
#23,615 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the jaxrs-spec generation from the shown YAML and Gradle/Npx arguments, focusing on --model-name-suffix, --type-mappings, --import-mappings, and --language-specific-primitives. Inspect the generated BankAccountDTO.java and compilation output; done means the balance field uses java.math.BigDecimal, imports the JDK type, and no BigDecimalDTO is generated or referenced.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.