Skip to content

Commit

Permalink
fix: parameter conversion for v3 (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jul 24, 2023
1 parent b5afb03 commit 35f3dca
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 9 deletions.
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 ref = parameter['$ref'] ?? null;
if(ref !== null) {
return {
$ref: ref
}
}

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;

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

//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},
location === null ? null : {location}
);
}
/**
* 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"
4 changes: 1 addition & 3 deletions test/output/3.0.0/from-2.6.0-with-custom-schema-format.yml
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

0 comments on commit 35f3dca

Please sign in to comment.