OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA][SPRING] missing JsonValue import if allOf used with discriminator
Nobody has claimed this yet.
- 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
- Save the contract above into file
contract.yml - Run the docker command above
- open
spring-client/src/main/java/co/test/v2/server/models/TransactionEvent.java - Verify that
@JsonValueis 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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