Skip to content

Commit

Permalink
update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 9, 2023
1 parent 00ef990 commit a733e44
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type ConvertV2ToV3Options = {
useChannelIdExtension?: boolean;
convertServerComponents?: boolean;
convertChannelComponents?: boolean;
failOnParameterReference?: boolean;
}
export type ConvertOptions = {
v2tov3?: ConvertV2ToV3Options;
Expand Down
18 changes: 12 additions & 6 deletions src/third-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -158,7 +159,7 @@ function convertChannelObjects(channels: Record<string, any>, asyncapi: AsyncAPI

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

const operations: Record<string, any> = {};
Expand Down Expand Up @@ -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<string, any>): Record<string, any> {
function convertParameters(parameters: Record<string, any>, options: RequiredConvertV2ToV3Options): Record<string, any> {
const newParameters: Record<string, any> = {};
Object.entries(parameters).forEach(([name, parameter]) => {
newParameters[name] = convertParameter(parameter);
newParameters[name] = convertParameter(parameter, options);
});
return newParameters;
}
Expand All @@ -383,7 +384,7 @@ function convertParameters(parameters: Record<string, any>): Record<string, any>
*
* 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 {
Expand All @@ -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;
Expand All @@ -416,6 +421,7 @@ function convertParameter(parameter: any): any {
location === null ? null : {location}
);
}

/**
* Convert `channels`, `servers` and `securitySchemes` in components.
*/
Expand Down
12 changes: 12 additions & 0 deletions test/second-to-third-version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit a733e44

Please sign in to comment.