quarkus-openapi-generator
quarkus-openapi-generator copied to clipboard
OneOf not resolved properly for Client
Hello,
I am trying to generate a Client with following example yaml:
info:
title: Polymorphic types example
version: 1.0.0
paths:
/pets/{id}:
get:
summary: Get a pet by ID
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: Pet details
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
components:
schemas:
Pet:
type: object
required:
- petType
properties:
petType:
type: string
discriminator:
propertyName: petType
mapping:
cat: '#/components/schemas/Cat'
dog: '#/components/schemas/Dog'
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
name:
type: string
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
breed:
type: string
following classes are generated
The API looks like this:
It is using a PetsIdGet200Response Object instead of the "Pet" Parent class. This PetsIdGet200Response Object only includes the fields from the last defined "onOf", in this case the Dog. If i swap the order of cat and dog in the respone schema, the class changes and now has the fields of the "Cat" class.
Is this a known Bug? Or could it be wrong configuration on my side?
Thanks in advance!
For some reason the embedded pictures don't seems to work, any idea why?
@donndorf it's not a known bug, it's known now :)
Can you dig in and send a PR to fix it? Maybe there's a trick in the openapi schema that we are missing. If that's the case we can add a troubleshooting section to the docs explaining how to fix it.
I am not sure where a fix would have to go or where to find it in the codegen package, maybe i'll have a chance to look into it.
In the meantime i tried to generate a client via CLI to have something to compare it to. That helped a bit more in understanding it. Apparently the usage of the PetsIdGet200Response is standard procedure but it seems to be generated wrong when using the plugin.
Hello @ricardozanini,
I am trying to find the source of aforementioned Problem right now and possibly provide a fix. But i have to admit that i don't yet understand how the extension works.
What's the "Entrypoint" for triggering the code generation? How could i make use of a Debugger in this case? When i inspect the imported Library in my IDE i only see annotations, providers, META-INF and a few Config classes.
Any advice to help me get started would be appreciated.
@donndorf you can start here: https://github.com/quarkiverse/quarkus-openapi-generator/blob/main/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java#L171
We read from the openapi dir during build time and delegate the generation with custom Qute templates to the underlying generator. You can debug there from the unit tests we have in this very same maven module.
Or you can run mvn command in any integration-tests module with debug active and see how that works during the maven lifecycle.
Update after further testing and debugging: it seems that the problem might not be the quarkus plugin after all but rather the underlying openapi-generator itself. For some reason it's not able to properly resolve the "oneOf" in some cases.
So unless anyone else has this problem and thinks it's in the quarkus plugin, this can probably be closed.
May be related to https://github.com/OpenAPITools/openapi-generator/issues/15044.
@ricardozanini @hbelmiro This is being labeled as Stale.
@ricardozanini @hbelmiro This is being closed due to inactivity.
@donndorf Have you tried directly returning the parent schema in the response? (In this case return Pet instead of oneOf). Then, in you sub-schemas, add allOf to reference the parent.
I'm not sure if will work in this case in Quarkus, but I managed to make it work this way in the past directly using the https://github.com/OpenAPITools/openapi-generator/
P.S. The possible downside is that maybe it's not super clear to the API clients that Pet may be abstract. I can't see a way to enforce or indicate it in the YAML (in case it was actually abstract). If there's a way, I'm happy to know it :)
I have the same problem. The OpenAPI JSON for which I want to generate a client is not under my control and generated from Code in a Spring Boot project.
I do not know if the linked OpenAPI-Generator-Issue 15044 or the other one I found (https://github.com/OpenAPITools/openapi-generator/issues/17745) is really related, but for the latter there seems to have been a fix for some java generators which is available since 7.10.0 (client is using 7.14.0).
Apparently oneOf is not officially supported by the Java Code Generator? https://openapi-generator.tech/docs/generators/java/#schema-support-feature (although there are config options related to it, and it clearly seems to produce something...). Just documenting this here, I am very thankful for the information in this issue!
@fkellner, if you figure out how to add support for oneOf or additional configuration, that would help to improve support. Feel free to reopen this issue and send a PR.