Skip to content

Commit

Permalink
avoid crashing of resolveLocalRef (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Sep 5, 2022
1 parent dc1c0a9 commit c1a7b72
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ function resolveLocalRef (jsonSchema, externalSchemas) {
}

// $ref is in the format: #/definitions/<resolved definition>/<optional fragment>
const localRef = jsonSchema.$ref.split('/')[2]
if (externalSchemas[localRef]) return resolveLocalRef(externalSchemas[localRef], externalSchemas)
// $ref is in the format: #/components/schemas/<resolved definition>
return resolveLocalRef(externalSchemas[jsonSchema.$ref.split('/')[3]], externalSchemas)
if (jsonSchema.$ref) {
const localRef = jsonSchema.$ref.split('/')[2]
if (externalSchemas[localRef]) return resolveLocalRef(externalSchemas[localRef], externalSchemas)
// $ref is in the format: #/components/schemas/<resolved definition>
return resolveLocalRef(externalSchemas[jsonSchema.$ref.split('/')[3]], externalSchemas)
}
return jsonSchema
}

function readPackageJson () {
Expand Down
44 changes: 44 additions & 0 deletions test/spec/openapi/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,47 @@ test('support $ref schema in allOf in headers', async (t) => {

t.equal(responseAfterSwagger.statusCode, 200)
})

test('uses examples if has property required in body', async (t) => {
t.plan(3)
const fastify = Fastify()

await fastify.register(fastifySwagger, openapiOption)

fastify.get('/', {
schema: {
query: {
type: 'object',
oneOf: [
{
properties: {
bar: { type: 'number' }
}
},
{
properties: {
foo: { type: 'string' }
}
}
]
},
response: {
200: {
type: 'object',
properties: {
result: { type: 'string' }
}
}
}
}
}, (req, reply) => ({ result: 'OK' }))

await fastify.ready()

const openapiObject = fastify.swagger()
const schema = openapiObject.paths['/'].get

t.ok(schema)
t.ok(schema.parameters)
t.same(schema.parameters[0].in, 'query')
})

0 comments on commit c1a7b72

Please sign in to comment.