1+ import { EdgesRefDict , Nodes , OpenAPIGraphsBuilderInterface , SchemaNodeInterface } from 'openapi-graph-types' ;
12import { OpenAPIV3 } from 'openapi-types' ;
2- import { EdgesRefDict , Nodes , OpenAPIGraphInterface } from 'openapi-graph-types' ;
33import { SchemaNode } from '../../graph/nodes/SchemaNode' ;
44import { RefEdge } from '../edges' ;
55
6+ /**
7+ * Creates a new Schema depends on its type. Swagger schemas can be Array or NonArray
8+ *
9+ * @param schema source
10+ * @param name The given name for the schema
11+ * @returns the new schema instance
12+ */
13+ function createSchema (
14+ schemaNodes : { [ key : string ] : SchemaNodeInterface } ,
15+ schema : OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ,
16+ schemaName : string ,
17+ isInline : boolean ,
18+ ) {
19+ if ( schema && ! ( '$ref' in schema ) ) {
20+ if ( 'type' in schema && schema ?. type === 'array' ) {
21+ schemaNodes [ schemaName ] = new SchemaNode ( schemaName , schema , isInline ) ;
22+ } else {
23+ schemaNodes [ schemaName ] = new SchemaNode ( schemaName , schema , isInline ) ;
24+ }
25+ }
26+ }
27+ 28+ export function getDefinedSchemasNodes ( api : OpenAPIV3 . Document ) : { [ key : string ] : SchemaNodeInterface } {
29+ const nodes : { [ key : string ] : SchemaNodeInterface } = { } ;
30+ const schemas : { [ key : string ] : OpenAPIV3 . ReferenceObject | OpenAPIV3 . SchemaObject } | undefined =
31+ api . components ?. schemas ;
32+ if ( schemas ) {
33+ Object . keys ( schemas ) . forEach ( ( schemaName ) => createSchema ( nodes , schemas [ schemaName ] , schemaName , false ) ) ;
34+ }
35+ return nodes ;
36+ }
37+ 638/**
739 * It will find all the schemas defined in the specification
840 *
941 * @param api source
1042 * @param fn callback which will be executed for every node
1143 */
12- export function getSchemaNodes ( apiContent : OpenAPIV3 . Document ) : Nodes [ 'schemas' ] {
13- const nodes : Nodes [ 'schemas' ] = { } ;
14- const schemas = apiContent ?. components ?. schemas ;
15- if ( schemas ) {
16- Object . keys ( schemas ) . forEach ( ( schema ) => {
17- if ( '$ref' !== schema ) {
18- nodes [ schema ] = new SchemaNode ( schema , schemas [ schema ] as OpenAPIV3 . SchemaObject ) ;
19- }
20- } ) ;
44+ export function getInlineSchemasNodes (
45+ json : any ,
46+ currentIndex = 1 ,
47+ nodes : { [ key : string ] : SchemaNodeInterface } = { } ,
48+ ) : { [ key : string ] : SchemaNodeInterface } {
49+ const schema : OpenAPIV3 . SchemaObject | undefined = json ?. schema ;
50+ if ( schema ) {
51+ createSchema ( nodes , schema , `inline-schema-${ currentIndex ++ } ` , true ) ;
52+ if ( schema ?. type === 'array' ) {
53+ getInlineSchemasNodes ( json . items , currentIndex , nodes ) ;
54+ }
55+ } else if ( json ) {
56+ function handleJson ( ) {
57+ Object . keys ( json ) . forEach ( ( key ) => {
58+ nodes = getInlineSchemasNodes ( json [ key ] , currentIndex , nodes ) ;
59+ } ) ;
60+ }
61+ if ( { } . constructor === json . constructor ) {
62+ handleJson ( ) ;
63+ } else if ( [ ] . constructor === json . constructor ) {
64+ json . forEach ( handleJson ) ;
65+ }
2166 }
2267 return nodes ;
2368}
2469
70+ export function getSchemaNodes ( api : OpenAPIV3 . Document ) : Nodes [ 'schemas' ] {
71+ return { ...getDefinedSchemasNodes ( api ) , ...getInlineSchemasNodes ( api ) } ;
72+ }
73+ 2574/**
2675 * It will find all the references defined in the specification
2776 *
2877 * @param api source
2978 * @param fn callback which will be executed for every node
3079 */
3180export function getRefEdges ( json : any , absolutePath : string , edges : EdgesRefDict = { schemaRef : { } } ) : EdgesRefDict {
32- /* tslint:disable:no-string-literal */
33- const ref : string = json [ '$ref' ] ;
81+ const ref : string | undefined = json ?. $ref ;
3482 if ( ref ) {
3583 // TODO Should test any type of component
3684 if ( / c o m p o n e n t s \/ s c h e m a s / . test ( ref ) ) {
@@ -39,7 +87,7 @@ export function getRefEdges(json: any, absolutePath: string, edges: EdgesRefDict
3987 } else {
4088 function handleJson ( ) {
4189 Object . keys ( json ) . forEach ( ( key ) => {
42- edges = getRefEdges ( json [ key ] , absolutePath , edges ) ;
90+ getRefEdges ( json [ key ] , absolutePath , edges ) ;
4391 } ) ;
4492 }
4593 if ( { } . constructor === json . constructor ) {
@@ -51,17 +99,26 @@ export function getRefEdges(json: any, absolutePath: string, edges: EdgesRefDict
5199 return edges ;
52100}
53101
54- export function resolveReference ( graphs : OpenAPIGraphInterface [ ] , refs : EdgesRefDict ) : EdgesRefDict {
102+ /**
103+ * Sets the edges' child value
104+ * @param graphs
105+ * @param refs
106+ * @returns
107+ */
108+ export function resolveReference ( graphs : OpenAPIGraphsBuilderInterface [ 'graphs' ] , refs : EdgesRefDict ) : EdgesRefDict {
55109 const filteredRefs : EdgesRefDict = {
56110 schemaRef : { } ,
57111 } ;
58112
59113 Object . values ( refs . schemaRef )
60- . map ( ( r ) => ( { r, g : graphs . find ( ( g ) => g . path === r . absolutePath ) } ) )
61- . filter ( ( o ) => o . g ?. nodes . schemas [ o . r . tokenName ] )
62- . forEach ( ( o ) => {
63- o . r . child = o . g ?. nodes . schemas [ o . r . tokenName ] ;
64- filteredRefs . schemaRef [ o . r . getFullPath ( ) ] = o . r ;
114+ . filter ( ( r ) => graphs ?. [ r . refToFilePath ] . nodes . schemas [ r . tokenName ] )
115+ . forEach ( ( r ) => {
116+ const schema = graphs ?. [ r . refToFilePath ] . nodes . schemas [ r . tokenName ] ;
117+ if ( schema ) {
118+ schema . referencedBy [ r . path ] = r ;
119+ r . child = schema ;
120+ filteredRefs . schemaRef [ r . path ] = r ;
121+ }
65122 } ) ;
66123
67124 return filteredRefs ;
0 commit comments