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

Fix for issue#153-relative server paths in OpenAPI #181

Merged
merged 20 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,4 @@ Thanks to these awesome companies for their support of Open Source developers
[![GitHub](https://apitools.dev/img/badges/github.svg)](https://github.com/open-source)
[![NPM](https://apitools.dev/img/badges/npm.svg)](https://www.npmjs.com/)
[![Coveralls](https://apitools.dev/img/badges/coveralls.svg)](https://coveralls.io)
[![Travis CI](https://apitools.dev/img/badges/travis-ci.svg)](https://travis-ci.com)
[![SauceLabs](https://apitools.dev/img/badges/sauce-labs.svg)](https://saucelabs.com)
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ SwaggerParser.prototype.parse = async function (path, api, options, callback) {
`Swagger Parser only supports versions ${supportedVersions.join(", ")}`
);
}

// This is an OpenAPI v3 schema, check if the "servers" have any relative paths and
// fix them if the content was pulled from a web resource
util.fixOasRelativeServers(schema, args.path);
}

// Looks good!
Expand Down
66 changes: 66 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const util = require("util");
const url = require("@apidevtools/json-schema-ref-parser/lib/util/url");

exports.format = util.format;
exports.inherits = util.inherits;
Expand All @@ -9,3 +10,68 @@ exports.inherits = util.inherits;
* Regular Expression that matches Swagger path params.
*/
exports.swaggerParamRegExp = /\{([^/}]+)}/g;

/**
* List of HTTP verbs used for OperationItem as per the Swagger specification
*/
const operationsList = ["get", "post", "put", "delete", "patch", "options", "head", "trace"];

/**
* This function takes in a Server object, checks if it has relative path
* and then fixes it as per the path url
*
* @param {object} server - The server object to be fixed
* @param {string} path - The path (an http/https url) from where the file was downloaded
* @returns {object} - The fixed server object
*/
function fixServers (server, path) {
// Server url starting with "/" tells that it is not an http(s) url
if (server.url && server.url.startsWith("/")) {
const inUrl = url.parse(path);
const finalUrl = inUrl.protocol + "//" + inUrl.hostname + server.url;
server.url = finalUrl;
return server;
}
}

/**
* This function helps fix the relative servers in the API definition file
* be at root, path or operation's level
*/
function fixOasRelativeServers (schema, filePath) {
if (schema.openapi && (filePath && (filePath.startsWith("http:") || filePath.startsWith("https:")))) {
/**
* From OpenAPI v3 spec for Server object's url property: "REQUIRED. A URL to the target host.
* This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where
* the OpenAPI document is being served."
* Further, the spec says that "servers" property can show up at root level, in 'Path Item' object or in 'Operation' object.
* However, interpretation of the spec says that relative paths for servers should take into account the hostname that
* serves the OpenAPI file.
*/
if (schema.servers) {
schema.servers.map(server => fixServers(server, filePath)); // Root level servers array's fixup
}

// Path or Operation level servers array's fixup
Object.keys(schema.paths).forEach(path => {
const pathItem = schema.paths[path];
Object.keys(pathItem).forEach(opItem => {
if (opItem === "servers") {
// servers at pathitem level
pathItem[opItem].map(server => fixServers(server, filePath));
}
else if (operationsList.includes(opItem)) {
// servers at operation level
if (pathItem[opItem].servers) {
pathItem[opItem].servers.map(server => fixServers(server, filePath));
}
}
});
});
}
else {
// Do nothing and return
}
}

exports.fixOasRelativeServers = fixOasRelativeServers;
3,824 changes: 3,395 additions & 429 deletions online/js/bundle.js

Large diffs are not rendered by default.

548 changes: 292 additions & 256 deletions online/js/bundle.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions online/js/bundle.min.js

Large diffs are not rendered by default.

1,470 changes: 900 additions & 570 deletions online/js/bundle.min.js.map

Large diffs are not rendered by default.

Loading