diff --git a/src/third-version.ts b/src/third-version.ts index f962c50f..9eeb33bf 100644 --- a/src/third-version.ts +++ b/src/third-version.ts @@ -159,7 +159,7 @@ function convertChannelObjects(channels: Record, asyncapi: AsyncAPI //Change parameter formats if (isPlainObject(channel.parameters)) { - channel.parameters = convertParameters(channel.parameters, options); + channel.parameters = convertParameters(channel.parameters); } const operations: Record = {}; @@ -364,19 +364,20 @@ function convertComponents(asyncapi: AsyncAPIDocument, options: RequiredConvertV } if (isPlainObject(components.parameters)) { - components.parameters = convertParameters(components.parameters, options); + components.parameters = convertParameters(components.parameters); } } /** * Convert all parameters to the new v3 format */ -function convertParameters(parameters: Record, options: RequiredConvertV2ToV3Options): Record { +function convertParameters(parameters: Record): Record { const newParameters: Record = {}; Object.entries(parameters).forEach(([name, parameter]) => { - newParameters[name] = convertParameter(parameter, options); + newParameters[name] = convertParameter(parameter); }); return newParameters; } + /** * Convert the old v2 parameter object to v3. * @@ -384,7 +385,7 @@ function convertParameters(parameters: Record, options: RequiredCon * * Does not include extensions from schema. */ -function convertParameter(parameter: any, options: RequiredConvertV2ToV3Options): any { +function convertParameter(parameter: any): any { const ref = parameter['$ref'] ?? null; if(ref !== null) { return { @@ -393,14 +394,17 @@ function convertParameter(parameter: any, options: RequiredConvertV2ToV3Options) } 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); + console.warn('Could not convert parameter object because the `.schema` property was a reference.\nThis have to be changed manually if you want any of the properties included, it will be converted to a default parameter. The reference was ' + parameter.schema?.$ref); } const enumValues = parameter.schema?.enum ?? null; + const constValue = parameter.schema?.const ?? null; const defaultValues = parameter.schema?.default ?? null; const description = parameter.description ?? parameter.schema?.description ?? null; const examples = parameter.schema?.examples ?? null; const location = parameter.location ?? null; + + reportUnsupportedParameterValues(parameter.schema); //Make sure we keep parameter extensions const v2ParameterObjectProperties = ["location", "schema", "description"]; @@ -411,6 +415,7 @@ function convertParameter(parameter: any, options: RequiredConvertV2ToV3Options) //Return the new v3 parameter object return {...v2ParameterObjectExtensions, ...(enumValues === null ? null : {enum: enumValues}), + ...(constValue === null ? null : {enum: [constValue]}), ...(defaultValues === null ? null : {default: defaultValues}), ...(description === null ? null : {description}), ...(examples === null ? null : {examples}), @@ -418,6 +423,22 @@ function convertParameter(parameter: any, options: RequiredConvertV2ToV3Options) }; } +/** + * This function makes sure we complain if a parameter schema uses now unsupported properties + */ +function reportUnsupportedParameterValues(schema: any) { + if(schema === undefined) return; + const excessProperties = Object.entries(schema).filter((([propertyName,]) => { + return !['$ref', 'enum', 'const', 'default', 'examples', 'description'].includes(propertyName); + })); + if(excessProperties.length > 0) { + const listOfProperties = excessProperties.map(([propertyName, property]) => { + return `- schema.${propertyName} with value: ${JSON.stringify(property)} are no longer supported` + }) + console.warn(`Found properties in parameter schema that are no longer supported and will be ignored by the converter.\n${listOfProperties.join('\n')}`); + } +} + /** * Convert `channels`, `servers` and `securitySchemes` in components. */ diff --git a/test/input/2.6.0/for-3.0.0-with-reference-parameter.yml b/test/input/2.6.0/for-3.0.0-with-reference-parameter.yml index c023e104..d82767a1 100644 --- a/test/input/2.6.0/for-3.0.0-with-reference-parameter.yml +++ b/test/input/2.6.0/for-3.0.0-with-reference-parameter.yml @@ -5,11 +5,14 @@ info: version: 1.0.1 channels: - 'lightingMeasured/{parameter}': + 'lightingMeasured/{parameter}/{parameter2}': parameters: parameter: schema: $ref: '#/components/schemas/sentAt' + parameter2: + schema: + pattern: test publish: operationId: lightMeasured message: diff --git a/test/output/3.0.0/from-2.6.0-with-reference-parameter.yml b/test/output/3.0.0/from-2.6.0-with-reference-parameter.yml index c6039177..4e885347 100644 --- a/test/output/3.0.0/from-2.6.0-with-reference-parameter.yml +++ b/test/output/3.0.0/from-2.6.0-with-reference-parameter.yml @@ -3,8 +3,8 @@ info: title: AsyncAPI Sample App version: 1.0.1 channels: - 'lightingMeasured/{parameter}': - address: 'lightingMeasured/{parameter}' + 'lightingMeasured/{parameter}/{parameter2}': + address: 'lightingMeasured/{parameter}/{parameter2}' messages: lightMeasured.message: payload: @@ -14,17 +14,18 @@ channels: type: string parameters: parameter: {} + parameter2: {} operations: lightMeasured: action: receive channel: - $ref: '#/channels/lightingMeasured~1{parameter}' + $ref: '#/channels/lightingMeasured~1{parameter}~1{parameter2}' messages: - $ref: >- - #/channels/lightingMeasured~1{parameter}/messages/lightMeasured.message + #/channels/lightingMeasured~1{parameter}~1{parameter2}/messages/lightMeasured.message components: schemas: sentAt: type: string format: date-time - description: Date and time when the message was sent. \ No newline at end of file + description: Date and time when the message was sent.