diff --git a/package-lock.json b/package-lock.json index dc6d41b4..b4048648 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "dependencies": { "@asyncapi/avro-schema-parser": "^3.0.24", - "@asyncapi/modelina": "^5.7.2", + "@asyncapi/modelina": "^6.0.0-next.3", "@asyncapi/openapi-schema-parser": "^3.0.24", "@asyncapi/parser": "^3.4.0", "@asyncapi/protobuf-schema-parser": "^3.5.1", @@ -169,9 +169,9 @@ } }, "node_modules/@asyncapi/modelina": { - "version": "5.7.2", - "resolved": "https://registry.npmjs.org/@asyncapi/modelina/-/modelina-5.7.2.tgz", - "integrity": "sha512-TWVYLYtFi/atM5KUN+EKLvnRWB3WFeD/kwwkHAVPTj08xM4VHCxJ2kRUOTvu1j3YNpx7KinG2GPTMraxJi1vVA==", + "version": "6.0.0-next.3", + "resolved": "https://registry.npmjs.org/@asyncapi/modelina/-/modelina-6.0.0-next.3.tgz", + "integrity": "sha512-1ijgYT2/FA8Qq222YJe+exXrvUIEf8DJgjl6f/kCyP7v3tB/2NL49/CmZTO1qwduIcn236ci6HVcNdIoPYyN/A==", "license": "Apache-2.0", "dependencies": { "@apidevtools/json-schema-ref-parser": "^11.1.0", @@ -180,6 +180,7 @@ "@asyncapi/parser": "^3.4.0", "alterschema": "^1.1.2", "change-case": "^4.1.2", + "fast-xml-parser": "^5.3.0", "js-yaml": "^4.1.0", "openapi-types": "^12.1.3", "typescript-json-schema": "^0.58.1" @@ -207,6 +208,36 @@ "tslib": "^2.0.3" } }, + "node_modules/@asyncapi/modelina/node_modules/fast-xml-parser": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-5.3.1.tgz", + "integrity": "sha512-jbNkWiv2Ec1A7wuuxk0br0d0aTMUtQ4IkL+l/i1r9PRf6pLXjDgsBsWwO+UyczmQlnehi4Tbc8/KIvxGQe+I/A==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/NaturalIntelligence" + } + ], + "license": "MIT", + "dependencies": { + "strnum": "^2.1.0" + }, + "bin": { + "fxparser": "src/cli/cli.js" + } + }, + "node_modules/@asyncapi/modelina/node_modules/strnum": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/strnum/-/strnum-2.1.1.tgz", + "integrity": "sha512-7ZvoFTiCnGxBtDqJ//Cu6fWtZtc7Y3x+QOirG15wztbdngGSkht27o2pyGWrVy0b4WAy3jbKmnoK6g5VlVNUUw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/NaturalIntelligence" + } + ], + "license": "MIT" + }, "node_modules/@asyncapi/modelina/node_modules/tslib": { "version": "2.6.2", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", diff --git a/package.json b/package.json index a340df54..9ff3dd14 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "bugs": "https://github.com/the-codegen-project/cli/issues", "dependencies": { "@asyncapi/avro-schema-parser": "^3.0.24", - "@asyncapi/modelina": "^5.7.2", + "@asyncapi/modelina": "^6.0.0-next.3", "@asyncapi/openapi-schema-parser": "^3.0.24", "@asyncapi/parser": "^3.4.0", "@asyncapi/protobuf-schema-parser": "^3.5.1", diff --git a/src/codegen/inputs/asyncapi/generators/parameters.ts b/src/codegen/inputs/asyncapi/generators/parameters.ts index 4c8486d6..49690265 100644 --- a/src/codegen/inputs/asyncapi/generators/parameters.ts +++ b/src/codegen/inputs/asyncapi/generators/parameters.ts @@ -75,13 +75,14 @@ export function unwrap(channelParameters: ConstrainedObjectModel) { if(${variableName} && ${variableName} !== '') { parameters.${parameter.propertyName} = ${variableName} as any } else { - throw new Error(\`Parameter: '${parameter.propertyName}' is not valid. Abort! \`) + throw new Error(\`Parameter: '${parameter.propertyName}' is not valid in ${channelParameters.name}. Aborting parameter extracting! \`) }`; } ); - const parameterInitializer = Object.values(channelParameters.properties).map( - (parameter) => { + const parameterInitializer = Object.values(channelParameters.properties) + .filter(parameter => parameter.property.options.const?.value === undefined) + .map((parameter) => { if (parameter.property.options.isNullable) { return `${parameter.propertyName}: null`; } diff --git a/src/codegen/inputs/asyncapi/generators/payloads.ts b/src/codegen/inputs/asyncapi/generators/payloads.ts index 1d5f700f..3e42ba7e 100644 --- a/src/codegen/inputs/asyncapi/generators/payloads.ts +++ b/src/codegen/inputs/asyncapi/generators/payloads.ts @@ -15,8 +15,7 @@ export interface ProcessedPayloadSchemaData { operationPayloads: Record; otherPayloads: {schema: any; schemaId: string}[]; } -const processor = new AsyncAPIInputProcessor(); -// AsyncAPI input processor + // eslint-disable-next-line sonarjs/cognitive-complexity export async function processAsyncAPIPayloads( asyncapiDocument: AsyncAPIDocumentInterface diff --git a/src/codegen/types.ts b/src/codegen/types.ts index aa19dc80..4af533cf 100644 --- a/src/codegen/types.ts +++ b/src/codegen/types.ts @@ -68,7 +68,7 @@ export interface LoadArgument { export type SupportedLanguages = 'typescript'; export interface GenericCodegenContext { dependencyOutputs: Record; - config: TheCodegenConfiguration; + config?: TheCodegenConfiguration; } export const zodAsyncAPITypeScriptGenerators = z.discriminatedUnion('preset', [ diff --git a/test/codegen/generators/typescript/__snapshots__/channels.spec.ts.snap b/test/codegen/generators/typescript/__snapshots__/channels.spec.ts.snap index ed01fe76..52339864 100644 --- a/test/codegen/generators/typescript/__snapshots__/channels.spec.ts.snap +++ b/test/codegen/generators/typescript/__snapshots__/channels.spec.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`channels typescript should work with basic AsyncAPI inputs 1`] = ` "import * as TestPayloadModelModule from './../../../../TestPayloadModel'; diff --git a/test/codegen/generators/typescript/__snapshots__/headers.spec.ts.snap b/test/codegen/generators/typescript/__snapshots__/headers.spec.ts.snap index 557baf3c..0091889a 100644 --- a/test/codegen/generators/typescript/__snapshots__/headers.spec.ts.snap +++ b/test/codegen/generators/typescript/__snapshots__/headers.spec.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`headers typescript should work with OpenAPI 2.0 inputs 1`] = ` "import {Ajv, Options as AjvOptions, ErrorObject, ValidateFunction} from 'ajv'; diff --git a/test/codegen/generators/typescript/__snapshots__/parameters.spec.ts.snap b/test/codegen/generators/typescript/__snapshots__/parameters.spec.ts.snap index 218b1da1..df400c7e 100644 --- a/test/codegen/generators/typescript/__snapshots__/parameters.spec.ts.snap +++ b/test/codegen/generators/typescript/__snapshots__/parameters.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`payloads typescript asyncapi should work with AsyncAPI that contains parameters 1`] = ` +exports[`parameters typescript asyncapi should work with AsyncAPI that contains parameters 1`] = ` " class SingleParameterParameters { private _firstParameter: string; @@ -33,7 +33,7 @@ class SingleParameterParameters { if(firstParameterMatch && firstParameterMatch !== '') { parameters.firstParameter = firstParameterMatch as any } else { - throw new Error(\`Parameter: 'firstParameter' is not valid. Abort! \`) + throw new Error(\`Parameter: 'firstParameter' is not valid in SingleParameterParameters. Aborting parameter extracting! \`) } } else { throw new Error(\`Unable to find parameters in channel/topic, topic was \${channel}\`) @@ -44,9 +44,162 @@ class SingleParameterParameters { export { SingleParameterParameters };" `; -exports[`payloads typescript asyncapi should work with AsyncAPI that contains parameters 2`] = `undefined`; +exports[`parameters typescript asyncapi should work with AsyncAPI that contains parameters 2`] = ` +"import {EnumParameter} from './EnumParameter'; +class MultipleParameterParameters { + private _enumParameter: EnumParameter; + private _constParameter: string; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 1`] = ` + constructor(input: { + enumParameter: EnumParameter, + constParameter: string, + }) { + this._enumParameter = input.enumParameter; + this._constParameter = input.constParameter; + } + + get enumParameter(): EnumParameter { return this._enumParameter; } + set enumParameter(enumParameter: EnumParameter) { this._enumParameter = enumParameter; } + + get constParameter(): string { return this._constParameter; } + set constParameter(constParameter: string) { this._constParameter = constParameter; } + + + /** + * Realize the channel/topic with the parameters added to this class. + */ + public getChannelWithParameters(channel: string) { + channel = channel.replace(/\\{enum_parameter\\}/g, this.enumParameter); + channel = channel.replace(/\\{const_parameter\\}/g, this.constParameter); + return channel; + } + + public static createFromChannel(msgSubject: string, channel: string, regex: RegExp): MultipleParameterParameters { + const parameters = new MultipleParameterParameters({enumParameter: "a", constParameter: ''}); + const match = msgSubject.match(regex); + const sequentialParameters: string[] = channel.match(/\\{(\\w+)\\}/g) || []; + + if (match) { + const enumParameterMatch = match[sequentialParameters.indexOf('{enum_parameter}')+1]; + if(enumParameterMatch && enumParameterMatch !== '') { + parameters.enumParameter = enumParameterMatch as any + } else { + throw new Error(\`Parameter: 'enumParameter' is not valid in MultipleParameterParameters. Aborting parameter extracting! \`) + } + const constParameterMatch = match[sequentialParameters.indexOf('{const_parameter}')+1]; + if(constParameterMatch && constParameterMatch !== '') { + parameters.constParameter = constParameterMatch as any + } else { + throw new Error(\`Parameter: 'constParameter' is not valid in MultipleParameterParameters. Aborting parameter extracting! \`) + } + } else { + throw new Error(\`Unable to find parameters in channel/topic, topic was \${channel}\`) + } + return parameters; + } +} +export { MultipleParameterParameters };" +`; + +exports[`parameters typescript asyncapi should work with AsyncAPI v2 that contains parameters and const parameters 1`] = ` +" +class SingleParameterParameters { + private _firstParameter: string; + + constructor(input: { + firstParameter: string, + }) { + this._firstParameter = input.firstParameter; + } + + get firstParameter(): string { return this._firstParameter; } + set firstParameter(firstParameter: string) { this._firstParameter = firstParameter; } + + + /** + * Realize the channel/topic with the parameters added to this class. + */ + public getChannelWithParameters(channel: string) { + channel = channel.replace(/\\{first_parameter\\}/g, this.firstParameter); + return channel; + } + + public static createFromChannel(msgSubject: string, channel: string, regex: RegExp): SingleParameterParameters { + const parameters = new SingleParameterParameters({firstParameter: ''}); + const match = msgSubject.match(regex); + const sequentialParameters: string[] = channel.match(/\\{(\\w+)\\}/g) || []; + + if (match) { + const firstParameterMatch = match[sequentialParameters.indexOf('{first_parameter}')+1]; + if(firstParameterMatch && firstParameterMatch !== '') { + parameters.firstParameter = firstParameterMatch as any + } else { + throw new Error(\`Parameter: 'firstParameter' is not valid in SingleParameterParameters. Aborting parameter extracting! \`) + } + } else { + throw new Error(\`Unable to find parameters in channel/topic, topic was \${channel}\`) + } + return parameters; + } +} +export { SingleParameterParameters };" +`; + +exports[`parameters typescript asyncapi should work with AsyncAPI v2 that contains parameters and const parameters 2`] = ` +"import {EnumParameter} from './EnumParameter'; +class MultipleParameterParameters { + private _enumParameter: EnumParameter; + private _constParameter: 'test' = 'test'; + + constructor(input: { + enumParameter: EnumParameter, + }) { + this._enumParameter = input.enumParameter; + } + + get enumParameter(): EnumParameter { return this._enumParameter; } + set enumParameter(enumParameter: EnumParameter) { this._enumParameter = enumParameter; } + + get constParameter(): 'test' { return this._constParameter; } + + + /** + * Realize the channel/topic with the parameters added to this class. + */ + public getChannelWithParameters(channel: string) { + channel = channel.replace(/\\{enum_parameter\\}/g, this.enumParameter); + channel = channel.replace(/\\{const_parameter\\}/g, this.constParameter); + return channel; + } + + public static createFromChannel(msgSubject: string, channel: string, regex: RegExp): MultipleParameterParameters { + const parameters = new MultipleParameterParameters({enumParameter: "a"}); + const match = msgSubject.match(regex); + const sequentialParameters: string[] = channel.match(/\\{(\\w+)\\}/g) || []; + + if (match) { + const enumParameterMatch = match[sequentialParameters.indexOf('{enum_parameter}')+1]; + if(enumParameterMatch && enumParameterMatch !== '') { + parameters.enumParameter = enumParameterMatch as any + } else { + throw new Error(\`Parameter: 'enumParameter' is not valid in MultipleParameterParameters. Aborting parameter extracting! \`) + } + const constParameterMatch = match[sequentialParameters.indexOf('{const_parameter}')+1]; + if(constParameterMatch && constParameterMatch !== '') { + parameters.constParameter = constParameterMatch as any + } else { + throw new Error(\`Parameter: 'constParameter' is not valid in MultipleParameterParameters. Aborting parameter extracting! \`) + } + } else { + throw new Error(\`Unable to find parameters in channel/topic, topic was \${channel}\`) + } + return parameters; + } +} +export { MultipleParameterParameters };" +`; + +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 1`] = ` " class DeleteOrderParameters { private _orderId: number; @@ -164,7 +317,7 @@ class DeleteOrderParameters { export { DeleteOrderParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 2`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 2`] = ` " class DeletePetParameters { private _petId: number; @@ -282,7 +435,7 @@ class DeletePetParameters { export { DeletePetParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 3`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 3`] = ` " class DeleteUserParameters { private _username: string; @@ -400,7 +553,7 @@ class DeleteUserParameters { export { DeleteUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 4`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 4`] = ` " class FindPetsByStatusParameters { private _status: any[]; @@ -509,7 +662,7 @@ class FindPetsByStatusParameters { export { FindPetsByStatusParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 5`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 5`] = ` " class FindPetsByTagsParameters { private _tags: any[]; @@ -618,7 +771,7 @@ class FindPetsByTagsParameters { export { FindPetsByTagsParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 6`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 6`] = ` " class GetOrderByIdParameters { private _orderId: number; @@ -736,7 +889,7 @@ class GetOrderByIdParameters { export { GetOrderByIdParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 7`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 7`] = ` " class GetPetByIdParameters { private _petId: number; @@ -854,7 +1007,7 @@ class GetPetByIdParameters { export { GetPetByIdParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 8`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 8`] = ` " class GetUserByNameParameters { private _username: string; @@ -972,7 +1125,7 @@ class GetUserByNameParameters { export { GetUserByNameParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 9`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 9`] = ` " class LoginUserParameters { private _username: string; @@ -1109,7 +1262,7 @@ class LoginUserParameters { export { LoginUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 10`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 10`] = ` " class UpdatePetWithFormParameters { private _petId: number; @@ -1227,7 +1380,7 @@ class UpdatePetWithFormParameters { export { UpdatePetWithFormParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 11`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 11`] = ` " class UpdateUserParameters { private _username: string; @@ -1345,7 +1498,7 @@ class UpdateUserParameters { export { UpdateUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 2.0 that contains parameters 12`] = ` +exports[`parameters typescript openapi should work with OpenAPI 2.0 that contains parameters 12`] = ` " class UploadFileParameters { private _petId: number; @@ -1463,7 +1616,7 @@ class UploadFileParameters { export { UploadFileParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 1`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 1`] = ` " class DeleteOrderParameters { private _orderId: string; @@ -1581,7 +1734,7 @@ class DeleteOrderParameters { export { DeleteOrderParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 2`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 2`] = ` " class DeletePetParameters { private _petId: number; @@ -1699,7 +1852,7 @@ class DeletePetParameters { export { DeletePetParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 3`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 3`] = ` " class DeleteUserParameters { private _username: string; @@ -1817,7 +1970,7 @@ class DeleteUserParameters { export { DeleteUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 4`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 4`] = ` "import {StatusItem} from './StatusItem'; class FindPetsByStatusParameters { private _status: StatusItem[]; @@ -1928,7 +2081,7 @@ class FindPetsByStatusParameters { export { FindPetsByStatusParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 5`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 5`] = ` " class FindPetsByTagsParameters { private _tags: string[]; @@ -2039,7 +2192,7 @@ class FindPetsByTagsParameters { export { FindPetsByTagsParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 6`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 6`] = ` " class GetOrderByIdParameters { private _orderId: number; @@ -2157,7 +2310,7 @@ class GetOrderByIdParameters { export { GetOrderByIdParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 7`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 7`] = ` " class GetPetByIdParameters { private _petId: number; @@ -2275,7 +2428,7 @@ class GetPetByIdParameters { export { GetPetByIdParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 8`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 8`] = ` " class GetUserByNameParameters { private _username: string; @@ -2393,7 +2546,7 @@ class GetUserByNameParameters { export { GetUserByNameParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 9`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 9`] = ` " class LoginUserParameters { private _username: string; @@ -2530,7 +2683,7 @@ class LoginUserParameters { export { LoginUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 10`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 10`] = ` " class UpdatePetWithFormParameters { private _petId: number; @@ -2648,7 +2801,7 @@ class UpdatePetWithFormParameters { export { UpdatePetWithFormParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 11`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 11`] = ` " class UpdateUserParameters { private _username: string; @@ -2766,7 +2919,7 @@ class UpdateUserParameters { export { UpdateUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.0 that contains parameters 12`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.0 that contains parameters 12`] = ` " class UploadFileParameters { private _petId: number; @@ -2884,7 +3037,7 @@ class UploadFileParameters { export { UploadFileParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 1`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 1`] = ` " class DeleteOrderParameters { private _orderId: string; @@ -3002,7 +3155,7 @@ class DeleteOrderParameters { export { DeleteOrderParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 2`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 2`] = ` " class DeletePetParameters { private _petId: number; @@ -3120,7 +3273,7 @@ class DeletePetParameters { export { DeletePetParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 3`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 3`] = ` " class DeleteUserParameters { private _username: string; @@ -3238,7 +3391,7 @@ class DeleteUserParameters { export { DeleteUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 4`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 4`] = ` "import {StatusItem} from './StatusItem'; class FindPetsByStatusParameters { private _status: StatusItem[]; @@ -3349,7 +3502,7 @@ class FindPetsByStatusParameters { export { FindPetsByStatusParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 5`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 5`] = ` " class FindPetsByTagsParameters { private _tags: string[]; @@ -3460,7 +3613,7 @@ class FindPetsByTagsParameters { export { FindPetsByTagsParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 6`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 6`] = ` " class GetOrderByIdParameters { private _orderId: number; @@ -3578,7 +3731,7 @@ class GetOrderByIdParameters { export { GetOrderByIdParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 7`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 7`] = ` " class GetPetByIdParameters { private _petId: number; @@ -3696,7 +3849,7 @@ class GetPetByIdParameters { export { GetPetByIdParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 8`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 8`] = ` " class GetUserByNameParameters { private _username: string; @@ -3814,7 +3967,7 @@ class GetUserByNameParameters { export { GetUserByNameParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 9`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 9`] = ` " class LoginUserParameters { private _username: string; @@ -3951,7 +4104,7 @@ class LoginUserParameters { export { LoginUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 10`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 10`] = ` " class UpdatePetWithFormParameters { private _petId: number; @@ -4069,7 +4222,7 @@ class UpdatePetWithFormParameters { export { UpdatePetWithFormParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 11`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 11`] = ` " class UpdateUserParameters { private _username: string; @@ -4187,7 +4340,7 @@ class UpdateUserParameters { export { UpdateUserParameters };" `; -exports[`payloads typescript openapi should work with OpenAPI 3.1 that contains parameters 12`] = ` +exports[`parameters typescript openapi should work with OpenAPI 3.1 that contains parameters 12`] = ` " class UploadFileParameters { private _petId: number; diff --git a/test/codegen/generators/typescript/__snapshots__/payload.spec.ts.snap b/test/codegen/generators/typescript/__snapshots__/payload.spec.ts.snap index f634d244..124f2247 100644 --- a/test/codegen/generators/typescript/__snapshots__/payload.spec.ts.snap +++ b/test/codegen/generators/typescript/__snapshots__/payload.spec.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`payloads typescript should not render validation functions 1`] = ` "import {SimpleObject} from './SimpleObject'; diff --git a/test/codegen/generators/typescript/__snapshots__/types.spec.ts.snap b/test/codegen/generators/typescript/__snapshots__/types.spec.ts.snap index 04a6cdf3..5580f4c0 100644 --- a/test/codegen/generators/typescript/__snapshots__/types.spec.ts.snap +++ b/test/codegen/generators/typescript/__snapshots__/types.spec.ts.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`types typescript should work with OpenAPI 2.0 inputs 1`] = ` "export type Paths = '/pet/{petId}/uploadImage' | '/pet' | '/pet/findByStatus' | '/pet/findByTags' | '/pet/{petId}' | '/store/inventory' | '/store/order' | '/store/order/{orderId}' | '/user/createWithList' | '/user/{username}' | '/user/login' | '/user/logout' | '/user/createWithArray' | '/user'; diff --git a/test/codegen/generators/typescript/parameters.spec.ts b/test/codegen/generators/typescript/parameters.spec.ts index e5aa2d36..de161cb0 100644 --- a/test/codegen/generators/typescript/parameters.spec.ts +++ b/test/codegen/generators/typescript/parameters.spec.ts @@ -3,7 +3,7 @@ import { generateTypescriptParameters } from "../../../../src/codegen/generators import { loadAsyncapiDocument } from "../../../../src/codegen/inputs/asyncapi"; import { loadOpenapiDocument } from "../../../../src/codegen/inputs/openapi"; -describe('payloads', () => { +describe('parameters', () => { describe('typescript', () => { describe('asyncapi', () => { it('should work with AsyncAPI that contains parameters', async () => { @@ -23,7 +23,26 @@ describe('payloads', () => { dependencyOutputs: { } }); expect(renderedContent.channelModels['single_parameter']?.result).toMatchSnapshot(); - expect(renderedContent.channelModels['second_parameter']?.result).toMatchSnapshot(); + expect(renderedContent.channelModels['multiple_parameter']?.result).toMatchSnapshot(); + }); + it('should work with AsyncAPI v2 that contains parameters and const parameters', async () => { + const parsedAsyncAPIDocument = await loadAsyncapiDocument(path.resolve(__dirname, '../../../configs/parameters-v2.yaml')); + + const renderedContent = await generateTypescriptParameters({ + generator: { + serializationType: 'json', + outputPath: path.resolve(__dirname, './output'), + preset: 'parameters', + language: 'typescript', + dependencies: [], + id: 'test' + }, + inputType: 'asyncapi', + asyncapiDocument: parsedAsyncAPIDocument, + dependencyOutputs: { } + }); + expect(renderedContent.channelModels['single_parameter']?.result).toMatchSnapshot(); + expect(renderedContent.channelModels['multiple_parameter']?.result).toMatchSnapshot(); }); it('should work with no channels', async () => { const parsedAsyncAPIDocument = await loadAsyncapiDocument(path.resolve(__dirname, '../../../configs/parameters-no-channels.yaml')); diff --git a/test/configs/parameters-v2.yaml b/test/configs/parameters-v2.yaml new file mode 100644 index 00000000..032eb0f4 --- /dev/null +++ b/test/configs/parameters-v2.yaml @@ -0,0 +1,15 @@ +asyncapi: 2.6.0 +info: + title: Account Service + version: 1.0.0 + description: This service is in charge of processing user signups +channels: + single_parameter: + address: single/{first_parameter} + parameters: + first_parameter: { schema: { type: 'string' } } + multiple_parameter: + address: multiple/{enum_parameter}/{const_parameter} + parameters: + enum_parameter: { schema: { type: 'string', enum: [ 'a', 'b', 'c' ] } } + const_parameter: { schema: { type: 'string', const: 'test' } } \ No newline at end of file diff --git a/test/configs/parameters.yaml b/test/configs/parameters.yaml index f4211dda..4d19d3c4 100644 --- a/test/configs/parameters.yaml +++ b/test/configs/parameters.yaml @@ -8,8 +8,8 @@ channels: address: single/{first_parameter} parameters: first_parameter: {} - double_parameter: - address: double/{first_parameter}/{second_parameter} + multiple_parameter: + address: multiple/{enum_parameter}/{const_parameter} parameters: - first_parameter: {} - second_parameter: {} \ No newline at end of file + enum_parameter: { enum: [ 'a', 'b', 'c' ] } + const_parameter: { const: 'test' } \ No newline at end of file