OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.68k stars 6.55k forks source link

[BUG] [typescript-axios] allOf is not compatible with enum query params #19247

Open s-xu-wafios opened 3 months ago

s-xu-wafios commented 3 months ago

Bug Report Checklist

Description

When I use "allOf" in a query parameter (enum), the generated "AxiosParamCreator" uses the enum value in a wrong manner.

openapi-generator version

7.7.0

OpenAPI declaration file content or url

The following definition will cause the error

CustomFieldTypeEnum:
  type: string
  enum:
    - asset
    - work_order
    - spare_part

CustomFieldsTypeQuery:
  name: type
  in: query
  description: Custom Field Type to filter, possible values are in schema CustomFieldTypeEnum
  schema:
    allOf:
      - $ref: ../../enums/CustomFieldTypeEnum.yaml
      - example:
          asset

get:
  tags:
    - Custom Fields
  summary: Fetch custom fields
  description: Fetch custom fields
  operationId: fetchCustomFields
  parameters:
    - $ref: ../components/parameters/custom-fields/CustomFieldsTypeQuery.yaml
Generation Details
generate `
-g typescript-axios `
-i C:\Visualstudio\connectavo-api-cmms-openapi\openapi-api-cmms-v2\openapi-api-cmms-bundled.yaml `
-o C:\Visualstudio\experiment\v2-typescript-axios `
--additional-properties=npmName=v2-axios `
--additional-properties=npmVersion=0.0.0-alpha

The code would be generated:

            if (type !== undefined) {
                for (const [key, value] of Object.entries(type)) {
                    localVarQueryParameter[key] = value;
                }
            }

When the user calls the method and passes in CustomFieldTypeEnum.WorkOrder, the code above will split each character in the enum and result in {0=w&1=o&2=r&3=k&4=_&5=o&6=r&7=d&8=e&9=r}

Steps to reproduce

Call the generated method by running:

      const customFieldsApi = new CustomFieldsApi(config);
      customFieldsApi
        .fetchCustomFields(CustomFieldTypeEnum.WorkOrder)

It will generate a wrong request URL (see following screenshot), where the correct expectation should be type=work_order (instead of w, o, r, k, _, o, r, d, e, r with 0123456789)

image

Suggest a fix

As a temporal work-around, I had to remove "allOf"

CustomFieldsTypeQuery:
  name: type
  in: query
  description: Custom Field Type to filter, possible values are in schema CustomFieldTypeEnum
  schema:
    $ref: ../../enums/CustomFieldTypeEnum.yaml

To make "allOf" work again, I think, in the generated code, the enum should be generated in the same way (as if "allOf" was not being used):

            if (type !== undefined) {
                localVarQueryParameter['type'] = type;
            }
winniehell commented 2 months ago

this is probably the same as https://github.com/OpenAPITools/openapi-generator/issues/18999

s-xu-wafios commented 2 months ago

this is probably the same as #18999

@winniehell yeah the issues are similar. I had to abandon "allOf" to workaround this

winniehell commented 2 months ago

a workaround for the example above would be:

      const customFieldsApi = new CustomFieldsApi(config);
      customFieldsApi
        .fetchCustomFields({'work_order': CustomFieldTypeEnum.WorkOrder})

of course, this is not manageable for bigger code bases.