Skip to content

Commit ef4a23e

Browse files
committed
Removed operational chaining parameter
1 parent a92f479 commit ef4a23e

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/util.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const operationsList = ["get", "post", "put", "delete", "patch", "options", "hea
2626
*/
2727
function fixServers (server, path) {
2828
// Server url starting with "/" tells that it is not an http(s) url
29-
if (server.url?.startsWith("/")) {
29+
if (server.url && server.url.startsWith("/")) {
3030
const inUrl = new URL(path);
3131
const finalUrl = inUrl.protocol + "//" + inUrl.hostname + server.url;
3232
server.url = finalUrl;
@@ -39,7 +39,7 @@ function fixServers (server, path) {
3939
* be at root, path or operation's level
4040
*/
4141
function fixOasRelativeServers (schema, filePath) {
42-
if (schema?.openapi && (filePath?.startsWith("http:") || filePath?.startsWith("https:"))) {
42+
if (schema.openapi && (filePath && (filePath.startsWith("http:") || filePath.startsWith("https:")))) {
4343
/**
4444
* From OpenAPI v3 spec for Server object's url property: "REQUIRED. A URL to the target host.
4545
* This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where
@@ -48,7 +48,9 @@ function fixOasRelativeServers (schema, filePath) {
4848
* However, interpretation of the spec says that relative paths for servers should take into account the hostname that
4949
* serves the OpenAPI file.
5050
*/
51-
schema.servers?.map(server => fixServers(server, filePath)); // Root level servers array's fixup
51+
if (schema.servers) {
52+
schema.servers.map(server => fixServers(server, filePath)); // Root level servers array's fixup
53+
}
5254

5355
// Path or Operation level servers array's fixup
5456
Object.keys(schema.paths).forEach(path => {
@@ -60,7 +62,9 @@ function fixOasRelativeServers (schema, filePath) {
6062
}
6163
else if (operationsList.includes(opItem)) {
6264
// servers at operation level
63-
pathItem[opItem].servers?.map(server => fixServers(server, filePath));
65+
if (pathItem[opItem].servers) {
66+
pathItem[opItem].servers.map(server => fixServers(server, filePath));
67+
}
6468
}
6569
});
6670
});

test/specs/oas-relative-servers/v3-relative-servers.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("Servers with relative paths in OpenAPI v3 files", () => {
5454
let apiJson = await SwaggerParser.parse(RELATIVE_SERVERS_OAS3_URL_2);
5555
expect(apiJson.servers[0].url).to.equal("https://foo.my.cloud/api/v3");
5656
expect(apiJson.paths["/pet"].servers[0].url).to.equal("https://foo.my.cloud/api/v4");
57-
expect(apiJson.paths["/pet"]?.get.servers[0].url).to.equal("https://foo.my.cloud/api/v5");
57+
expect(apiJson.paths["/pet"].get.servers[0].url).to.equal("https://foo.my.cloud/api/v5");
5858
}
5959
catch (error) {
6060
console.error("\n\nError in relative servers at root, path and operations test case:", error);
@@ -70,7 +70,7 @@ describe("Servers with relative paths in OpenAPI v3 files", () => {
7070
let apiJson = await SwaggerParser.parse(path.rel("./v3-relative-server.json"));
7171
expect(apiJson.servers[0].url).to.equal("/api/v3");
7272
expect(apiJson.paths["/pet"].servers[0].url).to.equal("/api/v4");
73-
expect(apiJson.paths["/pet"]?.get.servers[0].url).to.equal("/api/v5");
73+
expect(apiJson.paths["/pet"].get.servers[0].url).to.equal("/api/v5");
7474
}
7575
catch (error) {
7676
console.error("\n\nError in relative servers at root but local file import test case:", error);
@@ -86,7 +86,7 @@ describe("Servers with relative paths in OpenAPI v3 files", () => {
8686
let apiJson = await SwaggerParser.parse(path.rel("./v3-non-relative-server.json"));
8787
expect(apiJson.servers[0].url).to.equal("https://petstore3.swagger.com/api/v3");
8888
expect(apiJson.paths["/pet"].servers[0].url).to.equal("https://petstore3.swagger.com/api/v4");
89-
expect(apiJson.paths["/pet"]?.get.servers[0].url).to.equal("https://petstore3.swagger.com/api/v5");
89+
expect(apiJson.paths["/pet"].get.servers[0].url).to.equal("https://petstore3.swagger.com/api/v5");
9090
}
9191
catch (error) {
9292
console.error("\n\nError in non-relative servers at root but local file import test case:", error);

0 commit comments

Comments
 (0)