Skip to content

Commit c1a7b72

Browse files
authored
avoid crashing of resolveLocalRef (#662)
1 parent dc1c0a9 commit c1a7b72

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

lib/util/common.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,13 @@ function resolveLocalRef (jsonSchema, externalSchemas) {
187187
}
188188

189189
// $ref is in the format: #/definitions/<resolved definition>/<optional fragment>
190-
const localRef = jsonSchema.$ref.split('/')[2]
191-
if (externalSchemas[localRef]) return resolveLocalRef(externalSchemas[localRef], externalSchemas)
192-
// $ref is in the format: #/components/schemas/<resolved definition>
193-
return resolveLocalRef(externalSchemas[jsonSchema.$ref.split('/')[3]], externalSchemas)
190+
if (jsonSchema.$ref) {
191+
const localRef = jsonSchema.$ref.split('/')[2]
192+
if (externalSchemas[localRef]) return resolveLocalRef(externalSchemas[localRef], externalSchemas)
193+
// $ref is in the format: #/components/schemas/<resolved definition>
194+
return resolveLocalRef(externalSchemas[jsonSchema.$ref.split('/')[3]], externalSchemas)
195+
}
196+
return jsonSchema
194197
}
195198

196199
function readPackageJson () {

test/spec/openapi/refs.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,47 @@ test('support $ref schema in allOf in headers', async (t) => {
238238

239239
t.equal(responseAfterSwagger.statusCode, 200)
240240
})
241+
242+
test('uses examples if has property required in body', async (t) => {
243+
t.plan(3)
244+
const fastify = Fastify()
245+
246+
await fastify.register(fastifySwagger, openapiOption)
247+
248+
fastify.get('/', {
249+
schema: {
250+
query: {
251+
type: 'object',
252+
oneOf: [
253+
{
254+
properties: {
255+
bar: { type: 'number' }
256+
}
257+
},
258+
{
259+
properties: {
260+
foo: { type: 'string' }
261+
}
262+
}
263+
]
264+
},
265+
response: {
266+
200: {
267+
type: 'object',
268+
properties: {
269+
result: { type: 'string' }
270+
}
271+
}
272+
}
273+
}
274+
}, (req, reply) => ({ result: 'OK' }))
275+
276+
await fastify.ready()
277+
278+
const openapiObject = fastify.swagger()
279+
const schema = openapiObject.paths['/'].get
280+
281+
t.ok(schema)
282+
t.ok(schema.parameters)
283+
t.same(schema.parameters[0].in, 'query')
284+
})

0 commit comments

Comments
 (0)