openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] Allow quotes string as enum paramter names, or ensure unique names

Open alexgustafson opened this issue 3 years ago • 4 comments

Is your feature request related to a problem? Please describe.

using the typescript-fetch generator...

I am using Django Rest Framework to create my API. Its common practice to use an OrderingFilter to define fields and values that you specify to sort your results by.

order = OrderingFilter(
        fields=(
            ("id", "id"),
            ("name", "name"),
            ("status", "status"),
        )
    )

the api then will expect the following values:

Available values : -id, -name, -status, id, name, status

The typescript-fetch generator will generate the following enum in typescript:

export enum ListOrderEnum {
    Id = '-id',
    Name = '-name',
    Status = '-status',
    Id = 'id',
    Name = 'name',
    Status = 'status'
}

The minus sign is lost in the enum property names ( creating duplicate names ). I need to manually rename the property names. I use quotes to properly rename them, for example: '-Status' = '-status'

Describe the solution you'd like

Add an option that will allow the enum parameter name to be quoted strings, or that otherwise preserves the '-' in some other way.

Describe alternatives you've considered

something like 'ensureUniqueParams' but 'enumUniquePropertyNaming' that otherwise renames duplicate values. The '-' sign's signaling that this is a descending order parameter in the sort function might be lost, but at least my code would compile.

I

Additional context

alexgustafson avatar Mar 14 '22 09:03 alexgustafson

I had the same issue with an enum spec like this that contains "special" characters:

    ContextVariableDataType:
      type: string
      description: Data type of a variable in a workflow execution variable context
      enum:
        - String
        - Date
        - PurchaseOrder
        - Shipment
        - Tender
        - PurchaseOrder[]
        - Shipment[]
        - Tender[]

This generates duplicate enum names:

/**
 * Data type of a variable in a workflow execution variable context
 * @export
 * @enum {string}
 */

export const ContextVariableDataType = {
    String: 'String',
    Date: 'Date',
    PurchaseOrder: 'PurchaseOrder',
    Shipment: 'Shipment',
    Tender: 'Tender',
    PurchaseOrder: 'PurchaseOrder[]',
    Shipment: 'Shipment[]',
    Tender: 'Tender[]'
} as const;

export type ContextVariableDataType = typeof ContextVariableDataType[keyof typeof ContextVariableDataType];

phillipuniverse avatar May 05 '22 14:05 phillipuniverse

@alexgustafson FYI I found a hacky way around this, it would probably work for you too.

  1. Create a bash script file-postprocessor.sh
    #!/bin/bash
    
    if [[ $1 == *api.ts ]] # * is used for pattern matching
    then
      echo "post-processing api.ts"
      sed -i "s/PurchaseOrder: 'PurchaseOrder\[\]',/PurchaseOrderArray: 'PurchaseOrder\[\]',/" $1
      sed -i "s/Shipment: 'Shipment\[\]',/ShipmentArray: 'Shipment\[\]',/" $1
      sed -i "s/Tender: 'Tender\[\]'/TenderArray: 'Tender\[\]'/" $1  # Tender entry is last in the list at time of writing, no trailing comma
    fi
    
  2. Set the environment variable TS_POST_PROCESS_FILE=/path/to/file_postprocessor.sh
  3. Set the --enable-post-process-file argument on the generator e.g. generate -g typescript-axios -i /path/to/api.yaml -o /build --enable-post-process-file ...

More information at https://openapi-generator.tech/docs/file-post-processing/

phillipuniverse avatar May 05 '22 15:05 phillipuniverse

@phillipuniverse Thanks! This workaround will definitely help and save me a lot of time.

I wont close this issue yet, hoping that a more robust fix will come. ( --enumNamesInQuotes hint hint someone? )

alexgustafson avatar May 05 '22 17:05 alexgustafson

This also happens if you try to use Apple's App Store Connect OpenAPI spec. They have parameters for controlling the sorting of returned data, and to sort in reverse you prefix the value with a -, like in the django example given above. The typescript-fetch code ends up generating code like:

export const AppsCustomerReviewsGetToManyRelatedSortEnum = {
    CreatedDate: 'createdDate',
    CreatedDate: '-createdDate',
    Rating: 'rating',
    Rating: '-rating'
} as const;

or, if using -p stringEnums=true :

export enum AppsCustomerReviewsGetToManyRelatedSortEnum {
    CreatedDate = 'createdDate',
    CreatedDate = '-createdDate',
    Rating = 'rating',
    Rating = '-rating'
}

Either way causes issues. And no amount of fiddling with enumPropertyNaming seems to help. The only way to fix it would seem to be to use the original value as the name and quote it. But at that point, having a mapping of a string to the exact same representation of the string isn't very helpful. Having something like:

export type AppsCustomerReviewsGetToManyRelatedSortEnum = 'createdDate' | '-createdDate' | 'rating' | '-rating';

would probably be fine.

The typescript generator seems to just inline the string union like:

public async appsCustomerReviewsGetToManyRelated(id: string, ..., sort?: Array<'createdDate' | '-createdDate' | 'rating' | '-rating'>, ...) {
  // [...snip...]

  // Query Params
  if (sort !== undefined) {
      requestContext.setQueryParam("sort", ObjectSerializer.serialize(sort, "Array<'createdDate' | '-createdDate' | 'rating' | '-rating'>", ""));
  }

  // [...snip...]

}

onlynone avatar Sep 15 '22 16:09 onlynone

@alexgustafson i see you labeled this as "Enhancement: Feature", but this is a bug, as it prevents normal usage.

There's similar problem and suggestion to update enum name generation logic, i'm not a Java programmer, but i can try to make a PR for this. Do you need any help? Will it be merged if it has some backward incompatible changes?

I see there's MINUS_/PLUS_/DOT_ handling for numbers, but it should work for strings too.

https://github.com/OpenAPITools/openapi-generator/issues/5110#issuecomment-823818324

last-partizan avatar Apr 14 '23 07:04 last-partizan

@alexgustafson Oh, you're reported this bug. I was thinking you're team member :)

last-partizan avatar Apr 14 '23 07:04 last-partizan