Skip to content

Commit da237e5

Browse files
authored
Merge pull request #1192 from shockey/ft/ui-3820-path-operation-servers
OAS3: Path & Operation Servers
2 parents 1bdb45a + 0b5b380 commit da237e5

File tree

4 files changed

+123
-30
lines changed

4 files changed

+123
-30
lines changed

src/execute/index.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export function buildRequest(options) {
104104

105105
// Base Template
106106
let req = {
107-
url: baseUrl({spec, scheme, contextUrl, server, serverVariables}),
107+
url: '',
108108
credentials: 'same-origin',
109109
headers: {
110110
// This breaks CORSs... removing this line... probably breaks oAuth. Need to address that
@@ -124,6 +124,15 @@ export function buildRequest(options) {
124124
req.userFetch = userFetch
125125
}
126126

127+
const operationRaw = getOperationRaw(spec, operationId)
128+
if (!operationRaw) {
129+
throw new OperationNotFoundError(`Operation ${operationId} not found`)
130+
}
131+
132+
const {operation = {}, method, pathName} = operationRaw
133+
134+
req.url += baseUrl({spec, scheme, contextUrl, server, serverVariables, pathName, method})
135+
127136
// Mostly for testing
128137
if (!operationId) {
129138
// Not removing req.cookies causes testing issues and would
@@ -134,13 +143,6 @@ export function buildRequest(options) {
134143
return req
135144
}
136145

137-
const operationRaw = getOperationRaw(spec, operationId)
138-
if (!operationRaw) {
139-
throw new OperationNotFoundError(`Operation ${operationId} not found`)
140-
}
141-
142-
const {operation = {}, method, pathName} = operationRaw
143-
144146
req.url += pathName // Have not yet replaced the path parameters
145147
req.method = (`${method}`).toUpperCase()
146148

@@ -240,8 +242,11 @@ export function baseUrl(obj) {
240242
return specIsOAS3 ? oas3BaseUrl(obj) : swagger2BaseUrl(obj)
241243
}
242244

243-
function oas3BaseUrl({spec, server, contextUrl, serverVariables = {}}) {
244-
const servers = spec.servers
245+
function oas3BaseUrl({spec, pathName, method, server, contextUrl, serverVariables = {}}) {
246+
const servers =
247+
getIn(spec, ['paths', pathName, (method || '').toLowerCase(), 'servers']) ||
248+
getIn(spec, ['paths', pathName, 'servers']) ||
249+
getIn(spec, ['servers'])
245250

246251
let selectedServerUrl = ''
247252
let selectedServerObj = null

test.js

Whitespace-only changes.

test/execute/main.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,27 @@ describe('execute', () => {
3737
})
3838
})
3939

40-
it('should include host + http + baseUrl', function () {
41-
// Given
42-
const spec = {
43-
host: 'swagger.io',
44-
basePath: '/v1',
45-
}
46-
47-
// When
48-
const req = buildRequest({spec})
49-
50-
// Then
51-
expect(req).toEqual({
52-
url: 'http://swagger.io/v1',
53-
credentials: 'same-origin',
54-
headers: { }
55-
})
56-
})
57-
5840
it('should include host + port', function () {
5941
// Given
6042
const spec = {
6143
host: 'foo.com:8081',
6244
basePath: '/v1',
45+
paths: {
46+
'/': {
47+
get: {
48+
operationId: 'foo'
49+
}
50+
}
51+
}
6352
}
6453

6554
// When
66-
const req = buildRequest({spec})
55+
const req = buildRequest({spec, operationId: 'foo'})
6756

6857
// Then
6958
expect(req).toEqual({
70-
url: 'http://foo.com:8081/v1',
59+
url: 'http://foo.com:8081/v1/',
60+
method: 'GET',
7161
credentials: 'same-origin',
7262
headers: { }
7363
})

test/oas3/execute/main.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,104 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
471471

472472
expect(res).toEqual('https://petstore.net')
473473
})
474+
it('should use an explicitly chosen server at the operation level', function () {
475+
const spec = {
476+
openapi: '3.0.0',
477+
servers: [
478+
{
479+
url: 'https://petstore.com'
480+
},
481+
{
482+
url: 'https://petstore.net'
483+
}
484+
],
485+
paths: {
486+
'/': {
487+
get: {
488+
servers: [
489+
{
490+
url: 'https://petstore-operation.net/{path}',
491+
variables: {
492+
path: {
493+
default: 'foobar'
494+
}
495+
}
496+
}
497+
]
498+
}
499+
}
500+
}
501+
}
502+
503+
const res = baseUrl({
504+
spec,
505+
server: 'https://petstore-operation.net/{path}',
506+
pathName: '/',
507+
method: 'GET'
508+
})
509+
510+
const resWithVariables = baseUrl({
511+
spec,
512+
server: 'https://petstore-operation.net/{path}',
513+
serverVariables: {
514+
path: 'fizzbuzz'
515+
},
516+
pathName: '/',
517+
method: 'GET'
518+
})
519+
520+
expect(res).toEqual('https://petstore-operation.net/foobar')
521+
expect(resWithVariables).toEqual('https://petstore-operation.net/fizzbuzz')
522+
})
523+
524+
it('should use an explicitly chosen server at the path level', function () {
525+
const spec = {
526+
openapi: '3.0.0',
527+
servers: [
528+
{
529+
url: 'https://petstore.com'
530+
},
531+
{
532+
url: 'https://petstore.net'
533+
}
534+
],
535+
paths: {
536+
'/': {
537+
servers: [
538+
{
539+
url: 'https://petstore-path.net/{path}',
540+
variables: {
541+
path: {
542+
default: 'foobar'
543+
}
544+
}
545+
}
546+
],
547+
get: {}
548+
}
549+
}
550+
}
551+
552+
const res = baseUrl({
553+
spec,
554+
server: 'https://petstore-path.net/{path}',
555+
pathName: '/',
556+
method: 'GET'
557+
})
558+
559+
const resWithVariables = baseUrl({
560+
spec,
561+
server: 'https://petstore-path.net/{path}',
562+
serverVariables: {
563+
path: 'fizzbuzz'
564+
},
565+
pathName: '/',
566+
method: 'GET'
567+
})
568+
569+
expect(res).toEqual('https://petstore-path.net/foobar')
570+
expect(resWithVariables).toEqual('https://petstore-path.net/fizzbuzz')
571+
})
474572
it('should not use an explicitly chosen server that is not present in the spec', function () {
475573
const spec = {
476574
openapi: '3.0.0',

0 commit comments

Comments
 (0)