Skip to content

Commit a0e9923

Browse files
authored
fix(serverVariables): refresh state on definition change (#7821)
* fix(serverVariables): refresh state on definition change * test(serverVariables): urls with server variables assertions
1 parent 2b30a34 commit a0e9923

File tree

5 files changed

+170
-5
lines changed

5 files changed

+170
-5
lines changed

src/core/plugins/oas3/components/servers.jsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,33 @@ export default class Servers extends React.Component {
3131
setServerVariableValue,
3232
getServerVariable
3333
} = nextProps
34-
3534
if (this.props.currentServer !== nextProps.currentServer || this.props.servers !== nextProps.servers) {
3635
// Server has changed, we may need to set default values
3736
let currentServerDefinition = servers
3837
.find(v => v.get("url") === nextProps.currentServer)
39-
38+
let prevServerDefinition = this.props.servers
39+
.find(v => v.get("url") === this.props.currentServer) || OrderedMap()
40+
4041
if(!currentServerDefinition) {
4142
return this.setServer(servers.first().get("url"))
4243
}
43-
44+
45+
let prevServerVariableDefs = prevServerDefinition.get("variables") || OrderedMap()
46+
let prevServerVariableDefaultKey = prevServerVariableDefs.find(v => v.get("default")) || OrderedMap()
47+
let prevServerVariableDefaultValue = prevServerVariableDefaultKey.get("default")
48+
4449
let currentServerVariableDefs = currentServerDefinition.get("variables") || OrderedMap()
45-
50+
let currentServerVariableDefaultKey = currentServerVariableDefs.find(v => v.get("default")) || OrderedMap()
51+
let currentServerVariableDefaultValue = currentServerVariableDefaultKey.get("default")
52+
4653
currentServerVariableDefs.map((val, key) => {
4754
let currentValue = getServerVariable(nextProps.currentServer, key)
55+
56+
// note: it is possible for both key/val to be the same across definitions,
57+
// but we will try to detect a change in default values between definitions
4858
// only set the default value if the user hasn't set one yet
49-
if(!currentValue) {
59+
// or if the definition appears to have changed
60+
if (!currentValue || prevServerVariableDefaultValue !== currentServerVariableDefaultValue) {
5061
setServerVariableValue({
5162
server: nextProps.currentServer,
5263
key,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
urls:
2+
- name: One
3+
url: /documents/features/urls/server-variables-1.yaml
4+
- name: Two
5+
url: /documents/features/urls/server-variables-2.yaml
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Server Variables - One
4+
description: sample OAS 3 definition to test server variables with urls
5+
version: 1.0.0
6+
servers:
7+
- url: "https://localhost:3200{basePath}"
8+
variables:
9+
basePath:
10+
default: "/oneFirstUrl"
11+
- url: "http://localhost:3201{basePath}"
12+
variables:
13+
basePath:
14+
default: "/oneSecondUrl"
15+
paths:
16+
/a:
17+
post:
18+
summary: simple service A
19+
requestBody:
20+
content:
21+
'application/json':
22+
schema:
23+
properties:
24+
foo:
25+
type: string
26+
bar:
27+
type: string
28+
required:
29+
- foo
30+
type: object
31+
required: true
32+
responses:
33+
200:
34+
description: OK
35+
content:
36+
application/json:
37+
schema:
38+
type: 'string'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Server Variables - Two
4+
description: sample OAS 3 definition to test server variables with urls
5+
version: 1.0.0
6+
servers:
7+
- url: "https://localhost:3200{basePath}"
8+
variables:
9+
basePath:
10+
default: "/twoFirstUrl"
11+
- url: "http://localhost:3201{basePath}"
12+
variables:
13+
basePath:
14+
default: "/twoSecondUrl"
15+
paths:
16+
/b:
17+
post:
18+
summary: simple service B
19+
requestBody:
20+
content:
21+
'application/json':
22+
schema:
23+
properties:
24+
foo:
25+
type: string
26+
bar:
27+
type: string
28+
required:
29+
- foo
30+
type: object
31+
required: true
32+
responses:
33+
200:
34+
description: OK
35+
content:
36+
application/json:
37+
schema:
38+
type: 'string'

test/e2e-cypress/tests/features/urls.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,76 @@ describe("configuration options: `urls` and `urls.primaryName`", () => {
3636
.should("equal", "/documents/features/urls/2.yaml")
3737
})
3838
})
39+
40+
describe("urls with server variables", () => {
41+
it("should compute a url and default server variables", () => {
42+
cy.visit("/?configUrl=/configs/urls-server-variables.yaml")
43+
.get("code")
44+
.should("have.text", "https://localhost:3200/oneFirstUrl")
45+
.get("tr > :nth-child(1)")
46+
.should("have.text", "basePath")
47+
.get("input")
48+
.should("have.value", "/oneFirstUrl")
49+
})
50+
it("should change server variables", () => {
51+
cy.visit("/?configUrl=/configs/urls-server-variables.yaml")
52+
.get("code")
53+
.should("have.text", "https://localhost:3200/oneFirstUrl")
54+
.get("tr > :nth-child(1)")
55+
.should("have.text", "basePath")
56+
.get("input")
57+
.should("have.value", "/oneFirstUrl")
58+
.get(".servers > label > select")
59+
.eq(0)
60+
.select(1)
61+
.get("input")
62+
.should("have.value", "/oneSecondUrl")
63+
})
64+
it("should select and compute second url", () => {
65+
cy.visit("/?configUrl=/configs/urls-server-variables.yaml")
66+
.get("select > option")
67+
.eq(1)
68+
.should("have.text", "Two")
69+
.get("select")
70+
.eq(0)
71+
.select(1)
72+
.get("code")
73+
.should("have.text", "https://localhost:3200/twoFirstUrl")
74+
.get("input")
75+
.should("have.value", "/twoFirstUrl")
76+
})
77+
it("should select second url, then toggle back to first url", () => {
78+
cy.visit("/?configUrl=/configs/urls-server-variables.yaml")
79+
.get("select > option")
80+
.get("select")
81+
.eq(0)
82+
.select(1)
83+
.get("input")
84+
.should("have.value", "/twoFirstUrl")
85+
// toggle url back
86+
.get("select")
87+
.eq(0)
88+
.select(0)
89+
.get("code")
90+
.should("have.text", "https://localhost:3200/oneFirstUrl")
91+
.get("input")
92+
.should("have.value", "/oneFirstUrl")
93+
})
94+
it("should change server variables, then select second url, and maintain server variables index", () => {
95+
cy.visit("/?configUrl=/configs/urls-server-variables.yaml")
96+
.get(".servers > label >select")
97+
.eq(0)
98+
.select(1)
99+
.get("input")
100+
.should("have.value", "/oneSecondUrl")
101+
// change url
102+
.get("select > option")
103+
.get("select")
104+
.eq(0)
105+
.select(1)
106+
.get("input")
107+
.should("have.value", "/twoSecondUrl")
108+
.get("input")
109+
.should("have.value", "/twoSecondUrl")
110+
})
111+
})

0 commit comments

Comments
 (0)