kiota-typescript icon indicating copy to clipboard operation
kiota-typescript copied to clipboard

Open API Discriminator not handled properly

Open adsk-duszykf opened this issue 3 months ago • 0 comments

Given this Open API spec:

openapi: 3.0.1

info:
  title: Authentication
  version: 1.0.0

paths:
  /authentication/v2/token:
    post:
      summary: token
      operationId: fetchtoken
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              oneOf:
                - $ref: '#/components/schemas/threeLeggedRequestBody'
                - $ref: '#/components/schemas/twoLeggedRequestBody' 
              discriminator:
                propertyName: grant_type
                mapping:
                  authorization_code: '#/components/schemas/threeLeggedRequestBody'
                  client_credentials: '#/components/schemas/twoLeggedRequestBody'
          
      responses:
        '200':
          description: OK

    parameters: []
components:
  schemas:
    twoLeggedRequestBody:
      type: object
      properties:
        grant_type:
          $ref: '#/components/schemas/Granttype'
        client_id:
          type: string
        client_secret:
          type: string

    threeLeggedRequestBody:
      type: object
      properties:
        grant_type:
          $ref: '#/components/schemas/Granttype'
        code_verifier:
          type: string
        client_id:
          type: string

    Granttype:
      title: granttype
      type: string
      enum:
        - client_credentials
        - authorization_code
        - refresh_token
      description: Enum for grant types

The grant_type properties is transformed to grantTypein the models:

export interface ThreeLeggedRequestBody extends AdditionalDataHolder, Parsable {
    /**
     * This field is required for public client
     */
    clientId?: string | null;
    /**
     * Required if `grant_type` is `authorization_code` and `code_challenge` was specified in /authorize request
     */
    codeVerifier?: string | null;
    /**
     * Enum for grant types
     */
    grantType?: Granttype | null;
}

but the switch in the generated serialization method, uses grant_type which leads to a build error:

export function serializeTokenPostRequestBody(writer: SerializationWriter, tokenPostRequestBody: Partial<RefreshTokenRequestBody | ThreeLeggedRequestBody | TwoLeggedRequestBody> | undefined | null = {}, isSerializingDerivedType: boolean = false) : void {
    if (tokenPostRequestBody === undefined || tokenPostRequestBody === null) return;
    switch (tokenPostRequestBody.grant_type) {
        case "authorization_code":
            serializeThreeLeggedRequestBody(writer, tokenPostRequestBody as ThreeLeggedRequestBody);
            break;
        case "client_credentials":
            serializeTwoLeggedRequestBody(writer, tokenPostRequestBody as TwoLeggedRequestBody);
            break;
        case "refresh_token":
            serializeRefreshTokenRequestBody(writer, tokenPostRequestBody as RefreshTokenRequestBody);
            break;
    }
}

Do I miss something ?

Thank you for Kiota !!

adsk-duszykf avatar Sep 18 '25 21:09 adsk-duszykf