Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 of 6 tasks
ddeath opened this issue Nov 23, 2022 · 4 comments
Open
4 of 6 tasks

Comments

@ddeath
Copy link
Contributor

ddeath commented Nov 23, 2022

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 #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...

@Sax388
Copy link

Sax388 commented Jan 16, 2023

Unfortunately without the discriminator property the inheritance is ignored. So we either get a missing import or the class isn't extending the class as intended.

@mdgilene
Copy link

Can confirm, seeing this issue as well. Any update when we might see a fix here? Like @Sax388 mentioned, we effectively cannot have inheritance at the moment otherwise the generated code doesn't even compile.

@Rappahannock
Copy link

At the moment we are using a workaround similar to #13124 (comment)

In gradle:

tasks.register('addJsonValueImport') {
    doLast {
        ant.replace(dir: 'build', includes:'**/<some file>.java',
                token:'import com.fasterxml.jackson.annotation.JsonTypeName;',
                value:'import com.fasterxml.jackson.annotation.JsonTypeName;\nimport com.fasterxml.jackson.annotation.JsonValue;\n')
    }
}

tasks.named('compileJava').configure {
    dependsOn unpack, generateModel, addJsonValueImport
}

@GVladi
Copy link

GVladi commented Aug 31, 2023

I can confirm the issue with version 7.0.0 and generator "spring".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants