Skip to content

Commit e4d5097

Browse files
committed
Add warning for JSON Pointers that lack a leading /
1 parent 697166f commit e4d5097

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/specmap/lib/refs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ const plugin = {
5555
const refPath = splitString[0]
5656
const pointer = splitString[1] || ''
5757

58+
if (pointer.length > 0 && pointer[0] !== '/') {
59+
console.warn(`WARNING: $ref '${ref}' is malformed, a leading '/' is expected after '#'`)
60+
}
61+
5862
let basePath
5963
try {
6064
basePath = (baseDoc || refPath) ? absoluteify(refPath, baseDoc) : null

test/specmap/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import expect from 'expect'
1+
import expect, {createSpy} from 'expect'
22
import clone from 'clone'
33
import xmock from 'xmock'
44
import traverse from 'traverse'
@@ -419,6 +419,23 @@ describe('specmap', function () {
419419
})
420420
})
421421

422+
it('should warn about malformed JSON Pointers', function () {
423+
// for https://github.com/swagger-api/swagger-editor/issues/1560
424+
const oriWarn = console.warn
425+
console.warn = createSpy().andCallThrough()
426+
return mapSpec({
427+
spec: {
428+
nested: {one: 1},
429+
another: {$ref: '#nested'}
430+
},
431+
plugins: [plugins.refs]
432+
})
433+
.then((res) => {
434+
console.warn = oriWarn
435+
expect(console.warn).toHaveBeenCalledWith('WARNING: $ref \'http://example.com/doc-a#two\' is malformed, a leading \'/\' is expected after \'#\'')
436+
})
437+
})
438+
422439
it('should resolve internal $refs in arrays', function () {
423440
return mapSpec({
424441
spec: {

0 commit comments

Comments
 (0)