@@ -5,7 +5,8 @@ const path = require('path')
55const { v4 : uuid } = require ( 'uuid' )
66const validator = require ( 'oas-validator' ) ;
77const SchemaConvertor = require ( 'json-schema-for-openapi' )
8- const $RefParser = require ( "@apidevtools/json-schema-ref-parser" ) ;
8+ const $RefParser = require ( "@apidevtools/json-schema-ref-parser" )
9+ const isEqual = require ( 'lodash.isequal' )
910
1011class DefinitionGenerator {
1112 constructor ( serverless , options = { } ) {
@@ -29,6 +30,11 @@ class DefinitionGenerator {
2930 this . operationIds = [ ]
3031 this . schemaIDs = [ ]
3132
33+ this . componentTypes = {
34+ schemas : 'schemas' ,
35+ securitySchemes : 'securitySchemes'
36+ }
37+
3238 try {
3339 this . refParserOptions = require ( path . resolve ( 'options' , 'ref-parser.js' ) )
3440 } catch ( err ) {
@@ -480,49 +486,40 @@ class DefinitionGenerator {
480486 }
481487
482488 async schemaCreator ( schema , name ) {
483- let deReferencedSchema = await this . dereferenceSchema ( schema )
484- . catch ( ( err ) => {
489+ let schemaName = name
490+ let finalName = schemaName
491+ const dereferencedSchema = await this . dereferenceSchema ( schema )
492+ . catch ( err => {
493+ console . error ( err )
485494 throw err
486495 } )
487496
488- const convertedSchema = SchemaConvertor . convert ( deReferencedSchema , name )
489-
490- let schemaName = name
491- if ( this . schemaIDs . includes ( schemaName ) )
492- schemaName = `${ name } -${ uuid ( ) } `
493-
494- this . schemaIDs . push ( schemaName )
495-
496- for ( const key of Object . keys ( convertedSchema . schemas ) ) {
497- if ( key === name || key . split ( '-' ) [ 0 ] === name ) {
498- let ref = `#/components/schemas/`
499-
500- if ( this . existsInComponents ( name ) ) {
501- if ( this . isTheSameSchema ( convertedSchema . schemas [ key ] , name ) ) {
502- return `${ ref } ${ name } `
503- }
497+ const convertedSchemas = SchemaConvertor . convert ( dereferencedSchema , schemaName )
498+
499+ for ( const convertedSchemaName of Object . keys ( convertedSchemas . schemas ) ) {
500+ const convertedSchema = convertedSchemas . schemas [ convertedSchemaName ]
501+ if ( this . existsInComponents ( convertedSchemaName ) ) {
502+ if ( this . isTheSameSchema ( convertedSchema , convertedSchemaName ) === false ) {
503+ if ( convertedSchemaName === schemaName ) {
504+ finalName = `${ schemaName } -${ uuid ( ) } `
505+ this . addToComponents ( this . componentTypes . schemas , convertedSchema , finalName )
506+ } else
507+ this . addToComponents ( this . componentTypes . schemas , convertedSchema , convertedSchemaName )
504508 }
505-
506- this . addToComponents ( 'schemas' , convertedSchema . schemas [ key ] , schemaName )
507- return `${ ref } ${ schemaName } `
508509 } else {
509- if ( this . existsInComponents ( key ) ) {
510- if ( this . isTheSameSchema ( convertedSchema . schemas [ key ] , key ) ) {
511- this . addToComponents ( 'schemas' , convertedSchema . schemas [ key ] , key )
512- }
513- } else {
514- this . addToComponents ( 'schemas' , convertedSchema . schemas [ key ] , key )
515- }
510+ this . addToComponents ( this . componentTypes . schemas , convertedSchema , convertedSchemaName )
516511 }
517512 }
513+
514+ return `#/components/schemas/${ finalName } `
518515 }
519516
520517 existsInComponents ( name ) {
521518 return Boolean ( this . openAPI ?. components ?. schemas ?. [ name ] )
522519 }
523520
524521 isTheSameSchema ( schema , otherSchemaName ) {
525- return ( JSON . stringify ( schema ) === JSON . stringify ( this . openAPI . components . schemas [ otherSchemaName ] ) )
522+ return isEqual ( schema , this . openAPI . components . schemas [ otherSchemaName ] )
526523 }
527524
528525 addToComponents ( type , schema , name ) {
@@ -581,7 +578,7 @@ class DefinitionGenerator {
581578 break ;
582579 }
583580
584- this . addToComponents ( ' securitySchemes' , schema , scheme )
581+ this . addToComponents ( this . componentTypes . securitySchemes , schema , scheme )
585582 }
586583 }
587584
0 commit comments