Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/specmap/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const freelyNamedKeyParents = [
const freelyNamedKeyParents = [
'properties',
]
const freelyNamedPaths = [
// Swagger 2.0
'definitions',
'parameters',
Expand All @@ -11,3 +14,12 @@ export const freelyNamedKeyParents = [
'components/parameters',
'components/securitySchemes',
]

export function isFreelyNamed(parent) {
const parentKey = parent[parent.length - 1]
const parentStr = parent.join('/')
return (
(freelyNamedKeyParents.indexOf(parentKey) > -1) ||
(freelyNamedPaths.indexOf(parentStr) > -1)
)
}
6 changes: 2 additions & 4 deletions src/specmap/lib/all-of.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {freelyNamedKeyParents} from '../helpers'
import {isFreelyNamed} from '../helpers'

export default {
key: 'allOf',
Expand All @@ -11,9 +11,7 @@ export default {
}

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

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

Expand Down
8 changes: 3 additions & 5 deletions src/specmap/lib/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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'
import {isFreelyNamed} from '../helpers'

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

Expand Down Expand Up @@ -42,13 +42,11 @@ 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) {
if (isFreelyNamed(parent)) {
return
}

const baseDoc = specmap.getContext(fullPath).baseDoc
if (typeof ref !== 'string') {
return new JSONRefError('$ref: must be a string (JSON-Ref)', {
$ref: ref,
Expand Down
5 changes: 5 additions & 0 deletions test/specmap/all-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ describe('allOf', function () {
a: 123
}
},
properties: {
allOf: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to see a property named $ref tested as well (new test or added to existing one, i'm not picky!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

a: 123
}
},
}

return mapSpec({
Expand Down
6 changes: 6 additions & 0 deletions test/specmap/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ describe('refs', function () {
},
securityDefinitions: {
$ref: '#/a'
},
properties: {
$ref: '#/a'
}
},
plugins: [refs],
Expand All @@ -395,6 +398,9 @@ describe('refs', function () {
},
securityDefinitions: {
$ref: '#/a'
},
properties: {
$ref: '#/a'
}
})
})
Expand Down