OpenAPITools / OpenAPITools/openapi-generator
[BUG][java] oneOf in "anonymous" schemas: model not generated.
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)?
- What's the version of OpenAPI Generator used?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
As mentioned in #5706, if the openapi spec file defines a oneOf schema in responses "as is", the generator references a model in the DefaultApi but the model doesn't get generated. See examples below.
edit: the same issue happens to all the places where oneOf is used without being directly under a named schema (e.g. responses, array items etc.). See the comments.
openapi-generator version
latest master: cc09118ffc2f13ca5d251049dc3d7552f08b670a
OpenAPI declaration file content or url
OpenApi spec file:
openapi: 3.0.3
info:
title: oneOf test
version: '1.0'
paths:
/state:
get:
operationId: getState
responses:
'200':
description: get states
content:
application/json:
schema: # doesn't work
oneOf:
- $ref: '#/components/schemas/ObjA'
- $ref: '#/components/schemas/ObjB'
discriminator:
propertyName: realtype
/state_ref:
get:
operationId: getStateRef
responses:
'200':
description: 'get states using $ref'
content:
application/json:
schema: # logically same as above but works
$ref: '#/components/schemas/ObjAOrObjB'
components:
schemas:
ObjA:
type: object
properties:
realtype:
type: string
message:
type: string
ObjB:
type: object
properties:
realtype:
type: string
code:
type: integer
ObjAOrObjB:
oneOf:
- $ref: '#/components/schemas/ObjA'
- $ref: '#/components/schemas/ObjB'
discriminator:
propertyName: realtype
Command line used for generation
openapi-generator generate -i spec.yaml -g java --library native -o ./out/java_oneof
Steps to reproduce
- use spec file above
- run command above
- notice how
src/main/java/org/openapitools/client/api/DefaultApi.javafeatures
but theimport org.openapitools.client.model.GetState200; import org.openapitools.client.model.ObjAOrObjB;GetState200model is not present insrc/main/java/org/openapitools/client/modelwhileObjAOrObjBis.
@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @bkabrda
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
Run the supplied OpenAPI YAML with openapi-generator generate -i spec.yaml -g java --library native -o ./out/java_oneof. Compare src/main/java/org/openapitools/client/api/DefaultApi.java with src/main/java/org/openapitools/client/model, focusing on the imported GetState200 and the generated ObjAOrObjB; done means the referenced response model is present and the generated client is consistent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100