Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parameter conversion for v3 #190

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/third-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ function convertChannelObjects(channels: Record<string, any>, asyncapi: AsyncAPI
channel.servers = servers.map((serverName: string) => createRefObject('servers', serverName));
}

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

const operations: Record<string, any> = {};
// serialize publish and subscribe Operation Objects to standalone object
const publishMessages = toStandaloneOperation({kind: 'publish', channel, asyncapi, operations, context, inComponents, channelId, channelAddress, options, oldPath});
Expand Down Expand Up @@ -356,8 +361,57 @@ function convertComponents(asyncapi: AsyncAPIDocument, options: RequiredConvertV
messages: components.messages
});
}

if (isPlainObject(components.parameters)) {
components.parameters = convertParameters(components.parameters);
}
}
/**
* Convert all parameters to the new v3 format
*/
function convertParameters(parameters: Record<string, any>): Record<string, any> {
const newParameters: Record<string, any> = {};
Object.entries(parameters).forEach(([name, parameter]) => {
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): any {
const enumValues = parameter.schema?.enum ?? 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;
const ref = parameter['$ref'] ?? null;

//Make sure we keep parameter extensions
const v2ParameterObjectProperties = ["location", "schema", "description", "$ref"];
const v2ParameterObjectExtensions = Object.entries(parameter).filter(([key,]) => {
return !v2ParameterObjectProperties.includes(key);
});

if(ref !== null) {
return {
$ref: ref
}
}
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved

//Return the new v3 parameter object
return Object.assign({...v2ParameterObjectExtensions},
enumValues === null ? null : {enum: enumValues},
defaultValues === null ? null : {default: defaultValues},
description === null ? null : {description},
examples === null ? null : {examples},
examples === null ? null : {location}
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
);
}
/**
* Convert `channels`, `servers` and `securitySchemes` in components.
*/
Expand Down
54 changes: 54 additions & 0 deletions test/input/2.6.0/for-3.0.0-with-mixed-parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
asyncapi: 2.6.0
channels:
'{enum}/{default}/{description}/{examples}/{location}/{mixed}':
publish:
operationId: lightMeasured
message:
payload:
type: string
parameters:
enum:
$ref: '#/components/parameters/enum'
default:
$ref: '#/components/parameters/default'
description:
$ref: '#/components/parameters/description'
examples:
$ref: '#/components/parameters/examples'
location:
$ref: '#/components/parameters/location'
mixed:
$ref: '#/components/parameters/mixed'
components:
parameters:
enum:
schema:
type: string
enum: ["test"]
default:
schema:
type: string
default: "test"
description:
description: Just a test description
schema:
description: Just a test description 2
type: string
examples:
schema:
type: string
examples: ["test"]
location:
location: "$message.payload"
schema:
type: string
mixed:
location: "$message.payload"
description: Just a test description
schema:
type: string
enum: ["test"]
default: "test"
description: Just a test description 2
examples: ["test"]
x-custom-extension: "test"
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,4 @@ components:
type: int
parameters:
streetlightId:
description: The ID of the streetlight.
schema:
type: string
description: The ID of the streetlight.
2 changes: 0 additions & 2 deletions test/output/3.0.0/from-2.6.0-with-deep-local-references.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ components:
parameters:
streetlightId:
description: The ID of the streetlight.
schema:
type: string
operations:
someChannel.publish:
action: receive
Expand Down
40 changes: 40 additions & 0 deletions test/output/3.0.0/from-2.6.0-with-mixed-parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
asyncapi: 3.0.0
channels:
'{enum}/{default}/{description}/{examples}/{location}/{mixed}':
address: '{enum}/{default}/{description}/{examples}/{location}/{mixed}'
messages:
lightMeasured.message:
payload:
type: string
parameters:
enum:
$ref: '#/components/parameters/enum'
default:
$ref: '#/components/parameters/default'
description:
$ref: '#/components/parameters/description'
examples:
$ref: '#/components/parameters/examples'
location:
$ref: '#/components/parameters/location'
mixed:
$ref: '#/components/parameters/mixed'
components:
parameters:
enum:
enum: ["test"]
default:
default: "test"
description:
description: Just a test description
examples:
examples: ["test"]
location:
location: "$message.payload"
mixed:
enum: ["test"]
default: "test"
description: Just a test description
examples: ["test"]
location: "$message.payload"
x-custom-extension: "test"
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ components:
parameters:
streetlightId:
description: The ID of the streetlight.
schema:
type: string
operations:
usedChannel.publish:
action: receive
Expand Down
2 changes: 0 additions & 2 deletions test/output/3.0.0/from-2.6.0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,3 @@ components:
parameters:
streetlightId:
description: The ID of the streetlight.
schema:
type: string