Skip to content

Commit 3f5ad91

Browse files
committed
WIP
1 parent c68c9a4 commit 3f5ad91

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-0
lines changed

examples/oas3.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TODO: OAS3: provide examples
2+
// requestBody, etc

src/execute.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export const self = {
2424

2525
// These functions will update the request.
2626
// They'll be given {req, value, paramter, spec, operation}.
27+
28+
// TODO: OAS3: add builder for requestBody
29+
// QUESTION: OAS3: how do we decide which media type to use from a requestBody?
2730
export const PARAMETER_BUILDERS = {
2831
body: bodyBuilder,
2932
header: headerBuilder,
@@ -120,6 +123,9 @@ export function buildRequest({
120123
const builder = parameterBuilders[parameter.in]
121124
let value
122125

126+
// REVIEW: OAS3: have any key names or parameter shapes changed?
127+
// Any new features that need to be plugged in here?
128+
123129
if (parameter.in === 'body' && parameter.schema && parameter.schema.properties) {
124130
value = parameters
125131
}
@@ -140,6 +146,7 @@ export function buildRequest({
140146
})
141147

142148
// Add securities, which are applicable
149+
// REVIEW: OAS3: what changed in securities?
143150
req = applySecurities({request: req, securities, operation, spec})
144151

145152
if (req.body || req.form) {
@@ -156,6 +163,7 @@ export function buildRequest({
156163
req.headers['content-type'] = 'multipart/form-data'
157164
}
158165
else if (operation.parameters.filter(p => p.in === 'formData').length) {
166+
// TODO: OAS3: disable this
159167
req.headers['content-type'] = 'application/x-www-form-urlencoded'
160168
}
161169
}
@@ -169,11 +177,13 @@ export function buildRequest({
169177

170178
// Add the body to the request
171179
export function bodyBuilder({req, value}) {
180+
// REVIEW: OAS3: wtf does this do
172181
req.body = value
173182
}
174183

175184
// Add a form data object.
176185
export function formDataBuilder({req, value, parameter}) {
186+
// REVIEW: OAS3: check for any parameter changes that affect the builder
177187
req.form = req.form || {}
178188
if (value || parameter.allowEmptyValue) {
179189
req.form[parameter.name] = {
@@ -186,6 +196,7 @@ export function formDataBuilder({req, value, parameter}) {
186196

187197
// Add a header to the request
188198
export function headerBuilder({req, parameter, value}) {
199+
// REVIEW: OAS3: check for any parameter changes that affect the builder
189200
req.headers = req.headers || {}
190201
if (typeof value !== 'undefined') {
191202
req.headers[parameter.name] = value
@@ -194,11 +205,13 @@ export function headerBuilder({req, parameter, value}) {
194205

195206
// Replace path paramters, with values ( ie: the URL )
196207
export function pathBuilder({req, value, parameter}) {
208+
// REVIEW: OAS3: check for any parameter changes that affect the builder
197209
req.url = req.url.replace(`{${parameter.name}}`, encodeURIComponent(value))
198210
}
199211

200212
// Add a query to the `query` object, which will later be stringified into the URL's search
201213
export function queryBuilder({req, value, parameter}) {
214+
// REVIEW: OAS3: check for any parameter changes that affect the builder
202215
req.query = req.query || {}
203216

204217
if (value === false && parameter.type === 'boolean') {
@@ -226,6 +239,9 @@ const stripNonAlpha = str => (str ? str.replace(/\W/g, '') : null)
226239

227240
// Compose the baseUrl ( scheme + host + basePath )
228241
export function baseUrl({spec, scheme, contextUrl = ''}) {
242+
// TODO: OAS3: support `servers` instead of host+basePath
243+
// QUESTION: OAS3: how are we handling `servers`?
244+
// QUESTION: OAS3: are we still doing assumed URL components the same way?
229245
const parsedContextUrl = url.parse(contextUrl)
230246
const firstSchemeInSpec = Array.isArray(spec.schemes) ? spec.schemes[0] : null
231247

src/helpers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ export function eachOperation(spec, cb, find) {
8686
}
8787
}
8888

89+
// REVIEW: OAS3: identify normalization steps that need changes
90+
// ...maybe create `normalizeOAS3`?
91+
8992
export function normalizeSwagger(parsedSpec) {
9093
const {spec} = parsedSpec
9194
const {paths} = spec

src/http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ function isFile(obj) {
135135
}
136136

137137
function formatValue({value, collectionFormat, allowEmptyValue}, skipEncoding) {
138+
// REVIEW: OAS3: usage of this fn for compatibility w/ new value formats
138139
const SEPARATORS = {
139140
csv: ',',
140141
ssv: '%20',

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Swagger.prototype = {
8181
Swagger.prototype.applyDefaults = function () {
8282
const spec = this.spec
8383
const specUrl = this.url
84+
// TODO: OAS3: support servers here
8485
if (specUrl && specUrl.startsWith('http')) {
8586
const parsed = Url.parse(specUrl)
8687
if (!spec.host) {

test/execute.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,64 @@ describe('execute', () => {
825825
})
826826
})
827827

828+
describe.only("buildRequest for OpenAPI 3.0", function () {
829+
it('should build a request for the given operationId', function () {
830+
// Given
831+
const spec = {
832+
openapi: "3.0.0",
833+
paths: {
834+
'/one': {
835+
get: {
836+
operationId: 'getMe'
837+
}
838+
}
839+
}
840+
}
841+
842+
// when
843+
const req = buildRequest({spec, operationId: 'getMe'})
844+
845+
expect(req).toEqual({
846+
method: 'GET',
847+
url: '/one',
848+
credentials: 'same-origin',
849+
headers: {},
850+
})
851+
})
852+
853+
it('should build a request for the given operationId with a server provided', function () {
854+
// Given
855+
const spec = {
856+
openapi: '3.0.0',
857+
servers: [
858+
{
859+
url: 'http://petstore.swagger.io/v2',
860+
name: 'Petstore'
861+
}
862+
],
863+
paths: {
864+
'/one': {
865+
get: {
866+
operationId: 'getMe'
867+
}
868+
}
869+
}
870+
}
871+
872+
// when
873+
const req = buildRequest({spec, operationId: 'getMe'})
874+
875+
expect(req).toEqual({
876+
method: 'GET',
877+
url: 'http://petstore.swagger.io/v2/one',
878+
credentials: 'same-origin',
879+
headers: {},
880+
})
881+
})
882+
883+
884+
})
885+
828886
// Note: this is to handle requestContentType and responseContentType
829887
// although more might end up using it.
830888
it('should pass extras props to buildRequest', () => {

test/oas3/execute.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import expect, {createSpy, spyOn} from 'expect'
2+
import xmock from 'xmock'
3+
import {execute, buildRequest, baseUrl, applySecurities, self as stubs} from '../src/execute'
4+
5+
// Supported shape... { spec, operationId, parameters, securities, fetch }
6+
// One can use operationId or pathItem + method
7+
8+
describe.only("buildRequest - OpenAPI Specification 3.0", function () {
9+
it('should build a request for the given operationId', function () {
10+
// Given
11+
const spec = {
12+
openapi: "3.0.0",
13+
paths: {
14+
'/one': {
15+
get: {
16+
operationId: 'getMe'
17+
}
18+
}
19+
}
20+
}
21+
22+
// when
23+
const req = buildRequest({spec, operationId: 'getMe'})
24+
25+
expect(req).toEqual({
26+
method: 'GET',
27+
url: '/one',
28+
credentials: 'same-origin',
29+
headers: {},
30+
})
31+
})
32+
33+
it('should build a request for the given operationId with a server provided', function () {
34+
// Given
35+
const spec = {
36+
openapi: '3.0.0',
37+
servers: [
38+
{
39+
url: 'http://petstore.swagger.io/v2',
40+
name: 'Petstore'
41+
}
42+
],
43+
paths: {
44+
'/one': {
45+
get: {
46+
operationId: 'getMe'
47+
}
48+
}
49+
}
50+
}
51+
52+
// when
53+
const req = buildRequest({spec, operationId: 'getMe'})
54+
55+
expect(req).toEqual({
56+
method: 'GET',
57+
url: 'http://petstore.swagger.io/v2/one',
58+
credentials: 'same-origin',
59+
headers: {},
60+
})
61+
})
62+
63+
64+
})

0 commit comments

Comments
 (0)