From 5192e70fe9d0c899eff0fc007bc5bf4436f90719 Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Sat, 3 Feb 2018 12:24:01 -0500 Subject: [PATCH 1/6] Add isFreelyNamed Trying to fix https://github.com/swagger-api/swagger-ui/issues/4168 --- src/specmap/helpers.js | 8 ++++++++ src/specmap/lib/all-of.js | 8 +++----- src/specmap/lib/refs.js | 10 ++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/specmap/helpers.js b/src/specmap/helpers.js index 57c0b6bcf..d57d0bd38 100644 --- a/src/specmap/helpers.js +++ b/src/specmap/helpers.js @@ -11,3 +11,11 @@ export const freelyNamedKeyParents = [ 'components/parameters', 'components/securitySchemes', ] + +export function isFreelyNamed(parent) { + const parentStr = parent.join('/') + return ( + (parent[parent.length - 1] === 'properties') || + (freelyNamedKeyParents.indexOf(parentStr) > -1) + ) +} diff --git a/src/specmap/lib/all-of.js b/src/specmap/lib/all-of.js index 76d10576c..6e6d4c19d 100644 --- a/src/specmap/lib/all-of.js +++ b/src/specmap/lib/all-of.js @@ -1,4 +1,4 @@ -import {freelyNamedKeyParents} from '../helpers' +import {isFreelyNamed} from '../helpers' export default { key: 'allOf', @@ -10,10 +10,8 @@ export default { return } - const parent = fullPath.slice(0, -1) - const parentStr = parent.join('/') - - if (freelyNamedKeyParents.indexOf(parentStr) > -1) { + const parent = fullPath.slice(0, -1) + if (isFreelyNamed(parent)) { return } diff --git a/src/specmap/lib/refs.js b/src/specmap/lib/refs.js index 9ad5ceeb1..7747a98cd 100644 --- a/src/specmap/lib/refs.js +++ b/src/specmap/lib/refs.js @@ -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') @@ -41,14 +41,12 @@ const specmapRefs = new WeakMap() 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) { + const parent = fullPath.slice(0, -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, From e3f96e0c8790e91a91bfa79fa122c0770d3fcd5c Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Sat, 3 Feb 2018 12:35:36 -0500 Subject: [PATCH 2/6] Add a simple unitTest --- src/specmap/lib/all-of.js | 2 +- src/specmap/lib/refs.js | 2 +- test/specmap/all-of.js | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/specmap/lib/all-of.js b/src/specmap/lib/all-of.js index 6e6d4c19d..6953d960c 100644 --- a/src/specmap/lib/all-of.js +++ b/src/specmap/lib/all-of.js @@ -10,7 +10,7 @@ export default { return } - const parent = fullPath.slice(0, -1) + const parent = fullPath.slice(0, -1) if (isFreelyNamed(parent)) { return } diff --git a/src/specmap/lib/refs.js b/src/specmap/lib/refs.js index 7747a98cd..f8a22b811 100644 --- a/src/specmap/lib/refs.js +++ b/src/specmap/lib/refs.js @@ -41,7 +41,7 @@ const specmapRefs = new WeakMap() const plugin = { key: '$ref', plugin: (ref, key, fullPath, specmap) => { - const parent = fullPath.slice(0, -1) + const parent = fullPath.slice(0, -1) if (isFreelyNamed(parent)) { return } diff --git a/test/specmap/all-of.js b/test/specmap/all-of.js index 60de3e514..4deeeeab7 100644 --- a/test/specmap/all-of.js +++ b/test/specmap/all-of.js @@ -212,6 +212,11 @@ describe('allOf', function () { a: 123 } }, + properties: { + allOf: { + a: 123 + } + }, } return mapSpec({ From c1cf1f240470f35d2f0e0702786888ec0db449a9 Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Sat, 3 Feb 2018 13:43:17 -0500 Subject: [PATCH 3/6] refactor the isFreelyNamed freelyNamedKeyParents is private no need to export --- src/specmap/helpers.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/specmap/helpers.js b/src/specmap/helpers.js index d57d0bd38..57ccb8ef9 100644 --- a/src/specmap/helpers.js +++ b/src/specmap/helpers.js @@ -1,4 +1,7 @@ -export const freelyNamedKeyParents = [ +const freelyNamedKeyParents = [ + 'properties', +] +const freelyNamedStrParents = [ // Swagger 2.0 'definitions', 'parameters', @@ -13,9 +16,10 @@ export const freelyNamedKeyParents = [ ] export function isFreelyNamed(parent) { - const parentStr = parent.join('/') + const parentKey = parent[parent.length - 1] + const parentStr = parent.join('/') return ( - (parent[parent.length - 1] === 'properties') || - (freelyNamedKeyParents.indexOf(parentStr) > -1) + (freelyNamedKeyParents.indexOf(parentKey) > -1) || + (freelyNamedStrParents.indexOf(parentStr) > -1) ) } From 652467d11ca21da3dd0add07311565bd7877a9a3 Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Sat, 3 Feb 2018 13:46:10 -0500 Subject: [PATCH 4/6] Trim trailing spaces --- src/specmap/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/specmap/helpers.js b/src/specmap/helpers.js index 57ccb8ef9..2f4c030f5 100644 --- a/src/specmap/helpers.js +++ b/src/specmap/helpers.js @@ -17,7 +17,7 @@ const freelyNamedStrParents = [ export function isFreelyNamed(parent) { const parentKey = parent[parent.length - 1] - const parentStr = parent.join('/') + const parentStr = parent.join('/') return ( (freelyNamedKeyParents.indexOf(parentKey) > -1) || (freelyNamedStrParents.indexOf(parentStr) > -1) From 302f0a002405a8c50d07dafc7479a251abba99e5 Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Wed, 7 Feb 2018 21:02:04 -0500 Subject: [PATCH 5/6] Rename freelyNamedStrParents to freelyNamedPaths --- src/specmap/helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/specmap/helpers.js b/src/specmap/helpers.js index 2f4c030f5..4a7b121e2 100644 --- a/src/specmap/helpers.js +++ b/src/specmap/helpers.js @@ -1,7 +1,7 @@ const freelyNamedKeyParents = [ 'properties', ] -const freelyNamedStrParents = [ +const freelyNamedPaths = [ // Swagger 2.0 'definitions', 'parameters', @@ -20,6 +20,6 @@ export function isFreelyNamed(parent) { const parentStr = parent.join('/') return ( (freelyNamedKeyParents.indexOf(parentKey) > -1) || - (freelyNamedStrParents.indexOf(parentStr) > -1) + (freelyNamedPaths.indexOf(parentStr) > -1) ) } From 0b210c665368dbf1ff10b5e5d760f4c141eeac42 Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Wed, 7 Feb 2018 21:09:58 -0500 Subject: [PATCH 6/6] Add test for property named $ref --- test/specmap/refs.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/specmap/refs.js b/test/specmap/refs.js index 98b21ec92..b7cc8fece 100644 --- a/test/specmap/refs.js +++ b/test/specmap/refs.js @@ -378,6 +378,9 @@ describe('refs', function () { }, securityDefinitions: { $ref: '#/a' + }, + properties: { + $ref: '#/a' } }, plugins: [refs], @@ -395,6 +398,9 @@ describe('refs', function () { }, securityDefinitions: { $ref: '#/a' + }, + properties: { + $ref: '#/a' } }) })