Skip to content

Commit

Permalink
Ignore Swagger/OpenAPI freely-named key paths in $ref/allOf plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shockey committed Dec 28, 2017
1 parent 0201763 commit 1f52a95
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -28,6 +28,7 @@
"no-underscore-dangle": 0,
"no-unused-expressions": [2, {"allowShortCircuit": true}],
"object-curly-spacing": [2, "never"],
"import/prefer-default-export": 1,
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["!test/**/*.js"]}]
}
}
13 changes: 13 additions & 0 deletions src/specmap/helpers.js
@@ -0,0 +1,13 @@
export const freelyNamedKeyParents = [
// Swagger 2.0
'definitions',
'parameters',
'responses',
'securityDefinitions',

// OpenAPI 3.0
'components/schemas',
'components/responses',
'components/parameters',
'components/securitySchemes',
]
11 changes: 10 additions & 1 deletion src/specmap/lib/all-of.js
@@ -1,3 +1,5 @@
import {freelyNamedKeyParents} from '../helpers'

export default {
key: 'allOf',
plugin: (val, key, fullPath, specmap, patch) => {
Expand All @@ -8,13 +10,20 @@ export default {
return
}

const parent = fullPath.slice(0, -1)
const parentStr = parent.join('/')

if (freelyNamedKeyParents.indexOf(parentStr) > -1) {
return
}

if (!Array.isArray(val)) {
const err = new TypeError('allOf must be an array')
err.fullPath = fullPath // This is an array
return err
}

const parent = fullPath.slice(0, -1)

let alreadyAddError = false

// Find the original definition from the `patch.value` object
Expand Down
6 changes: 6 additions & 0 deletions src/specmap/lib/refs.js
Expand Up @@ -2,6 +2,7 @@ import {fetch} from 'cross-fetch'
import url from 'url'
import lib from '../lib'
import createError from '../lib/create-error'
import {freelyNamedKeyParents} from '../helpers'

const ABSOLUTE_URL_REGEXP = new RegExp('^([a-z]+://|//)', 'i')

Expand Down Expand Up @@ -41,8 +42,13 @@ const plugin = {
key: '$ref',
plugin: (ref, key, fullPath, specmap) => {
const parent = fullPath.slice(0, -1)
const parentStr = parent.join('/')
const baseDoc = specmap.getContext(fullPath).baseDoc

if (freelyNamedKeyParents.indexOf(parentStr) > -1) {
return
}

if (typeof ref !== 'string') {
return new JSONRefError('$ref: must be a string (JSON-Ref)', {
$ref: ref,
Expand Down
33 changes: 33 additions & 0 deletions test/specmap/all-of.js
Expand Up @@ -192,6 +192,39 @@ describe('allOf', function () {
})
})

it('should ignore "allOf" in freely named Swagger key positions', function () {
const spec = {
parameters: {
allOf: {
a: 123
}
},
responses: {
allOf: {
a: 123
}
},
definitions: {
allOf: {
a: 123
}
},
securityDefinitions: {
allOf: {
a: 123
}
},
}

return mapSpec({
spec,
plugins: [plugins.allOf]
}).then((res) => {
expect(res.errors).toEqual([])
expect(res.spec).toEqual(spec)
})
})

it('should throw error if allOf has a non-object item', function () {
return mapSpec({
spec: {
Expand Down
37 changes: 37 additions & 0 deletions test/specmap/refs.js
Expand Up @@ -363,6 +363,43 @@ describe('refs', function () {
})
})

it('should ignore $refs in freely named Swagger positions', function () {
return mapSpec({
spec: {
a: 1234,
parameters: {
$ref: '#/a'
},
responses: {
$ref: '#/a'
},
definitions: {
$ref: '#/a'
},
securityDefinitions: {
$ref: '#/a'
}
},
plugins: [refs],
}).then((res) => {
expect(res.spec).toEqual({
a: 1234,
parameters: {
$ref: '#/a'
},
responses: {
$ref: '#/a'
},
definitions: {
$ref: '#/a'
},
securityDefinitions: {
$ref: '#/a'
}
})
})
})

it('should include fullPath in invalid $ref type', function () {
return mapSpec({
spec: {one: {$ref: 1}},
Expand Down

0 comments on commit 1f52a95

Please sign in to comment.