11import type { OpenAPIV3 } from "openapi-types" ;
2- import { traverse } from './utils/OpenApiTraverser ' ;
2+ import { SpecificationVisitor , SpecificationContext , traverseSpec , MaybeRef , is_ref } from './utils/SpecificationVisitor ' ;
33import isEqual from 'lodash.isequal' ;
44import { compressMultipleUnderscores , isPrimitiveType , resolveObj , isReferenceObject , isEmptyObjectSchema , is_simple_ref } from './utils/helper' ;
55import logger from "./utils/logger" ;
@@ -14,44 +14,42 @@ export class SchemaModifier {
1414 this . root = root ;
1515 }
1616 public modify ( ) : OpenAPIV3 . Document {
17- traverse ( this . root , {
18- onSchemaProperty : ( schema ) => {
19- this . deduplicateEnumValue ( schema )
20- this . handleAdditionalPropertiesUndefined ( schema )
21- this . convertNullTypeToNullValue ( schema )
22- this . deduplicateOneOfWithArrayType ( schema )
23- this . collapseSingleItemComposite ( schema ) ;
24- this . removeArrayOfMapWrapper ( schema )
25- } ,
26- onSchema : ( schema , schemaName ) => {
27- if ( ! schema || isReferenceObject ( schema ) ) return ;
28- this . deduplicateEnumValue ( schema )
29- this . convertAdditionalPropertiesToProperty ( schema )
30- this . handleAdditionalPropertiesUndefined ( schema )
31- this . convertNullTypeToNullValue ( schema )
32- this . handleOneOfConst ( schema , schemaName )
33- this . deduplicateOneOfWithArrayType ( schema )
34- this . collapseSingleItemComposite ( schema ) ;
35- this . collapseOneOfObjectPropContainsTitleSchema ( schema )
36- this . removeArrayOfMapWrapper ( schema )
37- this . convertOneOfToMinMaxProperties ( schema )
38- } ,
39- } ) ;
40- const visit = new Set ( ) ;
41- traverse ( this . root , {
42- onSchemaProperty : ( schema ) => {
43- this . simplifySingleMapSchema ( schema , visit ) ;
44- this . handleAdditionalPropertiesUndefined ( schema )
17+ const modifier = this ;
18+
19+ class FirstPassVisitor extends SpecificationVisitor {
20+ visit_schema ( ctx : SpecificationContext , schema : MaybeRef < OpenAPIV3 . SchemaObject > ) : void {
21+ super . visit_schema ( ctx , schema ) ;
22+ if ( is_ref ( schema ) ) return ;
23+
24+ const schemaName = ctx . key ;
25+ modifier . deduplicateEnumValue ( schema ) ;
26+ modifier . convertAdditionalPropertiesToProperty ( schema ) ;
27+ modifier . handleAdditionalPropertiesUndefined ( schema ) ;
28+ modifier . convertNullTypeToNullValue ( schema ) ;
29+ modifier . handleOneOfConst ( schema , schemaName ) ;
30+ modifier . deduplicateOneOfWithArrayType ( schema ) ;
31+ modifier . collapseSingleItemComposite ( schema ) ;
32+ modifier . collapseOneOfObjectPropContainsTitleSchema ( schema ) ;
33+ modifier . removeArrayOfMapWrapper ( schema ) ;
34+ modifier . convertOneOfToMinMaxProperties ( schema ) ;
35+ }
36+ }
37+ traverseSpec ( this . root , new FirstPassVisitor ( ) ) ;
4538
46- } ,
47- onSchema : ( schema ) => {
48- if ( ! schema || isReferenceObject ( schema ) ) return ;
49- this . simplifySingleMapSchema ( schema , visit )
50- this . handleAdditionalPropertiesUndefined ( schema )
51- this . markOneOfExtensions ( schema ) ;
52- } ,
53- } ) ;
54- return this . root
39+ const visited = new Set ( ) ;
40+ class SecondPassVisitor extends SpecificationVisitor {
41+ visit_schema ( ctx : SpecificationContext , schema : MaybeRef < OpenAPIV3 . SchemaObject > ) : void {
42+ super . visit_schema ( ctx , schema ) ;
43+ if ( is_ref ( schema ) ) return ;
44+
45+ modifier . simplifySingleMapSchema ( schema , visited ) ;
46+ modifier . handleAdditionalPropertiesUndefined ( schema ) ;
47+ modifier . markOneOfExtensions ( schema ) ;
48+ }
49+ }
50+ traverseSpec ( this . root , new SecondPassVisitor ( ) ) ;
51+
52+ return this . root ;
5553 }
5654
5755 // Converts `additionalProperties: true` or `additionalProperties: {}` to `type: object`.
0 commit comments