@@ -382,7 +382,7 @@ export function OpenAPISchemaPresentation(props: {
382382 < div id = { id } className = "openapi-schema-presentation" >
383383 < OpenAPISchemaName
384384 schema = { schema }
385- type = { getSchemaTitle ( schema ) }
385+ type = { getSchemaTitle ( schema , { ignoreAlternatives : ! propertyName } ) }
386386 propertyName = { propertyName }
387387 isDiscriminatorProperty = { isDiscriminatorProperty }
388388 required = { required }
@@ -686,36 +686,102 @@ function flattenAlternatives(
686686 schemasOrRefs : ( OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ) [ ] ,
687687 ancestors : Set < OpenAPIV3 . SchemaObject >
688688) : OpenAPIV3 . SchemaObject [ ] {
689- // Get the parent schema's required fields from the most recent ancestor
690689 const latestAncestor = Array . from ( ancestors ) . pop ( ) ;
690+ const result : OpenAPIV3 . SchemaObject [ ] = [ ] ;
691691
692- return schemasOrRefs . reduce < OpenAPIV3 . SchemaObject [ ] > ( ( acc , schemaOrRef ) => {
692+ for ( const schemaOrRef of schemasOrRefs ) {
693693 if ( checkIsReference ( schemaOrRef ) ) {
694- return acc ;
694+ continue ;
695695 }
696696
697- if ( schemaOrRef [ alternativeType ] && ! ancestors . has ( schemaOrRef ) ) {
698- const alternatives = getSchemaAlternatives ( schemaOrRef , ancestors ) ;
699- if ( alternatives ?. schemas ) {
700- acc . push (
701- ...alternatives . schemas . map ( ( schema ) => ( {
702- ...schema ,
703- required : mergeRequiredFields ( schema , latestAncestor ) ,
704- } ) )
705- ) ;
697+ const flattened = flattenSchema ( schemaOrRef , alternativeType , ancestors , latestAncestor ) ;
698+
699+ if ( flattened ) {
700+ result . push ( ...flattened ) ;
701+ }
702+ }
703+
704+ return result ;
705+ }
706+
707+ function flattenSchema (
708+ schema : OpenAPIV3 . SchemaObject ,
709+ alternativeType : AlternativeType ,
710+ ancestors : Set < OpenAPIV3 . SchemaObject > ,
711+ latestAncestor : OpenAPIV3 . SchemaObject | undefined
712+ ) : OpenAPIV3 . SchemaObject [ ] {
713+ if ( schema [ alternativeType ] && ! ancestors . has ( schema ) ) {
714+ const alternatives = getSchemaAlternatives ( schema , ancestors ) ;
715+ if ( alternatives ?. schemas && alternatives . type === alternativeType ) {
716+ return alternatives . schemas . map ( ( s ) => ( {
717+ ...s ,
718+ required : mergeRequiredFields ( s , latestAncestor ) ,
719+ } ) ) ;
720+ }
721+
722+ return [
723+ {
724+ ...schema ,
725+ required : mergeRequiredFields ( schema , latestAncestor ) ,
726+ } ,
727+ ] ;
728+ }
729+
730+ if (
731+ ( alternativeType === 'oneOf' || alternativeType === 'anyOf' ) &&
732+ schema . allOf &&
733+ Array . isArray ( schema . allOf ) &&
734+ ! ancestors . has ( schema )
735+ ) {
736+ const allOfSchemas = schema . allOf . filter (
737+ ( s ) : s is OpenAPIV3 . SchemaObject => ! checkIsReference ( s )
738+ ) ;
739+
740+ if ( allOfSchemas . length > 0 ) {
741+ const merged = mergeAlternatives ( 'allOf' , allOfSchemas ) ;
742+ if ( merged && merged . length > 0 ) {
743+ return merged . map ( ( s ) => {
744+ const required = mergeRequiredFields ( s , latestAncestor ) ;
745+ const result : OpenAPIV3 . SchemaObject = {
746+ ...s ,
747+ ...( required !== undefined && { required } ) ,
748+ } ;
749+
750+ if ( schema . title && ! s . title ) {
751+ result . title = schema . title ;
752+ }
753+
754+ return result ;
755+ } ) ;
706756 }
707- return acc ;
708757 }
758+ }
709759
710- // For direct schemas, handle required fields
711- const schema = {
712- ...schemaOrRef ,
713- required : mergeRequiredFields ( schemaOrRef , latestAncestor ) ,
714- } ;
760+ const title = inferSchemaTitle ( schema ) ;
761+ const required = mergeRequiredFields ( schema , latestAncestor ) ;
762+
763+ return [
764+ {
765+ ...schema ,
766+ ...( required !== undefined && { required } ) ,
767+ ...( title ? { title } : { } ) ,
768+ } ,
769+ ] ;
770+ }
771+
772+ function inferSchemaTitle ( schema : OpenAPIV3 . SchemaObject ) : string | undefined {
773+ if ( schema . title ) {
774+ return schema . title ;
775+ }
776+
777+ if ( schema . properties ) {
778+ const keys = Object . keys ( schema . properties ) ;
779+ if ( keys . length > 0 ) {
780+ return keys [ 0 ] ;
781+ }
782+ }
715783
716- acc . push ( schema ) ;
717- return acc ;
718- } , [ ] ) ;
784+ return undefined ;
719785}
720786
721787/**
0 commit comments