Skip to content

Commit

Permalink
updated the test and made some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmin2 committed Jul 21, 2024
1 parent bcfa32b commit bdbac79
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { convert } from './convert';

export type { AsyncAPIDocument, ConvertVersion, ConvertOptions } from './interfaces';
export type { AsyncAPIDocument, AsyncAPIConvertVersion, OpenAPIConvertVersion, ConvertOptions } from './interfaces';
33 changes: 21 additions & 12 deletions src/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sortObjectKeys, isRefObject, isPlainObject } from "./utils";
import { sortObjectKeys, isRefObject, isPlainObject, removeEmptyObjects } from "./utils";
import { AsyncAPIDocument, ConvertOpenAPIFunction, ConvertOptions, OpenAPIDocument } from "./interfaces";

export const converters: Record<string, ConvertOpenAPIFunction > = {
Expand All @@ -7,14 +7,16 @@ export const converters: Record<string, ConvertOpenAPIFunction > = {

function from_openapi_to_asyncapi(openapi: OpenAPIDocument, options?: ConvertOptions): AsyncAPIDocument {
const asyncapi: Partial<AsyncAPIDocument> = {
asyncapi: openapi.openapi,
asyncapi: '3.0.0',
info: convertInfoObject(openapi.info, openapi),
servers: isPlainObject(openapi.servers) ? convertServerObjects(openapi.servers, openapi) : undefined,
channels: convertPathsToChannels(openapi.paths),
operations: convertPathsToOperations(openapi.paths, 'server'),
components: convertComponents(openapi.components),
servers: convertServerObjects(openapi.servers, openapi),
channels: isPlainObject(openapi.paths) ? convertPathsToChannels(openapi.paths) : undefined,
operations: isPlainObject(openapi.paths) ? convertPathsToOperations(openapi.paths, 'server') : undefined,
components: isPlainObject(openapi.components) ? convertComponents(openapi.components): undefined,
};

removeEmptyObjects(asyncapi);

return sortObjectKeys(
asyncapi as AsyncAPIDocument,
['asyncapi', 'info', 'defaultContentType', 'servers', 'channels', 'operations', 'components']
Expand All @@ -24,8 +26,8 @@ function from_openapi_to_asyncapi(openapi: OpenAPIDocument, options?: ConvertOpt
function convertInfoObject(info: OpenAPIDocument['info'], openapi: OpenAPIDocument): AsyncAPIDocument['info'] {
return sortObjectKeys({
...info,
tags: openapi.tags || undefined,
externalDocs: openapi.externalDocs || undefined,
tags: [openapi.tags],
externalDocs: openapi.externalDocs,
}, [
"title",
"version",
Expand All @@ -41,7 +43,9 @@ function convertInfoObject(info: OpenAPIDocument['info'], openapi: OpenAPIDocume
function convertServerObjects(servers: Record<string, any>, openapi: OpenAPIDocument): AsyncAPIDocument['servers'] {
const newServers: Record<string, any> = {};
const security: Record<string, any> = openapi.security;
Object.entries(servers).forEach(([serverName, server]: [string, any]) => {
Object.entries(servers).forEach(([index, server]: [string, any]) => {

const serverName = generateServerName(server.url);
if (isRefObject(server)) {
newServers[serverName] = server;
return;
Expand Down Expand Up @@ -72,19 +76,24 @@ function convertServerObjects(servers: Record<string, any>, openapi: OpenAPIDocu
return newServers;
}

function generateServerName(url: string): string {
const { host, pathname } = resolveServerUrl(url);
const baseName = host.split('.').slice(-2).join('.');
const pathSegment = pathname ? pathname.split('/')[1] : '';
return `${baseName}${pathSegment ? `_${pathSegment}` : ''}`.replace(/[^a-zA-Z0-9_]/g, '_');
}

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

if (pathnames.length) {
return {
host,
Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,17 @@ function untilde(str: string) {
return sub;
});
}

export function removeEmptyObjects(obj: Record<string, any>): Record<string, any> {
Object.keys(obj).forEach(key => {
if (obj[key] && typeof obj[key] === 'object') {
removeEmptyObjects(obj[key]);
if (Object.keys(obj[key]).length === 0) {
delete obj[key];
}
} else if (obj[key] === undefined) {
delete obj[key];
}
});
return obj;
}
7 changes: 0 additions & 7 deletions test/input/openapi/no-channel-operation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ servers:
default: '8443'
basePath:
default: v2

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

tags:
name: pet
description: Pets operations
Expand Down
18 changes: 7 additions & 11 deletions test/output/openapi-to-asyncapi/no-channel-parameter.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
asyncapi: 3.0.0
info:
info:
title: Sample Pet Store App
version: 1.0.1
description: This is a sample server for a pet store.
Expand All @@ -12,14 +12,16 @@ info:
name: Apache 2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
name: pet
description: Pets operations
- name: pet
description: Pets operations
externalDocs:
description: Find more info here
url: 'https://example.com'

servers:
- url: 'https://{username}.gigantic-server.com:{port}/{basePath}'
gigantic_server_com__port___basePath_:
host: '{username}.gigantic-server.com:{port}'
pathname: '/{basePath}'
protocol: https
description: The production API server
variables:
username:
Expand All @@ -34,9 +36,3 @@ servers:
default: '8443'
basePath:
default: v2

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

0 comments on commit bdbac79

Please sign in to comment.