openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open ddeath opened this issue 4 years ago • 5 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] 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...

ddeath avatar Nov 23 '22 09:11 ddeath

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.

Sax388 avatar Jan 16 '23 15:01 Sax388

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.

mdgilene avatar Jan 24 '23 20:01 mdgilene

At the moment we are using a workaround similar to https://github.com/OpenAPITools/openapi-generator/issues/13124#issuecomment-1369636931

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
}

Rappahannock avatar Feb 28 '23 08:02 Rappahannock

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

GVladi avatar Aug 31 '23 15:08 GVladi

This bug is still present with generator spring in version 7.18.0

DennisPallett avatar Jan 08 '26 12:01 DennisPallett