Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][Typescript] Incorrect response type handling for oneOf strategy #9305

Open
5 of 6 tasks
dinobal opened this issue Apr 21, 2021 · 4 comments
Open
5 of 6 tasks

[BUG][Typescript] Incorrect response type handling for oneOf strategy #9305

dinobal opened this issue Apr 21, 2021 · 4 comments

Comments

@dinobal
Copy link

dinobal commented Apr 21, 2021

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
  • Is there a way to not have a wrapper object as an input parameter and instead just take an union type?
  • The return types are correct but it seems not to be handled correctly, a bug perhaps?
  • If everything works as intended how can I change my schema so the generated client has a method that can parse the response and type it correctly?
openapi-generator version

5.1.0

OpenAPI declaration file content or url

specs.yaml

openapi: 3.0.1
info:
  title: Sample API
  description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  version: 0.1.9
servers:
  - url: http://api.example.com/v1
    description: Optional server description, e.g. Main (production) server
  - url: http://staging-api.example.com
    description: Optional server description, e.g. Internal staging server for testing
paths:
  /pets:
    post:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: pet_type
                mapping:
                  obj1: '#/components/schemas/Cat'
                  obj2: '#/components/schemas/Dog'
      responses:
        '200':
          description: Created
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Cat'
                  - $ref: '#/components/schemas/Dog'
                discriminator:
                  propertyName: pet_type
                  mapping:
                    obj1: '#/components/schemas/Cat'
                    obj2: '#/components/schemas/Dog'
components:
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
    Dog:
      type: object
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
      properties:
        bark:
          type: boolean
        breed:
          type: string
          enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:
      type: object
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
      properties:
        hunts:
          type: boolean
        age:
          type: integer
Generation Details
java -jar openapi-generator-cli.jar generate -i specs.yaml -g typescript-fetch -o tsclient/
Output
...
export interface PetsPostRequest {
    catDog?: Cat | Dog;
}

/**
 * 
 */
export class DefaultApi extends runtime.BaseAPI {

    /**
     */
    async petsPostRaw(requestParameters: PetsPostRequest): Promise<runtime.ApiResponse<Cat | Dog>> {
        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        headerParameters['Content-Type'] = 'application/json';

        const response = await this.request({
            path: `/pets`,
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: Cat | DogToJSON(requestParameters.catDog),
        });

        return new runtime.JSONApiResponse(response, (jsonValue) => Cat | DogFromJSON(jsonValue));
    }

    /**
     */
    async petsPost(requestParameters: PetsPostRequest): Promise<Cat | Dog> {
        const response = await this.petsPostRaw(requestParameters);
        return await response.value();
    }
Expected, something like
async petsPostRaw(pet: Cat | Dog | undefined ): Promise<runtime.ApiResponse<Cat | Dog>> {
        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        headerParameters['Content-Type'] = 'application/json';
        const response = await this.request({
            path: `/pets`,
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: pet.petType === 'Cat' ? CatToJSON() : pet.petType === 'Dog' ? DogToJSON() : undefined,
        });

        return new runtime.JSONApiResponse(response, 
            (jsonValue) => {
            const typedValue = 
                pet.petType === 'Cat' ? CatFromJSON(jsonValue) 
                : pet.petType === 'Dog' ? DogFromJSON(jsonValue) 
                : undefined;

            if(!typedValue){
                throw new Error(`No type matching "${pet.petType}" found`);
            }
            
            return typedValue;
        });
    }

    /**
     */
    async petsPost(pet: Cat | Dog): Promise<Cat | Dog> {
        const response = await this.petsPostRaw(pet);
        return await response.value();
    }
Related issues/PRs
  • Probably more than one regarding oneOf, anyOf, allOf
@dinobal dinobal changed the title [BUG] Description [BUG][Typescript] Incorrect response type handling for oneOf strategy Apr 21, 2021
@miroslavvojtus
Copy link

We have the same issue.
@dinobal Did you found a workaround?
We tried to downgrade but the generation is there invalid as well.

@dinobal
Copy link
Author

dinobal commented Apr 26, 2021

Hi @miroslavvojtus
I have not yet found any workarounds that would keep the validation against schema available. Tried a bunch of different client generators with identical or worse issues regarding this but no progress.

@miroslavvojtus
Copy link

We had to reduce the schema to @Schema(implementation = Object.class).
This is a temporary workaround as we loose any validation and typings on the client side.
For us is this a blocking issue.

@ksvirkou-hubspot
Copy link
Contributor

ksvirkou-hubspot commented Apr 30, 2021

We have the same issue
For us it is a blocker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants