quarkiverse / quarkus-openapi-generator

OpenAPI Generator - REST Client Generator
Apache License 2.0
108 stars 66 forks source link

OneOf not resolved properly for Client #733

Open donndorf opened 2 weeks ago

donndorf commented 2 weeks ago

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

image

The API looks like this:

image

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!

donndorf commented 2 weeks ago

For some reason the embedded pictures don't seems to work, any idea why?

ricardozanini commented 2 weeks ago

@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.

donndorf commented 2 weeks ago

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.

donndorf commented 1 week ago

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.

ricardozanini commented 1 week ago

@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.

donndorf commented 4 days ago

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.

hbelmiro commented 3 days ago

May be related to https://github.com/OpenAPITools/openapi-generator/issues/15044.