Skip to content

Commit

Permalink
fix: non-standard resolution behavior (via #1465)
Browse files Browse the repository at this point in the history
...in RequestBody/Response Media Type Example values
  • Loading branch information
shockey authored Aug 9, 2019
1 parent 8b8e026 commit 2a3ab79
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/specmap/lib/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ const JSONRefError = createError('JSONRefError', function (message, extra, oriEr
const docCache = {}
const specmapRefs = new WeakMap()

const skipResolutionTestFns = [
path => (
// OpenAPI 3.0 Response Media Type Example
// ["paths", *, *, "responses", *, "content", *, "example"]
path[0] === 'paths'
&& path[3] === 'responses'
&& path[5] === 'content'
&& path[7] === 'example'
),
path => (
// OpenAPI 3.0 Request Body Media Type Example
// ["paths", *, *, "responses", *, "content", *, "example"]
path[0] === 'paths'
&& path[3] === 'requestBody'
&& path[4] === 'content'
&& path[6] === 'example'
)
]

const shouldSkipResolution = path => skipResolutionTestFns.some(fn => fn(path))

// =========================
// Core
Expand Down Expand Up @@ -45,7 +65,8 @@ const plugin = {
plugin: (ref, key, fullPath, specmap) => {
const specmapInstance = specmap.getInstance()
const parent = fullPath.slice(0, -1)
if (isFreelyNamed(parent)) {

if (isFreelyNamed(parent) || shouldSkipResolution(parent)) {
return
}

Expand Down
110 changes: 110 additions & 0 deletions test/bugs/ui-4924.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// https://github.com/swagger-api/swagger-ui/issues/4466
// https://github.com/swagger-api/swagger-ui/issues/4467

import resolveSubtree from '../../src/subtree-resolver'

const spec = {
openapi: '3.0.0',
paths: {
'/order': {
post: {
requestBody: {
content: {
'application/json': {
example: {
user: {
$ref: '#/components/examples/User/value'
},
quantity: 1
}
}
}
},
responses: {
200: {
description: 'OK',
content: {
'application/json': {
example: {
user: {
$ref: '#/components/examples/User/value'
},
quantity: 1
}
}
}
}
}
}
}
},
components: {
examples: {
User: {
value: {
id: 1,
name: 'Sasha'
}
}
}
}
}


test(
'should not resolve $ref pointers within OpenAPI RequestBody/Response media type examples',
async () => {
const res = await resolveSubtree(spec, [])

expect(res).toEqual({
errors: [],
spec: {
$$normalized: true,
openapi: '3.0.0',
paths: {
'/order': {
post: {
requestBody: {
content: {
'application/json': {
example: {
user: {
$ref: '#/components/examples/User/value'
},
quantity: 1
}
}
}
},
responses: {
200: {
description: 'OK',
content: {
'application/json': {
example: {
user: {
$ref: '#/components/examples/User/value'
},
quantity: 1
}
}
}
}
}
}
}
},
components: {
examples: {
User: {
value: {
id: 1,
name: 'Sasha'
}
}
}
}
}
})
}
)

0 comments on commit 2a3ab79

Please sign in to comment.