@@ -572,6 +572,9 @@ function flattenAlternatives(
572572 schemasOrRefs : ( OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ) [ ] ,
573573 ancestors : Set < OpenAPIV3 . SchemaObject >
574574) : OpenAPIV3 . SchemaObject [ ] {
575+ // Get the parent schema's required fields from the most recent ancestor
576+ const latestAncestor = Array . from ( ancestors ) . pop ( ) ;
577+
575578 return schemasOrRefs . reduce < OpenAPIV3 . SchemaObject [ ] > ( ( acc , schemaOrRef ) => {
576579 if ( checkIsReference ( schemaOrRef ) ) {
577580 return acc ;
@@ -580,16 +583,47 @@ function flattenAlternatives(
580583 if ( schemaOrRef [ alternativeType ] && ! ancestors . has ( schemaOrRef ) ) {
581584 const schemas = getSchemaAlternatives ( schemaOrRef , ancestors ) ;
582585 if ( schemas ) {
583- acc . push ( ...schemas ) ;
586+ acc . push (
587+ ...schemas . map ( ( schema ) => ( {
588+ ...schema ,
589+ required : mergeRequiredFields ( schema , latestAncestor ) ,
590+ } ) )
591+ ) ;
584592 }
585593 return acc ;
586594 }
587595
588- acc . push ( schemaOrRef ) ;
596+ // For direct schemas, handle required fields
597+ const schema = {
598+ ...schemaOrRef ,
599+ required : mergeRequiredFields ( schemaOrRef , latestAncestor ) ,
600+ } ;
601+
602+ acc . push ( schema ) ;
589603 return acc ;
590604 } , [ ] ) ;
591605}
592606
607+ /**
608+ * Merge the required fields of a schema with the required fields of its latest ancestor.
609+ */
610+ function mergeRequiredFields (
611+ schemaOrRef : OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ,
612+ latestAncestor : OpenAPIV3 . SchemaObject | undefined
613+ ) {
614+ if ( ! schemaOrRef . required && ! latestAncestor ?. required ) {
615+ return undefined ;
616+ }
617+
618+ if ( checkIsReference ( schemaOrRef ) ) {
619+ return latestAncestor ?. required ;
620+ }
621+
622+ return Array . from (
623+ new Set ( [ ...( latestAncestor ?. required || [ ] ) , ...( schemaOrRef . required || [ ] ) ] )
624+ ) ;
625+ }
626+
593627function getSchemaTitle ( schema : OpenAPIV3 . SchemaObject ) : string {
594628 // Otherwise try to infer a nice title
595629 let type = 'any' ;
0 commit comments