@@ -8,6 +8,29 @@ export interface TypedDictResult {
88export function convertToTypedDict ( name : string , schema : OpenAPI . SchemaObject | OpenAPI . ReferenceObject ) : TypedDictResult {
99 const typingImports = new Set < string > ( [ 'TypedDict' ] ) ;
1010
11+ function flatten ( s : OpenAPI . SchemaObject | OpenAPI . ReferenceObject ) : { bases : string [ ] ; schema : OpenAPI . SchemaObject | null } {
12+ if ( '$ref' in s ) {
13+ const match = s . $ref . match ( / ^ # \/ c o m p o n e n t s \/ s c h e m a s \/ ( .+ ) $ / ) ;
14+ return { bases : [ match ?. [ 1 ] ?? s . $ref ] , schema : null } ;
15+ }
16+
17+ if ( 'allOf' in s && Array . isArray ( s . allOf ) ) {
18+ const bases : string [ ] = [ ] ;
19+ const merged : OpenAPI . SchemaObject = { type : 'object' , properties : { } , required : [ ] } ;
20+ for ( const sub of s . allOf as Array < OpenAPI . SchemaObject | OpenAPI . ReferenceObject > ) {
21+ const res = flatten ( sub ) ;
22+ bases . push ( ...res . bases ) ;
23+ if ( res . schema ) {
24+ Object . assign ( merged . properties ! , res . schema . properties ) ;
25+ merged . required = Array . from ( new Set ( [ ...( merged . required ?? [ ] ) , ...( res . schema . required ?? [ ] ) ] ) ) ;
26+ }
27+ }
28+ return { bases, schema : merged } ;
29+ }
30+
31+ return { bases : [ ] , schema : s } ;
32+ }
33+
1134 function toType ( s : OpenAPI . SchemaObject | OpenAPI . ReferenceObject ) : string {
1235 if ( '$ref' in s ) {
1336 const match = s . $ref . match ( / ^ # \/ c o m p o n e n t s \/ s c h e m a s \/ ( .+ ) $ / ) ;
@@ -43,10 +66,12 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject |
4366 }
4467 }
4568
69+ const { bases, schema : flat } = flatten ( schema ) ;
70+
4671 let definition = '' ;
47- if ( ! ( '$ref' in schema ) && schema . type === 'object' ) {
48- const props = schema . properties ?? { } ;
49- const required = new Set ( schema . required ?? [ ] ) ;
72+ if ( flat && flat . type === 'object' ) {
73+ const props = flat . properties ?? { } ;
74+ const required = new Set ( flat . required ?? [ ] ) ;
5075 const fields : string [ ] = [ ] ;
5176 const attrLines : string [ ] = [ ] ;
5277 for ( const [ key , value ] of Object . entries ( props ) ) {
@@ -65,13 +90,14 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject |
6590 if ( fields . length === 0 ) {
6691 fields . push ( ' pass' ) ;
6792 }
68- const header = [ `class ${ name } (TypedDict, total=False):` ] ;
93+ const baseList = bases . length > 0 ? `${ bases . join ( ', ' ) } , ` : '' ;
94+ const header = [ `class ${ name } (${ baseList } TypedDict, total=False):` ] ;
6995 const docLines : string [ ] = [ ] ;
70- if ( schema . description ) {
71- docLines . push ( ( schema . description as string ) . replace ( / \n / g, ' ' ) ) ;
96+ if ( flat . description ) {
97+ docLines . push ( ( flat . description as string ) . replace ( / \n / g, ' ' ) ) ;
7298 }
7399 if ( attrLines . length > 0 ) {
74- if ( schema . description ) docLines . push ( '' ) ;
100+ if ( flat . description ) docLines . push ( '' ) ;
75101 docLines . push ( 'Attributes:' ) ;
76102 for ( const line of attrLines ) {
77103 docLines . push ( ` ${ line } ` ) ;
0 commit comments