diff --git a/src/interfaces.ts b/src/interfaces.ts index 31aa98a3..ac6e182b 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -9,6 +9,7 @@ export type ConvertV2ToV3Options = { useChannelIdExtension?: boolean; convertServerComponents?: boolean; convertChannelComponents?: boolean; + failOnParameterReference?: boolean; } export type ConvertOptions = { v2tov3?: ConvertV2ToV3Options; diff --git a/src/third-version.ts b/src/third-version.ts index 09ea22ed..6ec6393f 100644 --- a/src/third-version.ts +++ b/src/third-version.ts @@ -18,6 +18,7 @@ function from__2_6_0__to__3_0_0(asyncapi: AsyncAPIDocument, options: ConvertOpti useChannelIdExtension: true, convertServerComponents: true, convertChannelComponents: true, + failOnParameterReference: false, ...(options.v2tov3 ?? {}), } as RequiredConvertV2ToV3Options; v2tov3Options.idGenerator = v2tov3Options.idGenerator || idGeneratorFactory(v2tov3Options); @@ -158,7 +159,7 @@ function convertChannelObjects(channels: Record, asyncapi: AsyncAPI //Change parameter formats if (isPlainObject(channel.parameters)) { - channel.parameters = convertParameters(channel.parameters); + channel.parameters = convertParameters(channel.parameters, options); } const operations: Record = {}; @@ -363,16 +364,16 @@ function convertComponents(asyncapi: AsyncAPIDocument, options: RequiredConvertV } if (isPlainObject(components.parameters)) { - components.parameters = convertParameters(components.parameters); + components.parameters = convertParameters(components.parameters, options); } } /** * Convert all parameters to the new v3 format */ -function convertParameters(parameters: Record): Record { +function convertParameters(parameters: Record, options: RequiredConvertV2ToV3Options): Record { const newParameters: Record = {}; Object.entries(parameters).forEach(([name, parameter]) => { - newParameters[name] = convertParameter(parameter); + newParameters[name] = convertParameter(parameter, options); }); return newParameters; } @@ -383,7 +384,7 @@ function convertParameters(parameters: Record): Record * * Does not include extensions from schema. */ -function convertParameter(parameter: any): any { +function convertParameter(parameter: any, options: RequiredConvertV2ToV3Options): any { const ref = parameter['$ref'] ?? null; if(ref !== null) { return { @@ -392,7 +393,11 @@ function convertParameter(parameter: any): any { } if(parameter.schema?.$ref) { - console.error('Could not convert parameter object because the `.schema` property was a reference. This have to be changed manually if you want any of the properties included. The reference was ' + parameter.schema?.$ref); + const errorMessage = 'Could not convert parameter object because the `.schema` property was a reference. This have to be changed manually if you want any of the properties included. The reference was ' + parameter.schema?.$ref; + if(options.failOnParameterReference === true) { + throw new Error(errorMessage); + } + console.error(errorMessage); } const enumValues = parameter.schema?.enum ?? null; @@ -416,6 +421,7 @@ function convertParameter(parameter: any): any { location === null ? null : {location} ); } + /** * Convert `channels`, `servers` and `securitySchemes` in components. */ diff --git a/test/second-to-third-version.spec.ts b/test/second-to-third-version.spec.ts index 5304e262..44a00c64 100644 --- a/test/second-to-third-version.spec.ts +++ b/test/second-to-third-version.spec.ts @@ -32,4 +32,16 @@ describe('convert() - 2.X.X to 3.X.X versions', () => { const result = convert(input, '3.0.0'); assertResults(output, result); }); + + it('should throw exception when parameter encountered with failOnParameterReference=true', () => { + const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.6.0', 'for-3.0.0-with-reference-parameter.yml'), 'utf8'); + expect(() => {convert(input, '3.0.0', {v2tov3: {failOnParameterReference: true}})}).toThrowError('Could not convert parameter object because the `.schema` property was a reference. This have to be changed manually if you want any of the properties included. The reference was #/components/schemas/sentAt'); + }); + + it('should handle parameter object', () => { + const input = fs.readFileSync(path.resolve(__dirname, 'input', '2.6.0', 'for-3.0.0-with-reference-parameter.yml'), 'utf8'); + const output = fs.readFileSync(path.resolve(__dirname, 'output', '3.0.0', 'from-2.6.0-with-reference-parameter.yml'), 'utf8'); + const result = convert(input, '3.0.0', {v2tov3: {failOnParameterReference: false}}); + assertResults(output, result); + }); });