openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Single quotes in patterns aren't escaped and cause issues with schemas

Open mrtnvh opened this issue 3 years ago • 0 comments

Single quotes in patterns aren't escaped and cause issues with schemas.

Example

Using the ModelWithPattern component from test spec.

OpenAPI doc

{
  "ModelWithPattern": {
    "properties": {
      "patternWithSingleQuotes": {
        "type": "string",
        "pattern": "^[a-zA-Z0-9']*$"
      }
    }
  }
}

Generated schema:

export const $ModelWithPattern = {
    properties: {
        patternWithSingleQuotes: {
            type: 'string',
            pattern: '^[a-zA-Z0-9']*$',
        },
    },
} as const;

In the generated pattern, we see 3 quotes. In TS, this will close the string right behind the '9' character.

Solution

Escape single quotes in patterns.

Desired result:

export const $ModelWithPattern = {
    properties: {
        patternWithSingleQuotes: {
            type: 'string',
            pattern: '^[a-zA-Z0-9\']*$',
        },
    },
} as const;

Proposed solution in #1275

mrtnvh avatar Oct 03 '22 11:10 mrtnvh