Skip to content

Commit

Permalink
fix parameter conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 11, 2023
1 parent a7efa67 commit 18ee956
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
33 changes: 27 additions & 6 deletions src/third-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function convertChannelObjects(channels: Record<string, any>, asyncapi: AsyncAPI

//Change parameter formats
if (isPlainObject(channel.parameters)) {
channel.parameters = convertParameters(channel.parameters, options);
channel.parameters = convertParameters(channel.parameters);
}

const operations: Record<string, any> = {};
Expand Down Expand Up @@ -364,27 +364,28 @@ 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<string, any>, options: RequiredConvertV2ToV3Options): Record<string, any> {
function convertParameters(parameters: Record<string, any>): Record<string, any> {
const newParameters: Record<string, any> = {};
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.
*
* Ensure that extensions and references are all kept as is.
*
* 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 {
Expand All @@ -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"];
Expand All @@ -411,13 +415,30 @@ 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}),
...(location === null ? null : {location})
};
}

/**
* 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.
*/
Expand Down
5 changes: 4 additions & 1 deletion test/input/2.6.0/for-3.0.0-with-reference-parameter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions test/output/3.0.0/from-2.6.0-with-reference-parameter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
description: Date and time when the message was sent.

0 comments on commit 18ee956

Please sign in to comment.