Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-standard resolution behavior #1465

Merged
merged 1 commit into from
Aug 9, 2019
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
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'
}
}
}
}
}
})
}
)