swagger-api / swagger-api/swagger-codegen

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

Open
#7,369 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Feature: Enum Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractTypeScriptClientCodegen.java around the referenced escaping logic, then inspect escapeText() in modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java. Verify generated TypeScript enum unions and constants escape single quotes correctly for values such as pla'ced.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.