A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/OpenAPITools/openapi-generator/issues/9305 below:

[BUG][Typescript] Incorrect response type handling for oneOf strategy · Issue #9305 · OpenAPITools/openapi-generator · GitHub

Bug Report Checklist Description 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

miroslavvojtus and jyotishp


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4