Skip to content

Commit 5025997

Browse files
authored
fix(typescript-fetch): prevent HTML-escaping of pattern in validationAttributes (#23420)
1 parent 1dfe53c commit 5025997

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

modules/openapi-generator/src/main/resources/typescript-fetch/validationAttributes.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const {{classname}}PropertyValidationAttributesMap: {
2525
minLength: {{minLength}},
2626
{{/minLength}}
2727
{{#pattern}}
28-
pattern: '{{pattern}}',
28+
pattern: '{{{pattern}}}',
2929
{{/pattern}}
3030
{{#maximum}}
3131
maximum: {{maximum}},

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,19 @@ public void testValidationAttributesWithWithoutRuntimeChecks() throws IOExceptio
459459
TestUtils.assertFileContains(modelsIndex, "[property: string]:");
460460
}
461461

462+
@Test(description = "Verify pattern is not HTML-escaped in validationAttributes")
463+
public void testValidationAttributesPatternIsNotHtmlEscaped() throws IOException {
464+
Map<String, Object> properties = new HashMap<>();
465+
properties.put(TypeScriptFetchClientCodegen.VALIDATION_ATTRIBUTES, true);
466+
properties.put(TypeScriptFetchClientCodegen.WITHOUT_RUNTIME_CHECKS, true);
467+
468+
File output = generate(properties, "src/test/resources/3_0/typescript-fetch/validation-attributes.yaml");
469+
470+
Path modelsIndex = Paths.get(output + "/models/index.ts");
471+
TestUtils.assertFileNotContains(modelsIndex, "pattern: '/^[a-z&amp;]+$/'");
472+
TestUtils.assertFileContains(modelsIndex, "pattern: '/^[a-z&]+$/'");
473+
}
474+
462475
@Test(description = "Verify withRequestOptsInInterface=true (default) includes RequestOpts in interface")
463476
public void testRequestOptsInInterfaceByDefault() throws IOException {
464477
Map<String, Object> properties = new HashMap<>();

modules/openapi-generator/src/test/resources/3_0/typescript-fetch/validation-attributes.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ components:
675675
maxLength: 256
676676
phone:
677677
type: string
678+
nickname:
679+
type: string
680+
pattern: '^[a-z&]+$'
678681
userStatus:
679682
type: integer
680683
format: int32

samples/client/petstore/typescript-fetch/builds/validation-attributes/docs/User.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Name | Type
1414
`email` | string
1515
`password` | string
1616
`phone` | string
17+
`nickname` | string
1718
`userStatus` | number
1819

1920
## Example
@@ -30,6 +31,7 @@ const example = {
3031
"email": null,
3132
"password": null,
3233
"phone": null,
34+
"nickname": null,
3335
"userStatus": null,
3436
} satisfies User
3537

samples/client/petstore/typescript-fetch/builds/validation-attributes/models/User.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export interface User {
6161
* @memberof User
6262
*/
6363
phone?: string;
64+
/**
65+
*
66+
* @type {string}
67+
* @memberof User
68+
*/
69+
nickname?: string;
6470
/**
6571
* User Status
6672
* @type {number}
@@ -87,6 +93,9 @@ export const UserPropertyValidationAttributesMap: {
8793
maxLength: 256,
8894
minLength: 8,
8995
},
96+
nickname: {
97+
pattern: '/^[a-z&]+$/',
98+
},
9099
userStatus: {
91100
maximum: 100,
92101
exclusiveMaximum: true,
@@ -121,6 +130,7 @@ export function UserFromJSONTyped(json: any, ignoreDiscriminator: boolean): User
121130
'email': json['email'] == null ? undefined : json['email'],
122131
'password': json['password'] == null ? undefined : json['password'],
123132
'phone': json['phone'] == null ? undefined : json['phone'],
133+
'nickname': json['nickname'] == null ? undefined : json['nickname'],
124134
'userStatus': json['userStatus'] == null ? undefined : json['userStatus'],
125135
};
126136
}
@@ -143,6 +153,7 @@ export function UserToJSONTyped(value?: User | null, ignoreDiscriminator: boolea
143153
'email': value['email'],
144154
'password': value['password'],
145155
'phone': value['phone'],
156+
'nickname': value['nickname'],
146157
'userStatus': value['userStatus'],
147158
};
148159
}

0 commit comments

Comments
 (0)