Skip to content

Commit c22967e

Browse files
committed
move to own functions
1 parent fa5609e commit c22967e

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

src/definitionGenerator.js

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -455,42 +455,13 @@ class DefinitionGenerator {
455455
}
456456

457457
async dereferenceSchema(schema) {
458-
return await $RefParser.dereference(schema, this.refParserOptions)
458+
let deReferencedSchema = await $RefParser.dereference(schema, this.refParserOptions)
459459
.catch(err => {
460460
console.error(err)
461461
throw err
462462
})
463-
}
464-
465-
async schemaCreator(schema, name) {
466-
const addToComponents = (schema, name) => {
467-
const schemaObj = {
468-
[name]: schema
469-
}
470-
471-
if (this.openAPI?.components) {
472-
if (this.openAPI.components?.schemas) {
473-
Object.assign(this.openAPI.components.schemas, schemaObj)
474-
} else {
475-
Object.assign(this.openAPI.components, {schemas: schemaObj})
476-
}
477-
} else {
478-
const components = {
479-
components: {
480-
schemas: schemaObj
481-
}
482-
}
483-
484-
Object.assign(this.openAPI, components)
485-
}
486-
}
487463

488-
let deReferencedSchema = await this.dereferenceSchema(schema)
489-
.catch((err) => {
490-
throw err
491-
})
492-
493-
// deal with schemas that have been de-referenced poorly
464+
// deal with schemas that have been de-referenced poorly: naive
494465
if (deReferencedSchema.$ref === '#') {
495466
const oldRef = schema.$ref
496467
const path = oldRef.split('/')
@@ -505,7 +476,17 @@ class DefinitionGenerator {
505476
})
506477
}
507478

479+
return deReferencedSchema
480+
}
481+
482+
async schemaCreator(schema, name) {
483+
let deReferencedSchema = await this.dereferenceSchema(schema)
484+
.catch((err) => {
485+
throw err
486+
})
487+
508488
const convertedSchema = SchemaConvertor.convert(deReferencedSchema, name)
489+
509490
let schemaName = name
510491
if (this.schemaIDs.includes(schemaName))
511492
schemaName = `${name}-${uuid()}`
@@ -516,26 +497,34 @@ class DefinitionGenerator {
516497
if (key === name || key.split('-')[0] === name) {
517498
let ref = `#/components/schemas/`
518499

519-
if (this.openAPI?.components?.schemas?.[name]) {
520-
if (JSON.stringify(convertedSchema.schemas[key]) === JSON.stringify(this.openAPI.components.schemas[name])) {
500+
if (this.existsInComponents(name)) {
501+
if (this.isTheSameSchema(convertedSchema.schemas[key], name)) {
521502
return `${ref}${name}`
522503
}
523504
}
524505

525-
addToComponents(convertedSchema.schemas[key], schemaName)
506+
this.addToComponents('schemas', convertedSchema.schemas[key], schemaName)
526507
return `${ref}${schemaName}`
527508
} else {
528-
if (this.openAPI?.components?.schemas?.[key]) {
529-
if (JSON.stringify(convertedSchema.schemas[key]) !== JSON.stringify(this.openAPI.components.schemas[key])) {
530-
addToComponents(convertedSchema.schemas[key], key)
509+
if (this.existsInComponents(key)) {
510+
if (this.isTheSameSchema(convertedSchema.schemas[key], key)) {
511+
this.addToComponents('schemas', convertedSchema.schemas[key], key)
531512
}
532513
} else {
533-
addToComponents(convertedSchema.schemas[key], key)
514+
this.addToComponents('schemas', convertedSchema.schemas[key], key)
534515
}
535516
}
536517
}
537518
}
538519

520+
existsInComponents(name) {
521+
return Boolean(this.openAPI?.components?.schemas?.[name])
522+
}
523+
524+
isTheSameSchema(schema, otherSchemaName) {
525+
return (JSON.stringify(schema) === JSON.stringify(this.openAPI.components.schemas[otherSchemaName]))
526+
}
527+
539528
addToComponents(type, schema, name) {
540529
const schemaObj = {
541530
[name]: schema

0 commit comments

Comments
 (0)