@@ -82,6 +82,25 @@ class SchemaBuilder {
8282 }
8383 }
8484
85+ /** Find the sequence node whose child bindings match the struct's field names. */
86+ private findStructNode (
87+ node : Expr ,
88+ type : Extract < BoundType , { kind : "struct" } > ,
89+ ) : Extract < Expr , { kind : "sequence" } > | undefined {
90+ if ( node . kind !== "sequence" ) return undefined ;
91+ // Check if any child binding matches a field name
92+ for ( const child of node . attrs . nodes ) {
93+ const binding = this . ctx . resolve ( child ) ;
94+ if ( binding && binding . name in type . fields ) return node ;
95+ }
96+ // Recurse into child sequences (handles collapsed flag sequences)
97+ for ( const child of node . attrs . nodes ) {
98+ const result = this . findStructNode ( child , type ) ;
99+ if ( result ) return result ;
100+ }
101+ return undefined ;
102+ }
103+
85104 private findTerminal ( node : Expr ) : Expr {
86105 switch ( node . kind ) {
87106 case "optional" :
@@ -122,8 +141,9 @@ class SchemaBuilder {
122141 const properties : Record < string , JsonSchema > = { } ;
123142 const required : string [ ] = [ ] ;
124143
125- if ( node ?. kind === "sequence" ) {
126- for ( const child of node . attrs . nodes ) {
144+ const structNode = node ? this . findStructNode ( node , type ) : undefined ;
145+ if ( structNode ) {
146+ for ( const child of structNode . attrs . nodes ) {
127147 const childBinding = this . ctx . resolve ( child ) ;
128148 if ( childBinding && childBinding . name in type . fields ) {
129149 properties [ childBinding . name ] = this . fromBinding ( childBinding ) ;
0 commit comments