1+ import { ParameterModel } from '../../../models/parameter.model' ;
12import { OpenAPIV3 } from 'openapi-types' ;
23import { EnumModel } from '../../../models/enum.model' ;
34import { ModelModel } from '../../../models/model.model' ;
45import { StoreI } from '../../../stores/entities.store' ;
56import { ModelType } from '../../../stores/model.store' ;
67import { ParserBaseService } from './parser-base.service' ;
8+ import { findInObject } from '../../../utils/objects.util' ;
79
810/**
911 * This service Process all COMPONENTS (components/schemas/*) and convert it to ModelModel instances
@@ -15,6 +17,29 @@ export class ComponentsParserService extends ParserBaseService {
1517 }
1618
1719 process ( ) : void {
20+ this . processParameters ( ) ;
21+ this . processSchemas ( ) ;
22+ }
23+
24+ private processParameters ( ) : void {
25+ console . group ( "Processing parameters from 'components'" ) ;
26+ const parameterRawList = this . document . components ?. parameters ;
27+ for ( let parameterName in parameterRawList ) {
28+ console . group ( 'Processing parameter' , parameterName ) ;
29+ const rawModel = parameterRawList [
30+ parameterName
31+ ] as OpenAPIV3 . ParameterObject ;
32+ const parameter = new ParameterModel (
33+ parameterName ,
34+ this . parseParameter ( rawModel , parameterName ) ,
35+ ) ;
36+ this . parameterStore . add ( parameter ) ;
37+ console . groupEnd ( ) ;
38+ }
39+ console . groupEnd ( ) ;
40+ }
41+
42+ private processSchemas ( ) : void {
1843 console . group ( "Processing models from 'components'" ) ;
1944 const modelRawList = this . document . components ?. schemas ;
2045 for ( let modelName in modelRawList ) {
@@ -23,34 +48,57 @@ export class ComponentsParserService extends ParserBaseService {
2348 modelName
2449 ] as OpenAPIV3 . NonArraySchemaObject ;
2550
26- let elementRef : ModelType ;
27- if ( rawModel . type === 'object' ) {
28- const modelInstance = new ModelModel ( modelName ) ;
29- elementRef = modelInstance ;
30- modelInstance . addAttributes (
31- this . parseAttributes ( rawModel , modelInstance . name ) ,
32- ) ;
33- this . modelStore . add ( modelInstance ) ;
34- } else if ( this . isEnumObject ( rawModel ) ) {
35- console . debug ( `${ modelName } is ENUM of type ${ rawModel . type } ` ) ;
36- const enumInstance = new EnumModel ( modelName ) ;
37- enumInstance . type = rawModel . type ;
38- enumInstance . values = rawModel . enum ;
39- this . modelStore . add ( enumInstance ) ;
40- } else {
41- console . error ( `ERROR: ${ modelName } not a correct Schema` ) ;
42- if ( ( rawModel as any ) ?. type === 'array' ) {
43- console . error ( `ERROR: ARRAY not supported on COMPONENTS SCHEMA` ) ;
44- }
51+ const schemaParsed = this . processSchema ( modelName , rawModel ) ;
52+ if ( schemaParsed ) {
53+ this . modelStore . add ( schemaParsed ) ;
4554 }
4655
47- if ( elementRef ) {
48- elementRef . description = rawModel . description ;
49- elementRef . example = rawModel . example ;
50- elementRef . deprecated = rawModel . deprecated ;
51- }
5256 console . groupEnd ( ) ;
5357 }
5458 console . groupEnd ( ) ;
5559 }
60+
61+ private processSchema (
62+ modelName ,
63+ rawModel : OpenAPIV3 . NonArraySchemaObject ,
64+ ) : ModelType {
65+ let elementRef : ModelType ;
66+ if ( rawModel . type === 'object' ) {
67+ elementRef = new ModelModel ( modelName ) ;
68+ elementRef . addAttributes ( this . parseAttributes ( rawModel , elementRef . name ) ) ;
69+ } else if ( this . isEnumObject ( rawModel ) ) {
70+ console . debug ( `${ modelName } is ENUM of type ${ rawModel . type } ` ) ;
71+ elementRef = new EnumModel ( modelName ) ;
72+ elementRef . type = rawModel . type ;
73+ elementRef . values = rawModel . enum ;
74+ } else if ( this . isRefObject ( rawModel ) ) {
75+ const ref = ( rawModel as OpenAPIV3 . ReferenceObject ) . $ref ;
76+ console . error ( `${ modelName } is a pure ref of ${ ref } ` ) ;
77+ const refSchema = this . findRefSchema ( ref ) ;
78+ if ( ! refSchema ) {
79+ throw `Schema ${ ref } not found` ;
80+ }
81+ return this . processSchema ( modelName , refSchema ) ;
82+ } else {
83+ console . error ( `ERROR: ${ modelName } not a correct Schema` ) ;
84+ if ( ( rawModel as any ) ?. type === 'array' ) {
85+ console . error ( `ERROR: ARRAY not supported on COMPONENTS SCHEMA` ) ;
86+ }
87+ }
88+
89+ if ( elementRef ) {
90+ elementRef . description = rawModel . description ;
91+ elementRef . example = rawModel . example ;
92+ elementRef . deprecated = rawModel . deprecated ;
93+ }
94+ return elementRef ;
95+ }
96+
97+ private findRefSchema ( $ref : string ) : OpenAPIV3 . NonArraySchemaObject {
98+ if ( $ref . indexOf ( '#' ) === 0 ) {
99+ return findInObject ( $ref , this . document ) ;
100+ }
101+
102+ throw `$ref: ${ $ref } not supported yet!` ;
103+ }
56104}
0 commit comments