Skip to content

Commit

Permalink
Enable OpenAPI example export (#1067)
Browse files Browse the repository at this point in the history
fixes OpenAPI export crashing with CRUD routes
Closes #352
Closes #1066
  • Loading branch information
255kb committed Jul 5, 2023
1 parent ba113ff commit aeb17d3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
25 changes: 22 additions & 3 deletions packages/commons-server/src/libs/openapi-converter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import openAPI from '@apidevtools/swagger-parser';
import {
BodyTypes,
BuildEnvironment,
BuildHeader,
BuildHTTPRoute,
Expand All @@ -11,7 +12,8 @@ import {
Methods,
RemoveLeadingSlash,
Route,
RouteResponse
RouteResponse,
RouteType
} from '@mockoon/commons';
import { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types';
import { routesFromFolder } from './utils';
Expand Down Expand Up @@ -77,6 +79,10 @@ export class OpenAPIConverter {
}
],
paths: routes.reduce<OpenAPIV3.PathsObject>((paths, route) => {
if (route.type !== RouteType.HTTP) {
return paths;
}

const pathParameters = route.endpoint.match(/:[a-zA-Z0-9_]+/g);
let endpoint = '/' + route.endpoint;

Expand All @@ -97,11 +103,24 @@ export class OpenAPIConverter {
routeResponse
);

let responseBody = {};

// use inline body as an example if it parses correctly (valid JSON no containing templating)
if (
routeResponse.bodyType === BodyTypes.INLINE &&
routeResponse.body
) {
try {
JSON.parse(routeResponse.body);
responseBody = routeResponse.body;
} catch (error) {}
}

responses[routeResponse.statusCode.toString()] = {
description: routeResponse.label,
content: responseContentType
? { [responseContentType]: {} }
: {},
? { [responseContentType]: { example: responseBody } }
: { '*/*': { example: responseBody } },
headers: [
...environment.headers,
...routeResponse.headers
Expand Down
28 changes: 22 additions & 6 deletions packages/desktop/test/data/res/export-openapi/reference.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"responses": {
"200": {
"description": "Success",
"content": { "text/plain": {} },
"content": {
"text/plain": {
"example": "[\n {\n \"id\": 1,\n \"firstname\": \"John\",\n \"Lastname\": \"Snow\",\n \"status\": \"Learning things\"\n },\n {\n \"id\": 2,\n \"firstname\": \"Daenerys\",\n \"Lastname\": \"Targaryen\",\n \"status\": \"Riding a dragon\"\n }\n]"
}
},
"headers": {
"Global-Header": {
"schema": { "type": "string" },
Expand All @@ -28,7 +32,11 @@
"responses": {
"201": {
"description": "Success",
"content": { "application/json": {} },
"content": {
"application/json": {
"example": "{\n \"firstname\": \"{{body 'firstname'}}\",\n \"lastname\": \"{{body 'lastname'}}\",\n \"status\": \"{{body 'status'}}\"\n}"
}
},
"headers": {
"Global-Header": {
"schema": { "type": "string" },
Expand All @@ -38,7 +46,11 @@
},
"400": {
"description": "Missing data",
"content": { "application/json": {} },
"content": {
"application/json": {
"example": "{\n \"Error\": \"firstname is required\"\n}"
}
},
"headers": {
"Global-Header": {
"schema": { "type": "string" },
Expand All @@ -55,7 +67,11 @@
"responses": {
"200": {
"description": "Get userId 2",
"content": { "application/json": {} },
"content": {
"application/json": {
"example": "{\n \"id\": 2,\n \"firstname\": \"Daenerys\",\n \"Lastname\": \"Targaryen\",\n \"status\": \"Riding a dragon\"\n}"
}
},
"headers": {
"Global-Header": {
"schema": { "type": "string" },
Expand All @@ -78,7 +94,7 @@
"responses": {
"204": {
"description": "User deleted",
"content": { "application/json": {} },
"content": { "application/json": { "example": {} } },
"headers": {
"Global-Header": {
"schema": { "type": "string" },
Expand All @@ -103,7 +119,7 @@
"responses": {
"200": {
"description": "",
"content": { "application/json": {} },
"content": { "application/json": { "example": "{}" } },
"headers": {
"Global-Header": {
"schema": { "type": "string" },
Expand Down
1 change: 1 addition & 0 deletions packages/desktop/test/specs/environments-duplicate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Duplicate environments', () => {
it('should duplicate the environment', async () => {
await dialogs.save(resolve('./tmp/storage/dup-env2-test.json'));
await environments.duplicate(1);
await browser.pause(100);
await environments.assertCount(2);
await navigation.switchView('ENV_SETTINGS');
await environmentsSettings.assertSettingValue('port', '3001');
Expand Down

0 comments on commit aeb17d3

Please sign in to comment.