swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.98k stars 6.03k forks source link

[Bug][TypeScript]: single quote in enums not escaped #7369

Open macjohnny opened 6 years ago

macjohnny commented 6 years ago
Description

When defining a model property as an enum, single quotes are not escaped.

Example input:

definitions:
  Order:
    title: Pet Order
    description: An order for a pets from the pet store
    type: object
    properties:
      status:
        type: string
        description: Order Status
        enum:
          - pla'ced
          - approved
          - delivered

Example output:

export interface Order {
    status?: Order.StatusEnum;
}
export namespace Order {
    export type StatusEnum = 'pla'ced' | 'approved' | 'delivered';
    export const StatusEnum = {
        Placed: 'pla'ced' as StatusEnum,
        Approved: 'approved' as StatusEnum,
        Delivered: 'delivered' as StatusEnum
    }
}
Swagger-codegen version

2.3.0

Suggest a fix/enhancement

Fixing the escaping in https://github.com/swagger-api/swagger-codegen/blob/v2.3.0/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java#L342 should solve the issue. The escapeText() method in https://github.com/swagger-api/swagger-codegen/blob/v2.3.0/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java#L376 does not escape single quotes.

macjohnny commented 6 years ago

The escapeQuotationMark() method in https://github.com/swagger-api/swagger-codegen/blob/v2.3.0/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java#L427 is broken too, as it removes single quotes rather than escaping them. But as stated in the original method https://github.com/swagger-api/swagger-codegen/blob/v2.3.0/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java#L412 both quote types should be escaped, which would allow using this function in https://github.com/swagger-api/swagger-codegen/blob/v2.3.0/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java#L342 in order to fix this issue.

macjohnny commented 6 years ago

cc @wing328

macjohnny commented 6 years ago

this issue also affects the typescript angular generator

macjohnny commented 6 years ago

@wing328 could you add this to the 2.4.0 milestone?