diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelGenericInterfaces.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelGenericInterfaces.mustache index 7d6aceec7a5e..82420811d764 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelGenericInterfaces.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelGenericInterfaces.mustache @@ -1,3 +1,6 @@ +{{#validationAttributes}} +import type { PropertyValidationAttributes } from '../runtime{{importFileExtension}}'; +{{/validationAttributes}} /** * {{#lambda.indented_star_1}}{{{unescapedDescription}}}{{/lambda.indented_star_1}} * @export diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache index 235458a2df2f..d3ad8212878f 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache @@ -444,3 +444,21 @@ export class TextApiResponse { return await this.raw.text(); }; } +{{#validationAttributes}} + +export interface PropertyValidationAttributes { + dataType?: string, + required?: boolean, + maxLength?: number, + minLength?: number, + pattern?: string, + maximum?: number, + exclusiveMaximum?: boolean, + minimum?: number, + exclusiveMinimum?: boolean, + multipleOf?: number, + maxItems?: number, + minItems?: number, + uniqueItems?: boolean +} +{{/validationAttributes}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/validationAttributes.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/validationAttributes.mustache index 9b338f359c3c..9d2bb22ab3af 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/validationAttributes.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/validationAttributes.mustache @@ -1,22 +1,6 @@ {{#validationAttributes}} -export const {{classname}}PropertyValidationAttributesMap: { - [property: string]: { - dataType?: string, - required?: boolean, - maxLength?: number, - minLength?: number, - pattern?: string, - maximum?: number, - exclusiveMaximum?: boolean, - minimum?: number, - exclusiveMinimum?: boolean, - multipleOf?: number, - maxItems?: number, - minItems?: number, - uniqueItems?: boolean - } -} = { +export const {{classname}}PropertyValidationAttributesMap = { {{#vars}} {{#hasValidation}} {{name}}: { @@ -58,7 +42,7 @@ export const {{classname}}PropertyValidationAttributesMap: { }, {{/hasValidation}} {{/vars}} -} +} satisfies Record; {{#isAdditionalPropertiesTrue}} export const {{classname}}AdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java index 0ba3cd8a5305..45ff28e5a5bd 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java @@ -157,7 +157,7 @@ public void toVarName() { codegen.processOpts(); Assert.assertEquals(codegen.toVarName("valid_var"), "valid_var"); } - + @Test public void toVarNameWithAtSign() { TypeScriptFetchClientCodegen codegen = new TypeScriptFetchClientCodegen(); @@ -577,7 +577,31 @@ public void testValidationAttributesWithWithoutRuntimeChecks() throws IOExceptio Path modelsIndex = Paths.get(output + "/models/index.ts"); TestUtils.assertFileExists(modelsIndex); TestUtils.assertFileContains(modelsIndex, "PetPropertyValidationAttributesMap"); - TestUtils.assertFileContains(modelsIndex, "[property: string]:"); + TestUtils.assertFileContains(modelsIndex, "satisfies Record"); + TestUtils.assertFileContains(modelsIndex, "import type { PropertyValidationAttributes } from '../runtime';"); + + Path runtime = Paths.get(output + "/runtime.ts"); + TestUtils.assertFileExists(runtime); + TestUtils.assertFileContains(runtime, "export interface PropertyValidationAttributes"); + } + + @Test(description = "Verify validationAttributes not exist with validationAttributes=false") + public void testValidationAttributesNotExistWithFalse() throws IOException { + Map properties = new HashMap<>(); + properties.put(TypeScriptFetchClientCodegen.VALIDATION_ATTRIBUTES, false); + properties.put(TypeScriptFetchClientCodegen.WITHOUT_RUNTIME_CHECKS, true); + + File output = generate(properties, "src/test/resources/3_0/typescript-fetch/validation-attributes.yaml"); + + Path modelsIndex = Paths.get(output + "/models/index.ts"); + TestUtils.assertFileExists(modelsIndex); + TestUtils.assertFileNotContains(modelsIndex, "PetPropertyValidationAttributesMap"); + TestUtils.assertFileNotContains(modelsIndex, "satisfies Record"); + TestUtils.assertFileNotContains(modelsIndex, "import type { PropertyValidationAttributes } from '../runtime';"); + + Path runtime = Paths.get(output + "/runtime.ts"); + TestUtils.assertFileExists(runtime); + TestUtils.assertFileNotContains(runtime, "export interface PropertyValidationAttributes"); } @Test(description = "Verify pattern is not HTML-escaped in validationAttributes") diff --git a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Category.ts b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Category.ts index 7a5fb6a636ea..d5f6f2a02d6e 100644 --- a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Category.ts +++ b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Category.ts @@ -13,6 +13,7 @@ */ import { mapValues } from '../runtime'; +import type { PropertyValidationAttributes } from '../runtime'; /** * A category for a pet * @export @@ -28,28 +29,12 @@ export interface Category { */ name?: string; } -export const CategoryPropertyValidationAttributesMap: { - [property: string]: { - dataType?: string, - required?: boolean, - maxLength?: number, - minLength?: number, - pattern?: string, - maximum?: number, - exclusiveMaximum?: boolean, - minimum?: number, - exclusiveMinimum?: boolean, - multipleOf?: number, - maxItems?: number, - minItems?: number, - uniqueItems?: boolean - } -} = { +export const CategoryPropertyValidationAttributesMap = { name: { dataType: "string", pattern: '/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/', }, -} +} satisfies Record; /** diff --git a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/ModelApiResponse.ts b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/ModelApiResponse.ts index 94dbefb8376a..2206c316c65b 100644 --- a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/ModelApiResponse.ts +++ b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/ModelApiResponse.ts @@ -13,6 +13,7 @@ */ import { mapValues } from '../runtime'; +import type { PropertyValidationAttributes } from '../runtime'; /** * Describes the result of uploading an image resource * @export @@ -32,24 +33,8 @@ export interface ModelApiResponse { */ message?: string; } -export const ModelApiResponsePropertyValidationAttributesMap: { - [property: string]: { - dataType?: string, - required?: boolean, - maxLength?: number, - minLength?: number, - pattern?: string, - maximum?: number, - exclusiveMaximum?: boolean, - minimum?: number, - exclusiveMinimum?: boolean, - multipleOf?: number, - maxItems?: number, - minItems?: number, - uniqueItems?: boolean - } -} = { -} +export const ModelApiResponsePropertyValidationAttributesMap = { +} satisfies Record; /** diff --git a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Order.ts b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Order.ts index 0ad9b3725c98..8bae1862d851 100644 --- a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Order.ts +++ b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Order.ts @@ -13,6 +13,7 @@ */ import { mapValues } from '../runtime'; +import type { PropertyValidationAttributes } from '../runtime'; /** * An order for a pets from the pet store * @export @@ -57,24 +58,8 @@ export const OrderStatusEnum = { } as const; export type OrderStatusEnum = typeof OrderStatusEnum[keyof typeof OrderStatusEnum]; -export const OrderPropertyValidationAttributesMap: { - [property: string]: { - dataType?: string, - required?: boolean, - maxLength?: number, - minLength?: number, - pattern?: string, - maximum?: number, - exclusiveMaximum?: boolean, - minimum?: number, - exclusiveMinimum?: boolean, - multipleOf?: number, - maxItems?: number, - minItems?: number, - uniqueItems?: boolean - } -} = { -} +export const OrderPropertyValidationAttributesMap = { +} satisfies Record; export const OrderAdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = { maxProperties: 10, diff --git a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Pet.ts b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Pet.ts index cde0e7b42de0..bebf27bf2de4 100644 --- a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Pet.ts +++ b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Pet.ts @@ -28,6 +28,7 @@ import { TagToJSONTyped, } from './Tag'; +import type { PropertyValidationAttributes } from '../runtime'; /** * A pet for sale in the pet store * @export @@ -72,23 +73,7 @@ export const PetStatusEnum = { } as const; export type PetStatusEnum = typeof PetStatusEnum[keyof typeof PetStatusEnum]; -export const PetPropertyValidationAttributesMap: { - [property: string]: { - dataType?: string, - required?: boolean, - maxLength?: number, - minLength?: number, - pattern?: string, - maximum?: number, - exclusiveMaximum?: boolean, - minimum?: number, - exclusiveMinimum?: boolean, - multipleOf?: number, - maxItems?: number, - minItems?: number, - uniqueItems?: boolean - } -} = { +export const PetPropertyValidationAttributesMap = { photoUrls: { dataType: "Set", required: true, @@ -96,7 +81,7 @@ export const PetPropertyValidationAttributesMap: { minItems: 1, uniqueItems: true, }, -} +} satisfies Record; /** diff --git a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Tag.ts b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Tag.ts index b6222ddd699c..21ebcef9eae0 100644 --- a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Tag.ts +++ b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/Tag.ts @@ -13,6 +13,7 @@ */ import { mapValues } from '../runtime'; +import type { PropertyValidationAttributes } from '../runtime'; /** * A tag for a pet * @export @@ -28,24 +29,8 @@ export interface Tag { */ name?: string; } -export const TagPropertyValidationAttributesMap: { - [property: string]: { - dataType?: string, - required?: boolean, - maxLength?: number, - minLength?: number, - pattern?: string, - maximum?: number, - exclusiveMaximum?: boolean, - minimum?: number, - exclusiveMinimum?: boolean, - multipleOf?: number, - maxItems?: number, - minItems?: number, - uniqueItems?: boolean - } -} = { -} +export const TagPropertyValidationAttributesMap = { +} satisfies Record; /** diff --git a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/User.ts b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/User.ts index af745c1ee60e..132241b837ac 100644 --- a/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/User.ts +++ b/samples/client/petstore/typescript-fetch/builds/validation-attributes/models/User.ts @@ -13,6 +13,7 @@ */ import { mapValues } from '../runtime'; +import type { PropertyValidationAttributes } from '../runtime'; /** * A User who is purchasing from the pet store * @export @@ -56,23 +57,7 @@ export interface User { */ userStatus?: number; } -export const UserPropertyValidationAttributesMap: { - [property: string]: { - dataType?: string, - required?: boolean, - maxLength?: number, - minLength?: number, - pattern?: string, - maximum?: number, - exclusiveMaximum?: boolean, - minimum?: number, - exclusiveMinimum?: boolean, - multipleOf?: number, - maxItems?: number, - minItems?: number, - uniqueItems?: boolean - } -} = { +export const UserPropertyValidationAttributesMap = { password: { dataType: "string", maxLength: 256, @@ -90,7 +75,7 @@ export const UserPropertyValidationAttributesMap: { exclusiveMinimum: true, multipleOf: 10, }, -} +} satisfies Record; /** diff --git a/samples/client/petstore/typescript-fetch/builds/validation-attributes/runtime.ts b/samples/client/petstore/typescript-fetch/builds/validation-attributes/runtime.ts index 3389e4beb1cc..9a48c04ffde9 100644 --- a/samples/client/petstore/typescript-fetch/builds/validation-attributes/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/validation-attributes/runtime.ts @@ -452,3 +452,19 @@ export class TextApiResponse { return await this.raw.text(); }; } + +export interface PropertyValidationAttributes { + dataType?: string, + required?: boolean, + maxLength?: number, + minLength?: number, + pattern?: string, + maximum?: number, + exclusiveMaximum?: boolean, + minimum?: number, + exclusiveMinimum?: boolean, + multipleOf?: number, + maxItems?: number, + minItems?: number, + uniqueItems?: boolean +}