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

feat: conversion of openapi '3.0' to asyncapi '3.0' #269

Merged
merged 16 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 34 additions & 8 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ import { dump } from 'js-yaml';
import { converters as firstConverters } from "./first-version";
import { converters as secondConverters } from "./second-version";
import { converters as thirdConverters } from "./third-version";
import { converters as openapiConverters } from "./openapi";

import { serializeInput } from "./utils";

import type { AsyncAPIDocument, ConvertVersion, ConvertOptions, ConvertFunction } from './interfaces';
import type { AsyncAPIDocument, ConvertVersion, ConvertOptions, ConvertFunction, ConvertOpenAPIFunction, OpenAPIDocument } from './interfaces';

/**
* Value for key (version) represents the function which converts specification from previous version to the given as key.
*/
const converters: Record<string, ConvertFunction> = {
const asyncAPIconverters: Record<string, ConvertFunction> = {
...firstConverters,
...secondConverters,
...thirdConverters,
};
const conversionVersions = Object.keys(converters);

export function convert(asyncapi: string, version?: ConvertVersion, options?: ConvertOptions): string;
export function convert(asyncapi: AsyncAPIDocument, version?: ConvertVersion, options?: ConvertOptions): AsyncAPIDocument;
export function convert(asyncapi: string | AsyncAPIDocument, version: ConvertVersion = '2.6.0', options: ConvertOptions = {}): string | AsyncAPIDocument {
const { format, document } = serializeInput(asyncapi);
const conversionVersions = Object.keys(asyncAPIconverters);

export function convert(input: string, version: ConvertVersion, options?: ConvertOptions): string;
export function convert(input: AsyncAPIDocument, version: ConvertVersion, options?: ConvertOptions): AsyncAPIDocument;
export function convert(input: string | AsyncAPIDocument, version: ConvertVersion , options: ConvertOptions= {}): string | AsyncAPIDocument {
const { format, document } = serializeInput(input);

if ('openapi' in document) {
throw new Error('Cannot convert OpenAPI document. Use convertOpenAPI function instead.');
}

const asyncapiVersion = document.asyncapi;
let fromVersion = conversionVersions.indexOf(asyncapiVersion);
Expand All @@ -42,11 +48,31 @@ export function convert(asyncapi: string | AsyncAPIDocument, version: ConvertVer
let converted = document;
for (let i = fromVersion; i <= toVersion; i++) {
const v = conversionVersions[i] as ConvertVersion;
converted = converters[v](converted, options);
converted = asyncAPIconverters[v](converted, options);
}

if (format === 'yaml') {
return dump(converted, { skipInvalid: true });
}
return converted;
}

export function convertOpenAPI(input: string ,options?: ConvertOptions): string;
export function convertOpenAPI(input: OpenAPIDocument ,options?: ConvertOptions): AsyncAPIDocument;
export function convertOpenAPI(input: string | OpenAPIDocument,options: ConvertOptions = {}): string | AsyncAPIDocument {

const { format, document } = serializeInput(input);

const openapiToAsyncapiConverter = openapiConverters["openapi"] as ConvertOpenAPIFunction;

if (!openapiToAsyncapiConverter) {
throw new Error("OpenAPI to AsyncAPI converter is not available.");
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
}

const convertedAsyncAPI = openapiToAsyncapiConverter(document as OpenAPIDocument, options);

if (format === "yaml") {
return dump(convertedAsyncAPI, { skipInvalid: true });
}
return convertedAsyncAPI;
}
4 changes: 3 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* PUBLIC TYPES
*/
export type AsyncAPIDocument = { asyncapi: string } & Record<string, any>;
export type ConvertVersion = '1.1.0' | '1.2.0' | '2.0.0-rc1' | '2.0.0-rc2' | '2.0.0' | '2.1.0' | '2.2.0' | '2.3.0' | '2.4.0' | '2.5.0' | '2.6.0' | '3.0.0';
export type OpenAPIDocument = { openapi: string } & Record<string, any>;
export type ConvertVersion = '1.1.0' | '1.2.0' | '2.0.0-rc1' | '2.0.0-rc2' | '2.0.0' | '2.1.0' | '2.2.0' | '2.3.0' | '2.4.0' | '2.5.0' | '2.6.0' | '3.0.0' | 'openapi';
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
export type ConvertV2ToV3Options = {
idGenerator?: (data: { asyncapi: AsyncAPIDocument, kind: 'channel' | 'operation' | 'message', key: string | number | undefined, path: Array<string | number>, object: any, parentId?: string }) => string,
pointOfView?: 'application' | 'client',
Expand All @@ -18,4 +19,5 @@ export type ConvertOptions = {
* PRIVATE TYPES
*/
export type ConvertFunction = (asyncapi: AsyncAPIDocument, options: ConvertOptions) => AsyncAPIDocument;
export type ConvertOpenAPIFunction = (openapi: OpenAPIDocument, options: ConvertOptions) => AsyncAPIDocument;

119 changes: 119 additions & 0 deletions src/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { sortObjectKeys, isRefObject, isPlainObject } from "./utils";
import { AsyncAPIDocument, ConvertOpenAPIFunction, ConvertOptions, OpenAPIDocument } from "./interfaces";

export const converters: Record<string, ConvertOpenAPIFunction > = {
'openapi': from_openapi_to_asyncapi,
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
}

function from_openapi_to_asyncapi(openapi: OpenAPIDocument, options: ConvertOptions): AsyncAPIDocument{
convertName(openapi)

convertInfoObject(openapi);
if (isPlainObject(openapi.servers)) {
openapi.servers = convertServerObjects(openapi.servers, openapi);
}

return sortObjectKeys(
openapi,
['asyncapi', 'info', 'defaultContentType', 'servers', 'channels', 'operations', 'components']
)
}

function convertName(openapi: OpenAPIDocument): AsyncAPIDocument {
let { openapi: version } = openapi;
openapi.asyncapi = version;
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
delete (openapi as any).openapi;;

return sortObjectKeys(
openapi,
['asyncapi', 'info', 'defaultContentType', 'servers', 'channels', 'operations', 'components']
)
}

function convertInfoObject(openapi: OpenAPIDocument) {
if(openapi.tags) {
openapi.info.tags = openapi.tags;
delete openapi.tags;
}

if(openapi.externalDocs) {
openapi.info.externalDocs = openapi.externalDocs;
delete openapi.externalDocs;
}

return (openapi.info = sortObjectKeys(openapi.info, [
"title",
"version",
"description",
"termsOfService",
"contact",
"license",
"tags",
"externalDocs",
]));
}

function convertServerObjects(servers: Record<string, any>, openapi: OpenAPIDocument) {
const newServers: Record<string, any> = {};
const security: Record<string, any> = openapi.security;
Object.entries(servers).forEach(([serverName, server]: [string, any]) => {
if (isRefObject(server)) {
newServers[serverName] = server;
return;
}

const { host, pathname, protocol } = resolveServerUrl(server.url);
server.host = host;
if (pathname !== undefined) {
server.pathname = pathname;
}

if (protocol !== undefined && server.protocol === undefined) {
server.protocol = protocol;
}
delete server.url;

if (security) {
server.security = security;
delete openapi.security;
}

newServers[serverName] = sortObjectKeys(
server,
['host', 'pathname', 'protocol', 'protocolVersion', 'title', 'summary', 'description', 'variables', 'security', 'tags', 'externalDocs', 'bindings'],
);
});

return newServers
}

function resolveServerUrl(url: string): {
host: string;
pathname: string | undefined;
protocol: string | undefined;
} {
let [maybeProtocol, maybeHost] = url.split("://");
console.log("maybeProtocol", maybeProtocol);
if (!maybeHost) {
maybeHost = maybeProtocol;
}
const [host, ...pathnames] = maybeHost.split("/");
console.log("pathname1", pathnames);
if (pathnames.length) {
return {
host,
pathname: `/${pathnames.join("/")}`,
protocol: maybeProtocol,
};
}
return { host, pathname: undefined, protocol: maybeProtocol };
}

function convertSecurity(security: Record<string, any>) {
if(security.type === 'oauth2' && security.flows.authorizationCode.scopes) {
const availableScopes = security.flows.authorizationCode.scopes;
security.flows.authorizationCode.availableScopes = availableScopes;
delete security.flows.authorizationCode.scopes;
}
return security;
}
23 changes: 15 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { load } from 'js-yaml';

import type { AsyncAPIDocument } from "./interfaces";
import type { AsyncAPIDocument, OpenAPIDocument } from "./interfaces";

export function serializeInput(document: string | AsyncAPIDocument): { format: 'json' | 'yaml', document: AsyncAPIDocument } | never {
export function serializeInput(document: string | AsyncAPIDocument | OpenAPIDocument): { format: 'json' | 'yaml', document: AsyncAPIDocument | OpenAPIDocument } | never {
let triedConvertToYaml = false;
try {
if (typeof document === 'object') {
Expand All @@ -14,18 +14,25 @@ export function serializeInput(document: string | AsyncAPIDocument): { format: '

const maybeJSON = JSON.parse(document);
if (typeof maybeJSON === 'object') {
return {
format: 'json',
document: maybeJSON,
};
if ('openapi' in maybeJSON) {
return {
format: 'json',
document: maybeJSON,
};
} else {
return {
format: 'json',
document: maybeJSON,
};
}
}

triedConvertToYaml = true; // NOSONAR
// if `maybeJSON` is object, then we have 100% sure that we operate on JSON,
// but if it's `string` then we have option that it can be YAML but it doesn't have to be
return {
format: 'yaml',
document: load(document) as AsyncAPIDocument,
document: load(document) as AsyncAPIDocument | OpenAPIDocument,
};
} catch (e) {
try {
Expand All @@ -36,7 +43,7 @@ export function serializeInput(document: string | AsyncAPIDocument): { format: '
// try to parse (again) YAML, because the text itself may not have a JSON representation and cannot be represented as a JSON object/string
return {
format: 'yaml',
document: load(document as string) as AsyncAPIDocument,
document: load(document as string) as AsyncAPIDocument | OpenAPIDocument,
};
} catch (err) {
throw new Error('AsyncAPI document must be a valid JSON or YAML document.');
Expand Down
3 changes: 2 additions & 1 deletion test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
It is a helper required for testing on windows. It can't be solved by editor configuration and the end line setting because expected result is converted during tests.
We need to remove all line breaks from the string
as well as all multiple spaces and trim the string.
*/
export function removeLineBreaks(str: string) {
return str.replace(/\r?\n|\r/g, '')
return str.replace(/\r?\n|\r/g, '').replace(/\s+/g, ' ').trim();
}

export function assertResults(output: string, result: string){
Expand Down
42 changes: 42 additions & 0 deletions test/input/openapi/no-channel-operation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
openapi: 3.0.0
info:
title: Sample Pet Store App
description: This is a sample server for a pet store.
termsOfService: 'http://example.com/terms/'
contact:
name: API Support
url: 'http://www.example.com/support'
email: [email protected]
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
version: 1.0.1

servers:
- url: 'https://{username}.gigantic-server.com:{port}/{basePath}'
description: The production API server
variables:
username:
default: demo
description: this value is assigned by the service provider, in this example `gigantic-server.com`
port:
enum:
- '8443'
- '443'
default: '8443'
basePath:
default: v2

security:
- {}
- petstore_auth:
- write:pets
- read:pets

tags:
name: pet
description: Pets operations

externalDocs:
description: Find more info here
url: 'https://example.com'
14 changes: 14 additions & 0 deletions test/openapi-to-asyncapi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from 'fs';
import path from 'path';

import { convertOpenAPI } from '../src/convert';
import { assertResults } from './helpers';

describe("convert() - openapi to asyncapi", () => {
it("should convert the basic structure of openapi to asyncapi", () => {
const input = fs.readFileSync(path.resolve(__dirname, "input", "openapi", "no-channel-operation.yml"), "utf8");
const output = fs.readFileSync(path.resolve(__dirname, "output", "openapi-to-asyncapi", "no-channel-parameter.yml"), "utf8");
const result = convertOpenAPI(input);
assertResults(output, result);
});
});
42 changes: 42 additions & 0 deletions test/output/openapi-to-asyncapi/no-channel-parameter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
asyncapi: 3.0.0
info:
title: Sample Pet Store App
version: 1.0.1
description: This is a sample server for a pet store.
termsOfService: 'http://example.com/terms/'
contact:
name: API Support
url: 'http://www.example.com/support'
email: [email protected]
license:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
name: pet
description: Pets operations
externalDocs:
description: Find more info here
url: 'https://example.com'

servers:
- url: 'https://{username}.gigantic-server.com:{port}/{basePath}'
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
description: The production API server
variables:
username:
default: demo
description: >-
this value is assigned by the service provider, in this example
`gigantic-server.com`
port:
enum:
- '8443'
- '443'
default: '8443'
basePath:
default: v2

security:
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
- {}
- petstore_auth:
- 'write:pets'
- 'read:pets'
Loading