I tried generating code for a model using oneOf with master version acf85921e4e7de115139dd1d84adab5011cd4db5 today, and ran into the issue that a number of classes OneOfXxxxYyyyy are imported and used in the generated code, but there are no Java files defining these types.
Everything works fine if I use oneOf at the top level of a schema with no additional properties, effectively making this an interface. However, when I build an object with multiple properties, and one of them is a oneOf, then the generated code is broken.
A complete (broken) example and a slighty different version that works is below.
This issues happens with version acf85921e4e7de115139dd1d84adab5011cd4db5. (master branch before 4.3.0).
A complete (broken) example is here:
openapi: "3.0.1"
info:
version: "1.0.0"
title: "oneOf Test"
paths:
/magic:
get:
responses:
200:
$ref: '#/components/schemas/Response'
components:
schemas:
ResultA:
type: "object"
properties:
type:
type: "string"
ResultB:
type: "object"
properties:
type:
type: "string"
Response:
type: "object"
properties:
message:
type: "string"
result:
oneOf:
- $ref: '#/components/schemas/ResultA'
- $ref: '#/components/schemas/ResultB'
discriminator:
propertyName: type
mapping:
resultA: '#/components/schemas/ResultA'
resultB: '#/components/schemas/ResultB'
The error occurring during maven build of the generated models is:
[ERROR] /tmp/javasdk/src/main/java/org/openapitools/client/model/Response.java:[24,37] cannot find symbol
[ERROR] symbol: class OneOfResultAResultB
[ERROR] location: package org.openapitools.client.model
[ERROR] /tmp/javasdk/src/main/java/org/openapitools/client/model/Response.java:[40,11] cannot find symbol
[ERROR] symbol: class OneOfResultAResultB
[ERROR] location: class org.openapitools.client.model.Response
...
If I redefine Response in the following way it works, without generating any additional classes:
Response:
oneOf:
- $ref: '#/components/schemas/ResultA'
- $ref: '#/components/schemas/ResultB'
discriminator:
propertyName: type
mapping:
resultA: '#/components/schemas/ResultA'
resultB: '#/components/schemas/ResultB'
generate -i openapi.yaml -g java --library jersey2 -o /tmp/javasdk
Run the above command with the example posted above as input schema, then build the generated Java library.
https://github.com/OpenAPITools/openapi-generator/pull/5120 implemented the feature to make the working example work at all, the broken case from this bug here seems to be a special case currently not properly handled.
TBD
Hey 馃憢 thanks for opening this issue, my shot at fixing it is at https://github.com/OpenAPITools/openapi-generator/pull/5400
I faced the same problem, so I hope the PR https://github.com/OpenAPITools/openapi-generator/pull/5400 will be merged soon.
Most helpful comment
Hey 馃憢 thanks for opening this issue, my shot at fixing it is at https://github.com/OpenAPITools/openapi-generator/pull/5400