Skip to content

Commit b50e6cc

Browse files
gurkodildavidricodias
authored andcommitted
fix(typescript-fetch): generate validationAttributes when withoutRuntimeChecks=true (OpenAPITools#23107)
* fix(typescript-fetch): generate validationAttributes when withoutRuntimeChecks=true Previously, setting `validationAttributes=true` had no effect when `withoutRuntimeChecks=true` was set. This commit fixes that by adding the validation attributes to the `modelGenericInterfaces.mustache` template (that is used when withoutRuntimeChecks is enabled) - Moves validationAttributes logic to a partial template and includes it in the modelGenericInterfaces.mustache template - modelGeneric.mustache includes the modelGenericInterfaces.mustache partial and should work as before * update samples
1 parent ef6c9e3 commit b50e6cc

10 files changed

Lines changed: 206 additions & 192 deletions

File tree

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

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -194,68 +194,3 @@ export function {{classname}}ToJSONTyped(value?: {{#hasReadOnly}}Omit<{{classnam
194194
return value;
195195
{{/hasVars}}
196196
}
197-
{{#validationAttributes}}
198-
199-
export const {{classname}}PropertyValidationAttributesMap: {
200-
[property: string]: {
201-
maxLength?: number,
202-
minLength?: number,
203-
pattern?: string,
204-
maximum?: number,
205-
exclusiveMaximum?: boolean,
206-
minimum?: number,
207-
exclusiveMinimum?: boolean,
208-
multipleOf?: number,
209-
maxItems?: number,
210-
minItems?: number,
211-
uniqueItems?: boolean
212-
}
213-
} = {
214-
{{#vars}}
215-
{{#hasValidation}}
216-
{{name}}: {
217-
{{#maxLength}}
218-
maxLength: {{maxLength}},
219-
{{/maxLength}}
220-
{{#minLength}}
221-
minLength: {{minLength}},
222-
{{/minLength}}
223-
{{#pattern}}
224-
pattern: '{{pattern}}',
225-
{{/pattern}}
226-
{{#maximum}}
227-
maximum: {{maximum}},
228-
exclusiveMaximum: {{exclusiveMaximum}},
229-
{{/maximum}}
230-
{{#minimum}}
231-
minimum: {{minimum}},
232-
exclusiveMinimum: {{exclusiveMinimum}},
233-
{{/minimum}}
234-
{{#multipleOf}}
235-
multipleOf: {{multipleOf}},
236-
{{/multipleOf}}
237-
{{#maxItems}}
238-
maxItems: {{maxItems}},
239-
{{/maxItems}}
240-
{{#minItems}}
241-
minItems: {{minItems}},
242-
{{/minItems}}
243-
{{#isArray}}
244-
uniqueItems: {{uniqueItems}},
245-
{{/isArray}}
246-
},
247-
{{/hasValidation}}
248-
{{/vars}}
249-
}
250-
{{#isAdditionalPropertiesTrue}}
251-
252-
export const {{classname}}AdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = {
253-
{{#maxProperties}}
254-
maxProperties: {{maxProperties}},
255-
{{/maxProperties}}
256-
{{#minProperties}}
257-
minProperties: {{minProperties}},
258-
{{/minProperties}}
259-
}
260-
{{/isAdditionalPropertiesTrue}}
261-
{{/validationAttributes}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ export const {{classname}}{{enumName}} = {
4747
} as const;
4848
export type {{classname}}{{enumName}} = typeof {{classname}}{{enumName}}[keyof typeof {{classname}}{{enumName}}];
4949
{{/stringEnums}}
50-
{{/isEnum}}{{/vars}}{{/hasEnums}}
50+
{{/isEnum}}{{/vars}}{{/hasEnums}}{{>validationAttributes}}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{{#validationAttributes}}
2+
3+
export const {{classname}}PropertyValidationAttributesMap: {
4+
[property: string]: {
5+
maxLength?: number,
6+
minLength?: number,
7+
pattern?: string,
8+
maximum?: number,
9+
exclusiveMaximum?: boolean,
10+
minimum?: number,
11+
exclusiveMinimum?: boolean,
12+
multipleOf?: number,
13+
maxItems?: number,
14+
minItems?: number,
15+
uniqueItems?: boolean
16+
}
17+
} = {
18+
{{#vars}}
19+
{{#hasValidation}}
20+
{{name}}: {
21+
{{#maxLength}}
22+
maxLength: {{maxLength}},
23+
{{/maxLength}}
24+
{{#minLength}}
25+
minLength: {{minLength}},
26+
{{/minLength}}
27+
{{#pattern}}
28+
pattern: '{{pattern}}',
29+
{{/pattern}}
30+
{{#maximum}}
31+
maximum: {{maximum}},
32+
exclusiveMaximum: {{exclusiveMaximum}},
33+
{{/maximum}}
34+
{{#minimum}}
35+
minimum: {{minimum}},
36+
exclusiveMinimum: {{exclusiveMinimum}},
37+
{{/minimum}}
38+
{{#multipleOf}}
39+
multipleOf: {{multipleOf}},
40+
{{/multipleOf}}
41+
{{#maxItems}}
42+
maxItems: {{maxItems}},
43+
{{/maxItems}}
44+
{{#minItems}}
45+
minItems: {{minItems}},
46+
{{/minItems}}
47+
{{#isArray}}
48+
uniqueItems: {{uniqueItems}},
49+
{{/isArray}}
50+
},
51+
{{/hasValidation}}
52+
{{/vars}}
53+
}
54+
{{#isAdditionalPropertiesTrue}}
55+
56+
export const {{classname}}AdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = {
57+
{{#maxProperties}}
58+
maxProperties: {{maxProperties}},
59+
{{/maxProperties}}
60+
{{#minProperties}}
61+
minProperties: {{minProperties}},
62+
{{/minProperties}}
63+
}
64+
{{/isAdditionalPropertiesTrue}}
65+
{{/validationAttributes}}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,20 @@ public void testOneOfModelsImportNonPrimitiveTypes() throws IOException {
444444
TestUtils.assertFileContains(testResponse, "import type { OptionThree } from './OptionThree'");
445445
}
446446

447+
@Test(description = "Verify validationAttributes works with withoutRuntimeChecks=true")
448+
public void testValidationAttributesWithWithoutRuntimeChecks() throws IOException {
449+
Map<String, Object> properties = new HashMap<>();
450+
properties.put(TypeScriptFetchClientCodegen.VALIDATION_ATTRIBUTES, true);
451+
properties.put(TypeScriptFetchClientCodegen.WITHOUT_RUNTIME_CHECKS, true);
452+
453+
File output = generate(properties, "src/test/resources/3_0/typescript-fetch/validation-attributes.yaml");
454+
455+
Path modelsIndex = Paths.get(output + "/models/index.ts");
456+
TestUtils.assertFileExists(modelsIndex);
457+
TestUtils.assertFileContains(modelsIndex, "PetPropertyValidationAttributesMap");
458+
TestUtils.assertFileContains(modelsIndex, "[property: string]:");
459+
}
460+
447461
private static File generate(
448462
Map<String, Object> properties
449463
) throws IOException {

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ export interface Category {
3232
*/
3333
name?: string;
3434
}
35+
export const CategoryPropertyValidationAttributesMap: {
36+
[property: string]: {
37+
maxLength?: number,
38+
minLength?: number,
39+
pattern?: string,
40+
maximum?: number,
41+
exclusiveMaximum?: boolean,
42+
minimum?: number,
43+
exclusiveMinimum?: boolean,
44+
multipleOf?: number,
45+
maxItems?: number,
46+
minItems?: number,
47+
uniqueItems?: boolean
48+
}
49+
} = {
50+
name: {
51+
pattern: '/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/',
52+
},
53+
}
54+
3555

3656
/**
3757
* Check if a given object implements the Category interface.
@@ -71,23 +91,3 @@ export function CategoryToJSONTyped(value?: Category | null, ignoreDiscriminator
7191
};
7292
}
7393

74-
export const CategoryPropertyValidationAttributesMap: {
75-
[property: string]: {
76-
maxLength?: number,
77-
minLength?: number,
78-
pattern?: string,
79-
maximum?: number,
80-
exclusiveMaximum?: boolean,
81-
minimum?: number,
82-
exclusiveMinimum?: boolean,
83-
multipleOf?: number,
84-
maxItems?: number,
85-
minItems?: number,
86-
uniqueItems?: boolean
87-
}
88-
} = {
89-
name: {
90-
pattern: '/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/',
91-
},
92-
}
93-

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ export interface ModelApiResponse {
3838
*/
3939
message?: string;
4040
}
41+
export const ModelApiResponsePropertyValidationAttributesMap: {
42+
[property: string]: {
43+
maxLength?: number,
44+
minLength?: number,
45+
pattern?: string,
46+
maximum?: number,
47+
exclusiveMaximum?: boolean,
48+
minimum?: number,
49+
exclusiveMinimum?: boolean,
50+
multipleOf?: number,
51+
maxItems?: number,
52+
minItems?: number,
53+
uniqueItems?: boolean
54+
}
55+
} = {
56+
}
57+
4158

4259
/**
4360
* Check if a given object implements the ModelApiResponse interface.
@@ -79,20 +96,3 @@ export function ModelApiResponseToJSONTyped(value?: ModelApiResponse | null, ign
7996
};
8097
}
8198

82-
export const ModelApiResponsePropertyValidationAttributesMap: {
83-
[property: string]: {
84-
maxLength?: number,
85-
minLength?: number,
86-
pattern?: string,
87-
maximum?: number,
88-
exclusiveMaximum?: boolean,
89-
minimum?: number,
90-
exclusiveMinimum?: boolean,
91-
multipleOf?: number,
92-
maxItems?: number,
93-
minItems?: number,
94-
uniqueItems?: boolean
95-
}
96-
} = {
97-
}
98-

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ export const OrderStatusEnum = {
6969
} as const;
7070
export type OrderStatusEnum = typeof OrderStatusEnum[keyof typeof OrderStatusEnum];
7171

72+
export const OrderPropertyValidationAttributesMap: {
73+
[property: string]: {
74+
maxLength?: number,
75+
minLength?: number,
76+
pattern?: string,
77+
maximum?: number,
78+
exclusiveMaximum?: boolean,
79+
minimum?: number,
80+
exclusiveMinimum?: boolean,
81+
multipleOf?: number,
82+
maxItems?: number,
83+
minItems?: number,
84+
uniqueItems?: boolean
85+
}
86+
} = {
87+
}
88+
89+
export const OrderAdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = {
90+
maxProperties: 10,
91+
minProperties: 2,
92+
}
93+
7294

7395
/**
7496
* Check if a given object implements the Order interface.
@@ -118,25 +140,3 @@ export function OrderToJSONTyped(value?: Order | null, ignoreDiscriminator: bool
118140
};
119141
}
120142

121-
export const OrderPropertyValidationAttributesMap: {
122-
[property: string]: {
123-
maxLength?: number,
124-
minLength?: number,
125-
pattern?: string,
126-
maximum?: number,
127-
exclusiveMaximum?: boolean,
128-
minimum?: number,
129-
exclusiveMinimum?: boolean,
130-
multipleOf?: number,
131-
maxItems?: number,
132-
minItems?: number,
133-
uniqueItems?: boolean
134-
}
135-
} = {
136-
}
137-
138-
export const OrderAdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = {
139-
maxProperties: 10,
140-
minProperties: 2,
141-
}
142-

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ export const PetStatusEnum = {
8484
} as const;
8585
export type PetStatusEnum = typeof PetStatusEnum[keyof typeof PetStatusEnum];
8686

87+
export const PetPropertyValidationAttributesMap: {
88+
[property: string]: {
89+
maxLength?: number,
90+
minLength?: number,
91+
pattern?: string,
92+
maximum?: number,
93+
exclusiveMaximum?: boolean,
94+
minimum?: number,
95+
exclusiveMinimum?: boolean,
96+
multipleOf?: number,
97+
maxItems?: number,
98+
minItems?: number,
99+
uniqueItems?: boolean
100+
}
101+
} = {
102+
photoUrls: {
103+
maxItems: 8,
104+
minItems: 1,
105+
uniqueItems: true,
106+
},
107+
}
108+
87109

88110
/**
89111
* Check if a given object implements the Pet interface.
@@ -133,25 +155,3 @@ export function PetToJSONTyped(value?: Pet | null, ignoreDiscriminator: boolean
133155
};
134156
}
135157

136-
export const PetPropertyValidationAttributesMap: {
137-
[property: string]: {
138-
maxLength?: number,
139-
minLength?: number,
140-
pattern?: string,
141-
maximum?: number,
142-
exclusiveMaximum?: boolean,
143-
minimum?: number,
144-
exclusiveMinimum?: boolean,
145-
multipleOf?: number,
146-
maxItems?: number,
147-
minItems?: number,
148-
uniqueItems?: boolean
149-
}
150-
} = {
151-
photoUrls: {
152-
maxItems: 8,
153-
minItems: 1,
154-
uniqueItems: true,
155-
},
156-
}
157-

0 commit comments

Comments
 (0)