|
1 | 1 | "use strict";
|
2 | 2 |
|
3 | 3 | const SwaggerParser = require("../../../lib");
|
4 |
| -const {expect} = require("chai"); |
| 4 | +const { expect } = require("chai"); |
5 | 5 | const path = require("../../utils/path");
|
6 | 6 | const $RefParser = require("@apidevtools/json-schema-ref-parser");
|
7 | 7 | const sinon = require("sinon");
|
8 | 8 |
|
9 |
| -//Import of our fixed OpenAPI JSON files |
| 9 | +// Import of our fixed OpenAPI JSON files |
10 | 10 | const v3RelativeServerJson = require("./v3-relative-server.json");
|
11 | 11 | const v3RelativeServerPathsOpsJson = require("./v3-relative-server-paths-ops.json");
|
12 | 12 | const v3NonRelativeServerJson = require("./v3-non-relative-server.json");
|
13 | 13 |
|
14 |
| - //Petstore v3 json has relative path in "servers" |
| 14 | +// Petstore v3 json has relative path in "servers" |
15 | 15 | const RELATIVE_SERVERS_OAS3_URL_1 = "https://petstore3.swagger.io/api/v3/openapi.json";
|
16 | 16 |
|
17 |
| -//This will have "servers" at paths & operations level |
18 |
| -const RELATIVE_SERVERS_OAS3_URL_2 = "https://foo.my.cloud/v1/petstore/relativeservers"; |
| 17 | +// This will have "servers" at paths & operations level |
| 18 | +const RELATIVE_SERVERS_OAS3_URL_2 = "https://foo.my.cloud/v1/petstore/relativeservers"; |
19 | 19 |
|
20 |
| -describe("Servers with relative paths in OpenAPI v3 files",() => { |
| 20 | +describe("Servers with relative paths in OpenAPI v3 files", () => { |
21 | 21 | let mockParse;
|
22 |
| - before(function () { |
23 |
| - //Mock the parse function |
24 |
| - mockParse = sinon.stub($RefParser.prototype,'parse'); |
| 22 | + |
| 23 | + before(() => { |
| 24 | + // Mock the parse function |
| 25 | + mockParse = sinon.stub($RefParser.prototype, "parse"); |
25 | 26 | });
|
26 | 27 |
|
27 |
| - after(function () { |
28 |
| - //Restore the parse function |
| 28 | + after(() => { |
| 29 | + // Restore the parse function |
29 | 30 | $RefParser.prototype.parse.restore();
|
30 | 31 | });
|
31 | 32 |
|
32 |
| - it("should fix relative servers path in the file fetched from url",async()=>{ |
| 33 | + it("should fix relative servers path in the file fetched from url", async () => { |
33 | 34 | try {
|
34 | 35 | mockParse.callsFake(() => {
|
35 |
| - //to prevent edit of the original JSON |
| 36 | + // to prevent edit of the original JSON |
36 | 37 | return JSON.parse(JSON.stringify(v3RelativeServerJson));
|
37 | 38 | });
|
38 | 39 | let apiJson = await SwaggerParser.parse(RELATIVE_SERVERS_OAS3_URL_1);
|
39 | 40 | expect(apiJson.servers[0].url).to.equal("https://petstore3.swagger.io/api/v3");
|
40 |
| - }catch (error) { |
41 |
| - console.error("\n\nError in relative servers at root test case:",error); |
| 41 | + } |
| 42 | + catch (error) { |
| 43 | + console.error("\n\nError in relative servers at root test case:", error); |
42 | 44 | throw error;
|
43 | 45 | }
|
44 | 46 | });
|
45 | 47 |
|
46 |
| - it("should fix relative servers at root, path and operations level in the file fetched from url",async()=>{ |
| 48 | + it("should fix relative servers at root, path and operations level in the file fetched from url", async () => { |
47 | 49 | try {
|
48 | 50 | mockParse.callsFake(() => {
|
49 |
| - //to prevent edit of the original JSON |
| 51 | + // to prevent edit of the original JSON |
50 | 52 | return JSON.parse(JSON.stringify(v3RelativeServerPathsOpsJson));
|
51 | 53 | });
|
52 | 54 | let apiJson = await SwaggerParser.parse(RELATIVE_SERVERS_OAS3_URL_2);
|
53 | 55 | expect(apiJson.servers[0].url).to.equal("https://foo.my.cloud/api/v3");
|
54 | 56 | expect(apiJson.paths["/pet"].servers[0].url).to.equal("https://foo.my.cloud/api/v4");
|
55 |
| - expect(apiJson.paths["/pet"]["get"].servers[0].url).to.equal("https://foo.my.cloud/api/v5"); |
56 |
| - }catch (error) { |
57 |
| - console.error("\n\nError in relative servers at root test case:",error); |
| 57 | + expect(apiJson.paths["/pet"]?.get.servers[0].url).to.equal("https://foo.my.cloud/api/v5"); |
| 58 | + } |
| 59 | + catch (error) { |
| 60 | + console.error("\n\nError in relative servers at root, path and operations test case:", error); |
58 | 61 | throw error;
|
59 | 62 | }
|
60 | 63 | });
|
61 | 64 |
|
62 |
| - it("should parse but no change to relative servers path in local file import",async()=>{ |
| 65 | + it("should parse but no change to relative servers path in local file import", async () => { |
63 | 66 | try {
|
64 | 67 | mockParse.callsFake(() => {
|
65 | 68 | return JSON.parse(JSON.stringify(v3RelativeServerPathsOpsJson));
|
66 | 69 | });
|
67 | 70 | let apiJson = await SwaggerParser.parse(path.rel("./v3-relative-server.json"));
|
68 | 71 | expect(apiJson.servers[0].url).to.equal("/api/v3");
|
69 | 72 | expect(apiJson.paths["/pet"].servers[0].url).to.equal("/api/v4");
|
70 |
| - expect(apiJson.paths["/pet"]["get"].servers[0].url).to.equal("/api/v5"); |
71 |
| - }catch (error) { |
72 |
| - console.error("\n\nError in relative servers at root test case:",error); |
| 73 | + expect(apiJson.paths["/pet"]?.get.servers[0].url).to.equal("/api/v5"); |
| 74 | + } |
| 75 | + catch (error) { |
| 76 | + console.error("\n\nError in relative servers at root but local file import test case:", error); |
73 | 77 | throw error;
|
74 | 78 | }
|
75 | 79 | });
|
76 | 80 |
|
77 |
| - it("should parse but no change to non-relative servers path in local file import",async()=>{ |
| 81 | + it("should parse but no change to non-relative servers path in local file import", async () => { |
78 | 82 | try {
|
79 | 83 | mockParse.callsFake(() => {
|
80 | 84 | return JSON.parse(JSON.stringify(v3NonRelativeServerJson));
|
81 | 85 | });
|
82 | 86 | let apiJson = await SwaggerParser.parse(path.rel("./v3-non-relative-server.json"));
|
83 | 87 | expect(apiJson.servers[0].url).to.equal("https://petstore3.swagger.com/api/v3");
|
84 | 88 | expect(apiJson.paths["/pet"].servers[0].url).to.equal("https://petstore3.swagger.com/api/v4");
|
85 |
| - expect(apiJson.paths["/pet"]["get"].servers[0].url).to.equal("https://petstore3.swagger.com/api/v5"); |
86 |
| - }catch (error) { |
87 |
| - console.error("\n\nError in relative servers at root test case:",error); |
| 89 | + expect(apiJson.paths["/pet"]?.get.servers[0].url).to.equal("https://petstore3.swagger.com/api/v5"); |
| 90 | + } |
| 91 | + catch (error) { |
| 92 | + console.error("\n\nError in non-relative servers at root but local file import test case:", error); |
88 | 93 | throw error;
|
89 | 94 | }
|
90 | 95 | });
|
|
0 commit comments