Skip to content

Properties with leading double underscores (__) not generated properly from OpenAPI #6758

@zuzannaczajaichoosr

Description

@zuzannaczajaichoosr

What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Nuget tool

Client library/SDK language

TypeScript

Describe the bug

When generating a TypeScript client using Kiota from an OpenAPI specification, properties that start with double underscores (e.g. __component) are not correctly generated in the resulting models or types.
This causes issues when working with APIs (like Strapi) that use such fields as discriminators for dynamic zones.

For example, in the generated code I get the following error:
Property '__component' does not exist on type 'Partial<FridgeComponent | WashingMachineComponent>'. Did you mean 'component'? Property '__component' does not exist on type 'Partial<FridgeComponent>'.

This relates to the following code fragment:

export function serializeProductComponent(writer: SerializationWriter, productComponent: Partial<FridgeComponent | WashingMachineComponent> | undefined | null = {}) : void {
    if (productComponent === undefined || productComponent === null) return;
    switch (productComponent.__component) { //ERROR
        case "products.fridge":
            serializeFridgeComponent(writer, productComponent as FridgeComponent);
            break;
        case "products.washing-machine":
            serializeWashingMachineComponent(writer, productComponent as WashingMachineComponent);
            break;
    }
}

It appears that the generated types use __component instead of component, so accessing productComponent.__component results in a TypeScript error.

Expected behavior

Properties with double underscores (e.g. __component) should be correctly generated.

How to reproduce

Use the following OpenAPI schema with a property starting with double underscores (e.g. __component) as a discriminator and generate a client

Open API description file

{
  "openapi": "3.0.0",
  "info": {
    "title": "Strapi Example API",
    "version": "1.0.0"
  },
  "paths": {
    "/products": {
      "get": {
        "responses": {
          "200": {
            "description": "List of products",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProductListResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ProductListResponse": {
        "type": "object",
        "properties": {
          "data": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Product" }
          }
        }
      },
      "Product": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "title": { "type": "string" },
          "data": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/ProductComponent" }
          }
        }
      },
      "ProductComponent": {
        "type": "object",
        "discriminator": {
          "propertyName": "__component",
          "mapping": {
            "products.fridge": "#/components/schemas/FridgeComponent",
            "products.washing-machine": "#/components/schemas/WashingMachineComponent"
          }
        },
        "oneOf": [
          { "$ref": "#/components/schemas/FridgeComponent" },
          { "$ref": "#/components/schemas/WashingMachineComponent" }
        ]
      },
      "FridgeComponent": {
        "type": "object",
        "properties": {
          "__component": { "type": "string", "enum": ["products.fridge"] },
          "capacity": { "type": "integer", "description": "Capacity in liters" },
          "energyClass": { "type": "string" }
        }
      },
      "WashingMachineComponent": {
        "type": "object",
        "properties": {
          "__component": { "type": "string", "enum": ["products.washing-machine"] },
          "maxLoad": { "type": "integer", "description": "Max load in kg" },
          "spinSpeed": { "type": "integer", "description": "Spin speed in RPM" }
        }
      }
    }
  }
}

Kiota Version

1.28.0+57130b1b1db3bc5c060498682f41e20c8ae089f2

Latest Kiota version known to work for scenario above?(Not required)

No response

Known Workarounds

No response

Configuration

No response

Debug output

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypeScriptPull requests that update Javascript codestatus:waiting-for-triageAn issue that is yet to be reviewed or assignedtype:bugA broken experience

    Type

    No type

    Projects

    Status

    Needs Triage 🔍

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions