Skip to content

Commit 3b90d30

Browse files
authored
Merge branch 'master' into ft/oas3-authorization
2 parents 80277b8 + f335956 commit 3b90d30

File tree

10 files changed

+146
-17
lines changed

10 files changed

+146
-17
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-client",
3-
"version": "3.2.1",
3+
"version": "3.2.2",
44
"description": "SwaggerJS - a collection of interfaces for OAI specs",
55
"main": "dist/index.js",
66
"repository": "[email protected]:swagger-api/swagger-js.git",
@@ -50,7 +50,7 @@
5050
"eslint-config-airbnb-base": "^11.1.1",
5151
"eslint-plugin-import": "^2.2.0",
5252
"expect": "^1.20.2",
53-
"fetch-mock": "~5.12.0",
53+
"fetch-mock": "^5.12.0",
5454
"glob": "^7.1.1",
5555
"json-loader": "^0.5.4",
5656
"license-checker": "^8.0.3",

src/execute/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ function oas3BaseUrl({spec, server, contextUrl, serverVariables = {}}) {
241241
let selectedServerUrl = ''
242242
let selectedServerObj = null
243243

244-
if (server) {
244+
if (server && servers) {
245245
const serverUrls = servers.map(srv => srv.url)
246246

247247
if (serverUrls.indexOf(server) > -1) {

src/execute/swagger2/build-request.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
6666
const schema = securityDef[key]
6767
const {type} = schema
6868
const accessToken = token && token.access_token
69-
const tokenType = token && token.token_type
69+
let tokenType = token && token.token_type
7070

7171
if (auth) {
7272
if (type === 'apiKey') {
@@ -84,7 +84,8 @@ export function applySecurities({request, securities = {}, operation = {}, spec}
8484
}
8585
}
8686
else if (type === 'oauth2' && accessToken) {
87-
result.headers.authorization = `${tokenType || 'Bearer'} ${accessToken}`
87+
tokenType = (!tokenType || tokenType.toLowerCase() === 'bearer') ? 'Bearer' : tokenType
88+
result.headers.authorization = `${tokenType} ${accessToken}`
8889
}
8990
}
9091
}

src/helpers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isObject from 'lodash/isObject'
2+
import startsWith from 'lodash/startsWith'
23

34
const toLower = str => String.prototype.toLowerCase.call(str)
45
const escapeString = (str) => {
@@ -12,7 +13,7 @@ export function isOAS3(spec) {
1213
return false
1314
}
1415

15-
return oasVersion.startsWith('3.0.0')
16+
return startsWith(oasVersion, '3')
1617
}
1718

1819
export function isSwagger2(spec) {
@@ -21,7 +22,7 @@ export function isSwagger2(spec) {
2122
return false
2223
}
2324

24-
return swaggerVersion.startsWith('2')
25+
return startsWith(swaggerVersion, '2')
2526
}
2627

2728
// Strategy for determining operationId

src/http.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'isomorphic-fetch'
22
import qs from 'qs'
33
import jsYaml from 'js-yaml'
4-
import assign from 'lodash/assign'
54
import isString from 'lodash/isString'
65

76
// For testing

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import assign from 'lodash/assign'
3+
import startsWith from 'lodash/startsWith'
34
import Url from 'url'
45
import Http, {makeHttp, serializeRes, serializeHeaders} from './http'
56
import Resolver, {clearCache} from './resolver'
@@ -68,7 +69,9 @@ Swagger.prototype = {
6869
return Swagger.resolve({
6970
spec: this.spec,
7071
url: this.url,
71-
allowMetaPatches: this.allowMetaPatches
72+
allowMetaPatches: this.allowMetaPatches,
73+
requestInterceptor: this.requestInterceptor || null,
74+
responseInterceptor: this.responseInterceptor || null
7275
}).then((obj) => {
7376
this.originalSpec = this.spec
7477
this.spec = obj.spec
@@ -82,7 +85,7 @@ Swagger.prototype.applyDefaults = function () {
8285
const spec = this.spec
8386
const specUrl = this.url
8487
// TODO: OAS3: support servers here
85-
if (specUrl && specUrl.startsWith('http')) {
88+
if (specUrl && startsWith(specUrl, 'http')) {
8689
const parsed = Url.parse(specUrl)
8790
if (!spec.host) {
8891
spec.host = parsed.host

src/resolver.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import Http from './http'
22
import mapSpec, {plugins} from './specmap'
33
import {normalizeSwagger} from './helpers'
44

5-
export function makeFetchJSON(http) {
5+
export function makeFetchJSON(http, opts = {}) {
6+
const {requestInterceptor, responseInterceptor} = opts
67
return (docPath) => {
78
return http({
89
url: docPath,
910
loadSpec: true,
11+
requestInterceptor,
12+
responseInterceptor,
1013
headers: {
1114
Accept: 'application/json'
1215
},
@@ -23,10 +26,17 @@ export function clearCache() {
2326
plugins.refs.clearCache()
2427
}
2528

26-
export default function resolve({
27-
http, fetch, spec, url, baseDoc, mode, allowMetaPatches = true,
28-
modelPropertyMacro, parameterMacro
29-
}) {
29+
export default function resolve(obj) {
30+
const {
31+
fetch, spec, url, mode, allowMetaPatches = true,
32+
modelPropertyMacro, parameterMacro, requestInterceptor,
33+
responseInterceptor
34+
} = obj
35+
36+
let {http, baseDoc} = obj
37+
38+
// console.log(obj)
39+
3040
// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
3141
// need to fix and pick one.
3242
baseDoc = baseDoc || url
@@ -36,7 +46,7 @@ export default function resolve({
3646
http = fetch || http || Http
3747

3848
if (!spec) {
39-
return makeFetchJSON(http)(baseDoc).then(doResolve)
49+
return makeFetchJSON(http, {requestInterceptor, responseInterceptor})(baseDoc).then(doResolve)
4050
}
4151

4252
return doResolve(spec)
@@ -47,7 +57,7 @@ export default function resolve({
4757
}
4858

4959
// Build a json-fetcher ( ie: give it a URL and get json out )
50-
plugins.refs.fetchJSON = makeFetchJSON(http)
60+
plugins.refs.fetchJSON = makeFetchJSON(http, {requestInterceptor, responseInterceptor})
5161

5262
const plugs = [plugins.refs]
5363

test/index-authorizations.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,47 @@ describe('(instance) #execute', function () {
175175
})
176176
})
177177

178+
it('should replace any occurrence of `bearer` with `Bearer`', function () {
179+
const spec = {
180+
securityDefinitions: {
181+
testBearer: {
182+
type: 'oauth2',
183+
}
184+
},
185+
paths: {
186+
'/pet': {
187+
get: {
188+
operationId: 'getPets',
189+
security: [{testBearer: []}]
190+
}
191+
}
192+
}
193+
}
194+
195+
const authorizations = {
196+
testBearer: {
197+
token: {
198+
token_type: 'BeArEr',
199+
access_token: 'one two'
200+
}
201+
}
202+
}
203+
204+
return Swagger({spec, authorizations}).then((client) => {
205+
const http = createSpy()
206+
client.execute({http, operationId: 'getPets'})
207+
expect(http.calls.length).toEqual(1)
208+
expect(http.calls[0].arguments[0]).toEqual({
209+
credentials: 'same-origin',
210+
headers: {
211+
authorization: 'Bearer one two'
212+
},
213+
method: 'GET',
214+
url: '/pet'
215+
})
216+
})
217+
})
218+
178219
it('should add global securites', function () {
179220
const spec = {
180221
securityDefinitions: {

test/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,17 +494,27 @@ describe('constructor', () => {
494494

495495
describe('interceptor', function () {
496496
beforeEach(() => {
497+
Swagger.clearCache()
497498
const xapp = xmock()
498499
xapp
499500
.get('http://petstore.swagger.io/v2/swagger.json', () => require('./data/petstore.json'))
500501
.get('http://petstore.swagger.io/v2/pet/3', () => ({id: 3}))
501502
.get('http://petstore.swagger.io/v2/pet/4', () => ({id: 4}))
503+
.get('http://petstore.swagger.io/v2/ref.json', () => ({b: 2}))
504+
.get('http://petstore.swagger.io/v2/base.json', () => (
505+
{
506+
$ref: 'http://petstore.swagger.io/v2/ref.json#b'
507+
}
508+
))
502509
})
503510

504511
it('should support request interceptor', function (cb) {
505512
new Swagger({
506513
url: 'http://petstore.swagger.io/v2/swagger.json',
507514
requestInterceptor: (req) => {
515+
if (req.loadSpec) {
516+
return req
517+
}
508518
req.url = 'http://petstore.swagger.io/v2/pet/4'
509519
}
510520
}).then((client) => {
@@ -528,5 +538,44 @@ describe('constructor', () => {
528538
})
529539
}, cb)
530540
})
541+
542+
it('should support request interceptor when fetching a spec and remote ref', function (cb) {
543+
const spy = createSpy().andCall(a => a)
544+
new Swagger({
545+
url: 'http://petstore.swagger.io/v2/base.json',
546+
requestInterceptor: spy
547+
}).then((client) => {
548+
expect(spy.calls.length).toEqual(2)
549+
cb()
550+
}).catch(cb)
551+
})
552+
553+
it('should support response interceptor when fetching a spec and remote ref', function (cb) {
554+
const spy = createSpy().andCall((a) => {
555+
return a
556+
})
557+
558+
new Swagger({
559+
url: 'http://petstore.swagger.io/v2/base.json',
560+
responseInterceptor: spy
561+
}).then((client) => {
562+
expect(spy.calls.length).toEqual(2)
563+
cb()
564+
}).catch(cb)
565+
})
566+
567+
it('should support request and response interceptor when fetching a spec and remote ref', function (cb) {
568+
const reqSpy = createSpy().andCall(a => a)
569+
const resSpy = createSpy().andCall(a => a)
570+
new Swagger({
571+
url: 'http://petstore.swagger.io/v2/base.json',
572+
responseInterceptor: reqSpy,
573+
requestInterceptor: resSpy
574+
}).then((client) => {
575+
expect(reqSpy.calls.length).toEqual(2)
576+
expect(resSpy.calls.length).toEqual(2)
577+
cb()
578+
}).catch(cb)
579+
})
531580
})
532581
})

test/oas3/execute/main.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,5 +484,30 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
484484

485485
expect(res).toEqual('https://petstore.com')
486486
})
487+
it('should fall back to contextUrls if no servers are provided', function () {
488+
const spec = {
489+
openapi: '3.0.0'
490+
}
491+
492+
const res = baseUrl({
493+
spec,
494+
server: 'http://some-invalid-server.com/',
495+
contextUrl: 'http://google.com/'
496+
})
497+
498+
expect(res).toEqual('http://google.com')
499+
})
500+
it('should return an empty string if no servers or contextUrl are provided', function () {
501+
const spec = {
502+
openapi: '3.0.0'
503+
}
504+
505+
const res = baseUrl({
506+
spec,
507+
server: 'http://some-invalid-server.com/'
508+
})
509+
510+
expect(res).toEqual('')
511+
})
487512
})
488513
})

0 commit comments

Comments
 (0)