OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][SPRING] missing JsonValue import if allOf used with discriminator

Open
#14,100 5 comments 1 reaction 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

Missing import means that code does not compile...

openapi-generator version

6.3.0-snapshot, 6.2.1

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Test
  description: 'Test'
  version: v2.0

tags:
  - name: Test

paths:
  /test:
    get:
      tags:
        - Test
      description: Test
      operationId: test
      responses:
        '204':
          description: ''
    
components:
  schemas:
    SimulationEvent:
      required:
        - eventTime
      type: object
      properties:
        eventTime:
          type: integer
          format: int64
      discriminator:
        propertyName: type
    TransactionEvent:
      type: object
      properties:
        transactionType:
          type: string
          enum:
            - purchase
            - withdraw
            - refund
            - load
      allOf:
        - $ref: '#/components/schemas/SimulationEvent'
    
Generation Details
docker run --rm --workdir /github/workspace -v $(pwd):/github/workspace openapitools/openapi-generator-cli:v6.2.1 generate -g spring -o /github/workspace/spring-client -i /github/workspace/contract.yml -p groupId=co.test,basePackage=co.test.v2.server.config,basePackage=co.test.v2.server,apiPackage=co.test.v2.server,invokerPackage=co.test.v2.server.invoker,modelPackage=co.test.v2.server.models,library=spring-boot,artifactId=test-v2-server,documentationProvider=none,useSwaggerUI=false,interfaceOnly=true,snapshotVersion=true
Steps to reproduce
  1. Save the contract above into file contract.yml
  2. Run the docker command above
  3. open spring-client/src/main/java/co/test/v2/server/models/TransactionEvent.java
  4. Verify that @JsonValue is used in code but it is not imported
Related issues/PRs

There was similar issue before https://github.com/OpenAPITools/openapi-generator/issues/11323 And I can confirm that this is similar nature issue because hasEnums is not set during generation of that class...

Suggest a fix

I dont have suggestion for a fix, but tmp workaround is to add this code on top of pojo.mustache:

{{#jackson}}
{{^hasEnums}}
import com.fasterxml.jackson.annotation.JsonValue;
{{/hasEnums}}
{{/jackson}}

As you can see I am adding import only when the hasEnums is false... Because otherwise we are risking that we will have same import twice...

Important debugging note:
If you remove discriminator from the contract above, the generation is working fine...

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

Start with pojo.mustache and reproduce the report using contract.yml and the documented Docker generation command. Inspect the generated TransactionEvent.java, especially the @JsonValue usage and hasEnums behavior when discriminator and allOf are present. Done means the generated Spring Java code includes the required import exactly once and compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.