OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Java generator generates wrong default type when type is defined inside allOf

Open
#19,117 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

There is a schema of type number and an attribute uses such schema and set a default. The type number is casted to BigDecimal, but the default is not and compilation fails.

openapi-generator version

Tested with 7.7.0 and 7.8.0-SNAPSHOT (latest docker image)

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Example API v1
  description: A reference API
  contact: {}
  version: 2.0.0

paths: {}

components:
  schemas:
    Length:
      description: Distance in meters between the beginning and end of a roadway or sub-segment.
      type: number
      minimum: 0.0

    MySchema:
      type: object
      properties:
        length:
          allOf:
            - $ref: "#/components/schemas/Length"
          default: 0.0
Generation Details

language: java
library: tested without setting library or jersey2

Steps to reproduce
  1. Create the file openapi-example.yml with the content above in the current directory
  2. Execute docker run -v `pwd`:/temp -it openapitools/openapi-generator-cli:latest generate -g java -i /temp/src/main/resources/apis/ols/intermodal/openapi-example.yml -o /temp/build/
  3. Check the generated schema in build/src/main/java/org/openapitools/client/model/MySchema.java

Expected:

private BigDecimal test = new BigDecimal("0.0");

Actual:

private BigDecimal test = 0.0;
Related issues/PRs

Maybe #12883

Suggest a fix

Not a fix, but the work arounds I found so far:

1: repeat the type in the parameter:

    MySchema:
      type: object
      properties:
        length:
          allOf:
            - $ref: "#/components/schemas/Length"
          type: number.  # <-- this fix the issue
          default: 0.0

or set format: double in the Length schema

    Length:
      description: Distance in meters between the beginning and end of a roadway or sub-segment.
      type: number
      format: double
      minimum: 0.0

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 issue with the provided openapi-example.yml using the Java generator, then inspect the generated build/src/main/java/org/openapitools/client/model/MySchema.java. The fix is complete when the default for the allOf-referenced number is generated as a BigDecimal-compatible value and the generated Java code compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.