@@ -33,49 +33,65 @@ fn gen_struct(name: Option<&LitStr>, fields: &[ParseDataField]) -> TokenStream {
3333 None => quote ! ( #option:: None )
3434 } ;
3535
36- let field_name = fields. iter ( ) . map ( |f| & f. name ) ;
37- let field_doc = fields. iter ( ) . map ( |f| gen_doc_option ( & f. doc ) ) ;
38- let field_schema = fields. iter ( ) . map ( |f| match & f. ty {
39- TypeOrInline :: Type ( ty) => {
40- quote ! ( <#ty as :: openapi_type:: OpenapiType >:: schema( ) )
41- } ,
42- TypeOrInline :: Inline ( data) => data. gen_schema ( )
36+ let fields = fields. iter ( ) . map ( |f| {
37+ let name = & f. name ;
38+ let doc = gen_doc_option ( & f. doc ) ;
39+ let schema = match & f. ty {
40+ TypeOrInline :: Type ( ty) => {
41+ quote ! ( <#ty as :: openapi_type:: OpenapiType >:: schema( ) )
42+ } ,
43+ TypeOrInline :: Inline ( data) => data. gen_schema ( )
44+ } ;
45+
46+ if f. flatten {
47+ quote ! ( {
48+ let field_schema = #schema;
49+ :: openapi_type:: private:: flatten(
50+ & mut dependencies,
51+ & mut properties,
52+ & mut required,
53+ field_schema
54+ ) ;
55+ } )
56+ } else {
57+ quote ! ( {
58+ const FIELD_NAME : & :: core:: primitive:: str = #name;
59+ const FIELD_DOC : #option<& ' static :: core:: primitive:: str > = #doc;
60+
61+ let mut field_schema = #schema;
62+
63+ // fields in OpenAPI are nullable by default
64+ match field_schema. nullable {
65+ true => field_schema. nullable = false ,
66+ false => required. push( :: std:: string:: String :: from( FIELD_NAME ) )
67+ } ;
68+
69+ let field_schema = :: openapi_type:: private:: inline_if_unnamed(
70+ & mut dependencies, field_schema, FIELD_DOC
71+ ) ;
72+ let field_schema = match field_schema {
73+ #openapi:: ReferenceOr :: Item ( schema) => {
74+ #openapi:: ReferenceOr :: Item ( :: std:: boxed:: Box :: new( schema) )
75+ } ,
76+ #openapi:: ReferenceOr :: Reference { reference } => {
77+ #openapi:: ReferenceOr :: Reference { reference }
78+ }
79+ } ;
80+
81+ properties. insert(
82+ :: std:: string:: String :: from( FIELD_NAME ) ,
83+ field_schema
84+ ) ;
85+ } )
86+ }
4387 } ) ;
4488
4589 quote ! {
4690 {
47- let mut properties = <:: openapi_type:: indexmap:: IndexMap <
48- :: std:: string:: String ,
49- #openapi:: ReferenceOr <:: std:: boxed:: Box <#openapi:: Schema >>
50- >>:: new( ) ;
51- let mut required = <:: std:: vec:: Vec <:: std:: string:: String >>:: new( ) ;
52-
53- #( {
54- const FIELD_NAME : & :: core:: primitive:: str = #field_name;
55- const FIELD_DOC : #option<& ' static :: core:: primitive:: str > = #field_doc;
56-
57- let mut field_schema = #field_schema;
58-
59- // fields in OpenAPI are nullable by default
60- match field_schema. nullable {
61- true => field_schema. nullable = false ,
62- false => required. push( :: std:: string:: String :: from( FIELD_NAME ) )
63- } ;
64-
65- let field_schema = match :: openapi_type:: private:: inline_if_unnamed(
66- & mut dependencies, field_schema, FIELD_DOC
67- ) {
68- #openapi:: ReferenceOr :: Item ( schema) =>
69- #openapi:: ReferenceOr :: Item ( :: std:: boxed:: Box :: new( schema) ) ,
70- #openapi:: ReferenceOr :: Reference { reference } =>
71- #openapi:: ReferenceOr :: Reference { reference }
72- } ;
73-
74- properties. insert(
75- :: std:: string:: String :: from( FIELD_NAME ) ,
76- field_schema
77- ) ;
78- } ) *
91+ let mut properties = :: openapi_type:: private:: Properties :: new( ) ;
92+ let mut required = :: openapi_type:: private:: Required :: new( ) ;
93+
94+ #( #fields) *
7995
8096 let mut schema = :: openapi_type:: OpenapiSchema :: new(
8197 #openapi:: SchemaKind :: Type (
0 commit comments