Skip to content

Commit 9535e2c

Browse files
committed
whether a schema is just a string or an object, dereference it
1 parent 3b9278c commit 9535e2c

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

src/definitionGenerator.js

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -396,48 +396,41 @@ class DefinitionGenerator {
396396
}
397397
}
398398

399-
if (typeof schema !== 'string' && Object.keys(schema).length > 0) {
400-
const convertedSchema = SchemaConvertor.convert(schema)
399+
const deReferencedSchema = await $RefParser.dereference(schema, this.refParserOptions)
400+
.catch(err => {
401+
console.error(err)
402+
throw err
403+
})
401404

402-
let schemaName = name
403-
if (this.schemaIDs.includes(schemaName))
404-
schemaName = `${name}-${uuid()}`
405405

406-
this.schemaIDs.push(schemaName)
406+
const convertedSchema = SchemaConvertor.convert(deReferencedSchema)
407+
let schemaName = name
408+
if (this.schemaIDs.includes(schemaName))
409+
schemaName = `${name}-${uuid()}`
407410

408-
for (const key of Object.keys(convertedSchema.schemas)) {
409-
if (key === 'main' || key.split('-')[0] === 'main') {
410-
let ref = `#/components/schemas/`
411+
this.schemaIDs.push(schemaName)
411412

412-
if (this.openAPI?.components?.schemas?.[name]) {
413-
if (JSON.stringify(convertedSchema.schemas[key]) === JSON.stringify(this.openAPI.components.schemas[name])) {
414-
return `${ref}${name}`
415-
}
413+
for (const key of Object.keys(convertedSchema.schemas)) {
414+
if (key === 'main' || key.split('-')[0] === 'main') {
415+
let ref = `#/components/schemas/`
416+
417+
if (this.openAPI?.components?.schemas?.[name]) {
418+
if (JSON.stringify(convertedSchema.schemas[key]) === JSON.stringify(this.openAPI.components.schemas[name])) {
419+
return `${ref}${name}`
416420
}
421+
}
417422

418-
addToComponents(convertedSchema.schemas[key], schemaName)
419-
return `${ref}${schemaName}`
420-
} else {
421-
if (this.openAPI?.components?.schemas?.[key]) {
422-
if (JSON.stringify(convertedSchema.schemas[key]) !== JSON.stringify(this.openAPI.components.schemas[key])) {
423-
addToComponents(convertedSchema.schemas[key], key)
424-
}
425-
} else {
423+
addToComponents(convertedSchema.schemas[key], schemaName)
424+
return `${ref}${schemaName}`
425+
} else {
426+
if (this.openAPI?.components?.schemas?.[key]) {
427+
if (JSON.stringify(convertedSchema.schemas[key]) !== JSON.stringify(this.openAPI.components.schemas[key])) {
426428
addToComponents(convertedSchema.schemas[key], key)
427429
}
430+
} else {
431+
addToComponents(convertedSchema.schemas[key], key)
428432
}
429433
}
430-
} else {
431-
const combinedSchema = await $RefParser.dereference(schema, this.refParserOptions)
432-
.catch(err => {
433-
console.error(err)
434-
throw err
435-
})
436-
437-
return await this.schemaCreator(combinedSchema, name)
438-
.catch(err => {
439-
throw err
440-
})
441434
}
442435
}
443436

0 commit comments

Comments
 (0)