openapi-typescript-codegen
openapi-typescript-codegen copied to clipboard
Single quotes in patterns aren't escaped and cause issues with schemas
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