11import { isObject } from "../utils/isObject" ;
22import InvalidTypeError from "../errors/invalid-type-error" ;
3- import type { OptionsInternal } from "../../types" ;
3+ import type { OptionsInternal } from "../../openapi-schema- types" ;
44import { cloneDeep } from "lodash-es" ;
5- import type { STRUCTS } from "../../consts" ;
65import type { JSONSchema4 , JSONSchema4TypeName } from "json-schema" ;
76import { VALID_OPENAPI_FORMATS } from "../../consts" ;
87import type { SchemaObject } from "openapi-typescript/src/types" ;
9- import type { PatternPropertiesHandler } from "../../types" ;
8+ import type { PatternPropertiesHandler } from "../../openapi-schema- types" ;
109import type { OpenAPI3 } from "openapi-typescript" ;
1110import type { ReferenceObject } from "openapi-typescript/src/types" ;
1211
@@ -25,17 +24,19 @@ function convertSchema(schema: OpenAPI3 | SchemaObject | ReferenceObject, option
2524 const structs = options . _structs ;
2625 const notSupported = options . _notSupported ;
2726 const strictMode = options . strictMode ;
28- let i = 0 ;
29- let j = 0 ;
30- let struct : typeof STRUCTS [ number ] | null = null ;
27+ const definitionKeywords = options . definitionKeywords || [ ] ;
28+ const beforeTransform = options . beforeTransform ;
29+ const afterTransform = options . afterTransform ;
3130
32- for ( i ; i < structs . length ; i ++ ) {
33- struct = structs [ i ] ;
31+ if ( beforeTransform ) {
32+ schema = beforeTransform ( schema , options ) ;
33+ }
3434
35+ for ( const struct of structs ) {
3536 if ( Array . isArray ( schema [ struct ] ) ) {
3637 let cloned = false ;
3738
38- for ( j ; j < schema [ struct ] . length ; j ++ ) {
39+ for ( let j = 0 ; j < schema [ struct ] . length ; j ++ ) {
3940 if ( ! isObject ( schema [ struct ] [ j ] ) ) {
4041 if ( options . cloneSchema && ! cloned ) {
4142 cloned = true ;
@@ -57,12 +58,18 @@ function convertSchema(schema: OpenAPI3 | SchemaObject | ReferenceObject, option
5758 }
5859 let convertedSchema = schema as SchemaObject ;
5960
61+ for ( const def of definitionKeywords ) {
62+ if ( typeof schema [ def ] === "object" ) {
63+ schema [ def ] = convertProperties ( schema [ def ] , options ) ;
64+ }
65+ }
66+
6067 if ( "properties" in convertedSchema ) {
6168 convertedSchema . properties = convertProperties ( convertedSchema . properties , options ) ;
6269
6370 if ( Array . isArray ( convertedSchema . required ) ) {
6471 convertedSchema . required = convertedSchema . required . filter (
65- ( key ) => convertedSchema . properties ?. [ key ] !== undefined ,
72+ ( key ) => "properties" in convertedSchema && convertedSchema . properties ?. [ key ] !== undefined ,
6673 ) ;
6774 if ( convertedSchema . required . length === 0 ) {
6875 delete convertedSchema . required ;
@@ -73,7 +80,7 @@ function convertSchema(schema: OpenAPI3 | SchemaObject | ReferenceObject, option
7380 }
7481 }
7582
76- if ( strictMode ) {
83+ if ( strictMode && "type" in convertedSchema ) {
7784 validateType ( convertedSchema . type ) ;
7885 }
7986
@@ -84,8 +91,12 @@ function convertSchema(schema: OpenAPI3 | SchemaObject | ReferenceObject, option
8491 convertedSchema = convertPatternProperties ( convertedSchema , options . patternPropertiesHandler ) ;
8592 }
8693
87- for ( i = 0 ; i < notSupported . length ; i ++ ) {
88- delete convertedSchema [ notSupported [ i ] ] ;
94+ for ( const item of notSupported ) {
95+ delete convertedSchema [ item ] ;
96+ }
97+
98+ if ( afterTransform ) {
99+ return afterTransform ( convertedSchema , options ) ;
89100 }
90101
91102 return convertedSchema as JSONSchema4 ;
@@ -130,13 +141,15 @@ function convertProperties(
130141}
131142
132143function convertTypes ( schema : SchemaObject ) {
133- const type = schema . type as JSONSchema4TypeName ;
134- const schemaEnum = schema . enum as unknown [ ] ;
135- if ( type !== undefined && schema . nullable === true ) {
136- ( < JSONSchema4 > schema ) . type = [ type , "null" ] ;
137-
138- if ( Array . isArray ( schemaEnum ) && ! schemaEnum . includes ( null ) ) {
139- schema . enum = schemaEnum . concat ( [ null ] ) ;
144+ if ( "type" in schema ) {
145+ const type = schema . type as JSONSchema4TypeName ;
146+ const schemaEnum = schema . enum as ( string | null ) [ ] ;
147+ if ( type !== undefined && schema . nullable === true ) {
148+ ( < JSONSchema4 > schema ) . type = [ type , "null" ] ;
149+ if ( Array . isArray ( schemaEnum ) && ! schemaEnum . includes ( null ) ) {
150+ // @ts -ignore
151+ schema . enum = schemaEnum . concat ( [ null ] ) ;
152+ }
140153 }
141154 }
142155
0 commit comments