Skip to content

Commit 4330b2f

Browse files
[typescript-fetch] Add option to generate validation attributes of model properties (#19448)
1 parent aae3ab3 commit 4330b2f

File tree

22 files changed

+3047
-1
lines changed

22 files changed

+3047
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
generatorName: typescript-fetch
2+
outputDir: samples/client/petstore/typescript-fetch/builds/validation-attributes
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-fetch/validation-attributes.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/typescript-fetch
5+
additionalProperties:
6+
validationAttributes: true

docs/generators/typescript-fetch.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4444
|supportsES6|Generate code that conforms to ES6.| |false|
4545
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |true|
4646
|useSquareBracketsInArrayNames|Setting this property to true will add brackets to array attribute names, e.g. my_values[].| |false|
47+
|validationAttributes|Setting this property to true will generate the validation attributes of model properties.| |false|
4748
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
4849
|withoutRuntimeChecks|Setting this property to true will remove any runtime checks on the request and response payloads. Payloads will be casted to their expected types.| |false|
4950

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
6060
public static final String CAMEL_CASE = "camelCase";
6161
public static final String PASCAL_CASE = "PascalCase";
6262
public static final String USE_SQUARE_BRACKETS_IN_ARRAY_NAMES = "useSquareBracketsInArrayNames";
63+
public static final String VALIDATION_ATTRIBUTES = "validationAttributes";
6364

6465
@Getter @Setter
6566
protected String npmRepository = null;
@@ -92,7 +93,8 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
9293
@Getter @Setter
9394
protected String inferEntityFromUniqueIdWithName = null;
9495
@Setter protected boolean packageAsSourceOnlyLibrary = false;
95-
96+
@Getter @Setter
97+
protected Boolean generateValidationAttributes = false;
9698

9799
public TypeScriptFetchClientCodegen() {
98100
super();
@@ -123,6 +125,7 @@ public TypeScriptFetchClientCodegen() {
123125
this.cliOptions.add(new CliOption(IMPORT_FILE_EXTENSION_SWITCH, IMPORT_FILE_EXTENSION_SWITCH_DESC).defaultValue(""));
124126
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'PascalCase', 'camelCase', 'kebab-case'.").defaultValue(this.fileNaming));
125127
this.cliOptions.add(new CliOption(USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, "Setting this property to true will add brackets to array attribute names, e.g. my_values[].", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
128+
this.cliOptions.add(new CliOption(VALIDATION_ATTRIBUTES, "Setting this property to true will generate the validation attributes of model properties.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
126129
}
127130

128131
@Override
@@ -308,6 +311,8 @@ public void processOpts() {
308311
}
309312
}
310313
}
314+
315+
setGenerateValidationAttributes(convertPropertyToBooleanAndWriteBack(VALIDATION_ATTRIBUTES));
311316
}
312317

313318
@Override

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,68 @@ export function {{classname}}ToJSON(value?: {{#hasReadOnly}}Omit<{{classname}},
158158
return value;
159159
{{/hasVars}}
160160
}
161+
{{#validationAttributes}}
162+
163+
export const {{classname}}PropertyValidationAttributesMap: {
164+
[property: string]: {
165+
maxLength?: number,
166+
minLength?: number,
167+
pattern?: string,
168+
maximum?: number,
169+
exclusiveMaximum?: boolean,
170+
minimum?: number,
171+
exclusiveMinimum?: boolean,
172+
multipleOf?: number,
173+
maxItems?: number,
174+
minItems?: number,
175+
uniqueItems?: boolean
176+
}
177+
} = {
178+
{{#vars}}
179+
{{#hasValidation}}
180+
{{name}}: {
181+
{{#maxLength}}
182+
maxLength: {{maxLength}},
183+
{{/maxLength}}
184+
{{#minLength}}
185+
minLength: {{minLength}},
186+
{{/minLength}}
187+
{{#pattern}}
188+
pattern: '{{pattern}}',
189+
{{/pattern}}
190+
{{#maximum}}
191+
maximum: {{maximum}},
192+
exclusiveMaximum: {{exclusiveMaximum}},
193+
{{/maximum}}
194+
{{#minimum}}
195+
minimum: {{minimum}},
196+
exclusiveMinimum: {{exclusiveMinimum}},
197+
{{/minimum}}
198+
{{#multipleOf}}
199+
multipleOf: {{multipleOf}},
200+
{{/multipleOf}}
201+
{{#maxItems}}
202+
maxItems: {{maxItems}},
203+
{{/maxItems}}
204+
{{#minItems}}
205+
minItems: {{minItems}},
206+
{{/minItems}}
207+
{{#isArray}}
208+
uniqueItems: {{uniqueItems}},
209+
{{/isArray}}
210+
},
211+
{{/hasValidation}}
212+
{{/vars}}
213+
}
214+
{{#isAdditionalPropertiesTrue}}
215+
216+
export const {{classname}}AdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = {
217+
{{#maxProperties}}
218+
maxProperties: {{maxProperties}},
219+
{{/maxProperties}}
220+
{{#minProperties}}
221+
minProperties: {{minProperties}},
222+
{{/minProperties}}
223+
}
224+
{{/isAdditionalPropertiesTrue}}
225+
{{/validationAttributes}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/TypeScriptFetchClientOptionsProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public Map<String, String> createOptions() {
8686
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
8787
.put(TypeScriptFetchClientCodegen.STRING_ENUMS, STRING_ENUMS)
8888
.put(TypeScriptFetchClientCodegen.USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, Boolean.FALSE.toString())
89+
.put(TypeScriptFetchClientCodegen.VALIDATION_ATTRIBUTES, Boolean.FALSE.toString())
8990
.build();
9091
}
9192

0 commit comments

Comments
 (0)