Skip to content

Commit

Permalink
feat(build): add the supplied-server to the list of servers in the AP…
Browse files Browse the repository at this point in the history
…IG spec endpoint
  • Loading branch information
uglow committed Sep 16, 2020
1 parent b993c55 commit 827076f
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 2 deletions.
282 changes: 282 additions & 0 deletions fixtures/output/buildMock3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
{
"openapi": "3.0.0",
"info": {
"description": "Fake Online REST API for Testing and Prototyping",
"version": "1.0.0",
"title": "JSON Placeholder"
},
"tags": [
{
"name": "posts"
}
],
"servers": [
{
"url": "https://first.server.com"
}
],
"paths": {
"/posts": {
"get": {
"tags": [
"posts"
],
"summary": "Get all available posts",
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "MOCK",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "\"\""
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match"
}
}
},
"/posts/{id}": {
"get": {
"tags": [
"posts"
],
"summary": "Get specific post",
"responses": {
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "MOCK",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "\"\""
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match"
}
}
},
"/posts/{id}/404": {
"get": {
"tags": [
"posts"
],
"summary": "Get specific post",
"responses": {
"404": {
"description": "not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "MOCK",
"responses": {
"default": {
"statusCode": "404",
"responseTemplates": {
"application/json": "\"\""
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match"
}
}
},
"/open-api-spec.json": {
"get": {
"x-ignore": true,
"tags": [
"meta"
],
"summary": "Open API Spec schema",
"parameters": [],
"responses": {
"200": {
"description": "Successful Operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/StringResponse"
}
}
},
"headers": {
"Access-Control-Allow-Origin": {
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Methods": {
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Headers": {
"schema": {
"type": "string"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "MOCK",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"application/json": "{\"openapi\":\"3.0.0\",\"info\":{\"description\":\"Fake Online REST API for Testing and Prototyping\",\"version\":\"1.0.0\",\"title\":\"JSON Placeholder\"},\"tags\":[{\"name\":\"posts\"}],\"servers\":[{\"url\":\"https://first.server.com\"},{\"url\":\"https://jsonplaceholder.typicode.com\"}],\"paths\":{\"/posts\":{\"get\":{\"tags\":[\"posts\"],\"summary\":\"Get all available posts\",\"parameters\":[{\"name\":\"id\",\"in\":\"query\",\"description\":\"Filter by post ID\",\"required\":false,\"schema\":{\"type\":\"integer\"}},{\"name\":\"userId\",\"in\":\"query\",\"description\":\"Filter by user ID\",\"required\":false,\"schema\":{\"type\":\"integer\"}}],\"responses\":{\"200\":{\"description\":\"successful operation\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"array\",\"items\":{\"\\$ref\":\"#/components/schemas/Post\"}}}},\"x-mock-file\":\"\",\"x-examples\":{\"default\":{\"parameters\":[{\"value\":null},{\"value\":null}]}}}}}},\"/posts/{id}\":{\"get\":{\"tags\":[\"posts\"],\"summary\":\"Get specific post\",\"parameters\":[{\"name\":\"id\",\"in\":\"path\",\"description\":\"The ID of the post to retrieve\",\"required\":true,\"schema\":{\"type\":\"integer\"}}],\"responses\":{\"200\":{\"description\":\"successful operation\",\"content\":{\"application/json\":{\"schema\":{\"\\$ref\":\"#/components/schemas/Post\"}}},\"x-mock-file\":\"\",\"x-examples\":{\"id_1\":{\"parameters\":[{\"value\":\"1\"}]},\"id_2\":{\"parameters\":[{\"value\":\"2\"}]}}},\"404\":{\"description\":\"not found\",\"content\":{\"application/json\":{\"schema\":{\"\\$ref\":\"#/components/schemas/NotFoundError\"}}},\"x-mock-file\":\"\",\"x-examples\":{\"badParam\":{\"parameters\":[{\"value\":\"wrong-param\"}]}}}}}}},\"components\":{\"schemas\":{\"Post\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"userId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"},\"body\":{\"type\":\"string\"}}},\"Comment\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"postId\":{\"type\":\"integer\",\"format\":\"int64\"},\"name\":{\"type\":\"string\"},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"body\":{\"type\":\"string\"}}},\"Album\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"userId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"}}},\"Photo\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"albumId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"},\"url\":{\"type\":\"string\",\"format\":\"uri\"},\"thumbnailUrl\":{\"type\":\"string\",\"format\":\"uri\"}}},\"Todo\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"userId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"},\"completed\":{\"type\":\"boolean\"}}},\"User\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"name\":{\"type\":\"string\"},\"username\":{\"type\":\"string\"},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"phone\":{\"type\":\"string\"},\"website\":{\"type\":\"string\"},\"company\":{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"catchPhrase\":{\"type\":\"string\"},\"bs\":{\"type\":\"string\"}}},\"address\":{\"type\":\"object\",\"properties\":{\"street\":{\"type\":\"string\"},\"suite\":{\"type\":\"string\"},\"city\":{\"type\":\"string\"},\"zipcode\":{\"type\":\"string\"},\"geo\":{\"type\":\"object\",\"properties\":{\"lat\":{\"type\":\"string\"},\"lng\":{\"type\":\"string\"}}}}}}},\"NotFoundError\":{\"type\":\"object\"},\"Empty\":{\"type\":\"object\",\"title\":\"Empty response model for AWS + CORS use\"},\"StringResponse\":{\"type\":\"string\",\"title\":\"String-type workaround for API Gateway\"}}}}"
},
"responseParameters": {
"method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'",
"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Origin": "'*'"
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match"
}
},
"options": {
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
},
"headers": {
"Access-Control-Allow-Origin": {
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Methods": {
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Headers": {
"schema": {
"type": "string"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "MOCK",
"responses": {
"default": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'",
"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Origin": "'*'"
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match"
}
}
},
"/": {
"get": {
"x-ignore": true,
"tags": [
"meta"
],
"responses": {
"200": {
"description": "200 response",
"content": {
"text/html": {
"schema": {
"$ref": "#/components/schemas/StringResponse"
},
"example": {
"value": "<html><body>Your HTML text</body></html>"
}
}
}
}
},
"x-amazon-apigateway-integration": {
"type": "MOCK",
"responses": {
"default": {
"statusCode": "200",
"responseTemplates": {
"text/html": "<html>\n<head>\n <title>My Company</title>\n <script src=\"https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js\"></script>\n <script src=\"https://unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js\"></script>\n <link rel=\"stylesheet\" href=\"https://unpkg.com/swagger-ui-dist@3/swagger-ui.css\">\n <link rel=\"shortcut icon\" href=\"data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAAAAAAAAAAAAAC0/9wAtP/cHLT/3SS0/96ktP/flLT/3/C0/9/wtP/flLT/3qS0/90ktP/cHLT/3AAAAAAAAAAAALT/3AC0/9wAtP/cZLT/3ky0/9+8tP/f/LT/3/y0/9/8tP/f/LT/3/y0/9/8tP/fvLT/3ky0/9xktP/cALT/3AC0/9wAtP/cZLT/3sS5A9/8tP/f/LT/3/y0/9/8tP/f/LT/3/y0/9/8tP/f/LT/3/y0/9/8tP/exLT/3GS0/9wAtP/cGLT/3kys+9/9GVvj/TVz4/ys99/8tP/f/LT/3/y0/9/8tP/f/LT/3/y0/9/8tP/f/LT/3/y0/95MtP/cGLT/3Si0/9+4rPff/TVz4/56m+/80Rff/Kjz3/zJE9/8vQPf/LT/3/y0/9/8tP/f/LT/3/y0/9/8tP/fuLT/3Si0/96gtP/f/LD73/zpK9//Kz/3/hpD6/2Rx+f+6wPz/aHX5/yo89/8tP/f/LT/3/y0/9/8tP/f/LT/3/y0/96gtP/fkLT/3/y0/9/8sPvf/pKz8//b3///z9P///////8nO/f88TPj/Kz33/zJE9/8xQvf/LT/3/y0/9/8tP/fkLT/3+y0/9/8tP/f/Kj33/2Fu+f/x8v7/7O7+//j5///9/v//lp/7/zhJ9/+Yofv/VWP5/ys99/8tP/f/LT/3+y0/9/stP/f/LT/3/y0/9/8yRPf/bHn5/1Ni+P+yufz///////Hy/v/Eyf3/8fL+/2Rx+f8qPPf/LT/3/y0/9/stP/fkLT/3/y0/9/8tP/f/LT/3/yo89/8pO/f/Sln4/9ve/v////////////z8//9ve/r/KTz3/y0/9/8tP/fkLT/3qC0/9/8tP/f/LT/3/y0/9/8sPvf/NUb3/4iS+v/p6/7/////////////////e4f6/yk89/8tP/f/LT/3qC0/90otP/fuLT/3/y0/9/8tP/f/LT/3/zRF9/97hvr/w8j9/+3u/v/9/v///////4mT+v8qPPf/LT/37i0/90otP/cGLT/3ky0/9/8tP/f/LT/3/y0/9/8tP/f/Kjz3/zNF9/9SYPj/fIj6/6au/P9tevn/Kz33/y0/95MtP/cGLT/3AC0/9xktP/exLT/3/y0/9/8tP/f/LT/3/y0/9/8tP/f/Kz33/yo89/8sPvf/LT/3/y0/97EtP/cZLT/3AC0/9wAtP/cALT/3GS0/95MtP/fvLT/3/y0/9/8tP/f/LT/3/y0/9/8tP/f/LT/37y0/95MtP/cZLT/3AC0/9wAAAAAAAAAAAC0/9wAtP/cHLT/3SS0/96ktP/flLT/3/C0/9/wtP/flLT/3qS0/90ktP/cHLT/3AAAAAAAAAAAA4AcAAMADAACAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIABAADAAwAA4AcAAA==\" />\n <style>\n /* Add company logo */\n .topbar-wrapper img {\n content: url('https://www.elitedangerous.com/img/logo-elite-dangerous-icon.c7206b1e.svg');\n }\n </style>\n</head>\n<body>\n<div id=\"api-ui\"></div>\n<script>\n // Source code from https://stackoverflow.com/questions/10687099/how-to-test-if-a-url-string-is-absolute-or-relative\n var protocolRegEx = new RegExp('^(?:[a-z]+:)?//', 'i'); // Detects absolute URLs\n\n function isRelativeUrl(url) {\n return !protocolRegEx.test(url); // \"not\" this regEx to return true for relative URLs\n }\n\n var swaggerConfig = {\n dom_id: '#api-ui',\n presets: [\n SwaggerUIBundle.presets.apis,\n SwaggerUIStandalonePreset\n ],\n layout: \"StandaloneLayout\"\n };\n var specFileUrl = '/open-api-spec.json'; // This variable is replaced at build time. The double-quotes are significant!\n\n // For demo purposes, use a blank object so we can test that the UI loads\n if (specFileUrl === '$$_specFileUrl_$$') {\n swaggerConfig.spec = {\"openapi\":\"3.0.0\",\"info\":{\"description\":\"Fake Online REST API for Testing and Prototyping\",\"version\":\"1.0.0\",\"title\":\"JSON Placeholder\"},\"tags\":[{\"name\":\"posts\"},{\"name\":\"comments\"},{\"name\":\"albums\"},{\"name\":\"photos\"},{\"name\":\"todos\"},{\"name\":\"users\"}],\"servers\":[{\"url\":\"https://jsonplaceholder.typicode.com\"}],\"paths\":{\"/posts\":{\"get\":{\"tags\":[\"posts\"],\"summary\":\"Get all available posts\",\"consumes\":[\"application/json\"],\"produces\":[\"application/json\"],\"parameters\":[{\"name\":\"id\",\"in\":\"query\",\"description\":\"Filter by post ID\",\"required\":false,\"schema\":{\"type\":\"integer\"}},{\"name\":\"userId\",\"in\":\"query\",\"description\":\"Filter by user ID\",\"required\":false,\"schema\":{\"type\":\"integer\"}}],\"responses\":{\"200\":{\"description\":\"successful operation\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Post\"}}}},\"x-examples\":{\"default\":{\"parameters\":[{\"value\":null},{\"value\":null}]}}}}}},\"/posts/{id}\":{\"get\":{\"tags\":[\"posts\"],\"summary\":\"Get specific post\",\"consumes\":[\"application/json\"],\"produces\":[\"application/json\"],\"parameters\":[{\"name\":\"id\",\"in\":\"path\",\"description\":\"The ID of the post to retrieve\",\"required\":true,\"schema\":{\"type\":\"integer\"}}],\"responses\":{\"200\":{\"description\":\"successful operation\",\"content\":{\"*/*\":{\"schema\":{\"$ref\":\"#/components/schemas/Post\"}}},\"x-examples\":{\"id_1\":{\"parameters\":[{\"value\":\"1\"}]},\"id_2\":{\"parameters\":[{\"value\":\"2\"}]}}},\"404\":{\"description\":\"not found\",\"content\":{\"*/*\":{\"schema\":{\"$ref\":\"#/components/schemas/NotFoundError\"}}},\"x-examples\":{\"badParam\":{\"parameters\":[{\"value\":\"wrong-param\"}]}}}}}}},\"components\":{\"schemas\":{\"Post\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"userId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"},\"body\":{\"type\":\"string\"}}},\"Comment\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"postId\":{\"type\":\"integer\",\"format\":\"int64\"},\"name\":{\"type\":\"string\"},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"body\":{\"type\":\"string\"}}},\"Album\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"userId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"}}},\"Photo\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"albumId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"},\"url\":{\"type\":\"string\",\"format\":\"uri\"},\"thumbnailUrl\":{\"type\":\"string\",\"format\":\"uri\"}}},\"Todo\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"userId\":{\"type\":\"integer\",\"format\":\"int64\"},\"title\":{\"type\":\"string\"},\"completed\":{\"type\":\"boolean\"}}},\"User\":{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\",\"format\":\"int64\"},\"name\":{\"type\":\"string\"},\"username\":{\"type\":\"string\"},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"phone\":{\"type\":\"string\"},\"website\":{\"type\":\"string\"},\"company\":{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"catchPhrase\":{\"type\":\"string\"},\"bs\":{\"type\":\"string\"}}},\"address\":{\"type\":\"object\",\"properties\":{\"street\":{\"type\":\"string\"},\"suite\":{\"type\":\"string\"},\"city\":{\"type\":\"string\"},\"zipcode\":{\"type\":\"string\"},\"geo\":{\"type\":\"object\",\"properties\":{\"lat\":{\"type\":\"string\"},\"lng\":{\"type\":\"string\"}}}}}}},\"NotFoundError\":{\"type\":\"object\"},\"Empty\":{\"type\":\"object\",\"title\":\"Empty response model for AWS + CORS use\"},\"StringResponse\":{\"type\":\"string\",\"title\":\"String-type workaround for API Gateway\"}}}};\n } else {\n // Get the path of the location, as we will need to adjust the specFileURL, if the path is relative\n if (isRelativeUrl(specFileUrl)) {\n specFileUrl = (location.href + specFileUrl).replace(/(:\\/\\/)|(\\/)+/g, \"$1$2\"); // full URL, replace '//' with '/' except when proceeded by ':'\n }\n swaggerConfig.url = specFileUrl;\n }\n\n SwaggerUIBundle(swaggerConfig);\n</script>\n</body>\n</html>\n"
}
}
},
"requestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"passthroughBehavior": "when_no_match"
}
}
}
},
"components": {
"schemas": {
"Empty": {
"type": "object",
"title": "Empty response model for AWS + CORS use"
},
"StringResponse": {
"type": "string",
"title": "String-type workaround for API Gateway"
}
}
}
}
8 changes: 7 additions & 1 deletion src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,17 @@ function addRequiredSchemas(params) {
}

function addSpecFileEndpoint(params) {
const { originalSpecObj, specObj, config } = params;
const { originalSpecObj, specObj, config, serverUrl } = params;

// We can filter out the AMAZON_INTEGRATION_PROP properties, as they are quite large
const filteredSwagger = getSmallerSwaggerForWebsite(originalSpecObj);

// Add the serverUrl to the list of servers if it is not already in the list
const urls = new Set(filteredSwagger.servers.map((i) => i.url));
if (!urls.has(serverUrl)) {
filteredSwagger.servers.unshift({ url: serverUrl });
}

specObj.paths[config.specFileEndpoint] = {
get: {
'x-ignore': true,
Expand Down
Loading

0 comments on commit 827076f

Please sign in to comment.