@@ -23,6 +23,7 @@ type CircularRefsIds = Map<OpenAPIV3.SchemaObject, string>;
2323export interface OpenAPISchemaPropertyEntry {
2424 propertyName ?: string ;
2525 required ?: boolean | null ;
26+ isDiscriminatorProperty ?: boolean ;
2627 schema : OpenAPIV3 . SchemaObject ;
2728}
2829
@@ -69,17 +70,19 @@ function OpenAPISchemaProperty(
6970 ) ;
7071 }
7172
72- if ( alternatives ) {
73+ if ( alternatives ?. schemas ) {
74+ const { schemas, discriminator } = alternatives ;
7375 return (
7476 < div className = "openapi-schema-alternatives" >
75- { alternatives . map ( ( alternativeSchema , index ) => (
77+ { schemas . map ( ( alternativeSchema , index ) => (
7678 < div key = { index } className = "openapi-schema-alternative" >
7779 < OpenAPISchemaAlternative
7880 schema = { alternativeSchema }
81+ discriminator = { discriminator }
7982 circularRefs = { circularRefs }
8083 context = { context }
8184 />
82- { index < alternatives . length - 1 ? (
85+ { index < schemas . length - 1 ? (
8386 < OpenAPISchemaAlternativeSeparator
8487 schema = { schema }
8588 context = { context }
@@ -228,11 +231,12 @@ export function OpenAPIRootSchemaFromServer(props: {
228231 */
229232function OpenAPISchemaAlternative ( props : {
230233 schema : OpenAPIV3 . SchemaObject ;
234+ discriminator : OpenAPIV3 . DiscriminatorObject | undefined ;
231235 circularRefs : CircularRefsIds ;
232236 context : OpenAPIClientContext ;
233237} ) {
234- const { schema, circularRefs, context } = props ;
235- const properties = getSchemaProperties ( schema ) ;
238+ const { schema, discriminator , circularRefs, context } = props ;
239+ const properties = getSchemaProperties ( schema , discriminator ) ;
236240
237241 return properties ?. length ? (
238242 < OpenAPIDisclosure
@@ -359,7 +363,7 @@ export function OpenAPISchemaPresentation(props: {
359363 context : OpenAPIClientContext ;
360364} ) {
361365 const {
362- property : { schema, propertyName, required } ,
366+ property : { schema, propertyName, required, isDiscriminatorProperty } ,
363367 context,
364368 } = props ;
365369
@@ -372,6 +376,7 @@ export function OpenAPISchemaPresentation(props: {
372376 schema = { schema }
373377 type = { getSchemaTitle ( schema ) }
374378 propertyName = { propertyName }
379+ isDiscriminatorProperty = { isDiscriminatorProperty }
375380 required = { required }
376381 context = { context }
377382 />
@@ -414,13 +419,19 @@ export function OpenAPISchemaPresentation(props: {
414419/**
415420 * Get the sub-properties of a schema.
416421 */
417- function getSchemaProperties ( schema : OpenAPIV3 . SchemaObject ) : null | OpenAPISchemaPropertyEntry [ ] {
422+ function getSchemaProperties (
423+ schema : OpenAPIV3 . SchemaObject ,
424+ discriminator ?: OpenAPIV3 . DiscriminatorObject | undefined
425+ ) : null | OpenAPISchemaPropertyEntry [ ] {
418426 // check array AND schema.items as this is sometimes null despite what the type indicates
419427 if ( schema . type === 'array' && schema . items && ! checkIsReference ( schema . items ) ) {
420428 const items = schema . items ;
421429 const itemProperties = getSchemaProperties ( items ) ;
422430 if ( itemProperties ) {
423- return itemProperties ;
431+ return itemProperties . map ( ( prop ) => ( {
432+ ...prop ,
433+ isDiscriminatorProperty : discriminator ?. propertyName === prop . propertyName ,
434+ } ) ) ;
424435 }
425436
426437 // If the items are a primitive type, we don't need to display them
@@ -451,6 +462,7 @@ function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISche
451462 required : Array . isArray ( schema . required )
452463 ? schema . required . includes ( propertyName )
453464 : undefined ,
465+ isDiscriminatorProperty : discriminator ?. propertyName === propertyName ,
454466 schema : propertySchema ,
455467 } ) ;
456468 } ) ;
@@ -471,14 +483,20 @@ function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISche
471483
472484type AlternativeType = 'oneOf' | 'allOf' | 'anyOf' ;
473485
486+ type SchemaAlternatives = {
487+ type : AlternativeType ;
488+ schemas : OpenAPIV3 . SchemaObject [ ] ;
489+ discriminator ?: OpenAPIV3 . DiscriminatorObject ;
490+ } | null ;
491+
474492/**
475493 * Get the alternatives to display for a schema.
476494 */
477495export function getSchemaAlternatives (
478496 schema : OpenAPIV3 . SchemaObject ,
479497 ancestors : Set < OpenAPIV3 . SchemaObject > = new Set ( )
480- ) : OpenAPIV3 . SchemaObject [ ] | null {
481- // Search for alternatives in the items property if it exists
498+ ) : SchemaAlternatives {
499+ // Check for nested alternatives in ` items`
482500 if (
483501 schema . items &&
484502 ( 'oneOf' in schema . items || 'allOf' in schema . items || 'anyOf' in schema . items )
@@ -487,32 +505,39 @@ export function getSchemaAlternatives(
487505 }
488506
489507 const alternatives :
490- | [ AlternativeType , ( OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ) [ ] ]
508+ | [
509+ AlternativeType ,
510+ ( OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ) [ ] ,
511+ OpenAPIV3 . DiscriminatorObject ?,
512+ ]
491513 | null = ( ( ) => {
492514 if ( schema . anyOf ) {
493- return [ 'anyOf' , schema . anyOf ] ;
515+ return [ 'anyOf' , schema . anyOf , schema . discriminator ] ;
494516 }
495-
496517 if ( schema . oneOf ) {
497- return [ 'oneOf' , schema . oneOf ] ;
518+ return [ 'oneOf' , schema . oneOf , schema . discriminator ] ;
498519 }
499-
500520 if ( schema . allOf ) {
501- return [ 'allOf' , schema . allOf ] ;
521+ return [ 'allOf' , schema . allOf , schema . discriminator ] ;
502522 }
503-
504523 return null ;
505524 } ) ( ) ;
506525
507526 if ( ! alternatives ) {
508527 return null ;
509528 }
510529
511- const [ type , schemas ] = alternatives ;
512- return mergeAlternatives (
530+ const [ type , schemas , discriminator ] = alternatives ;
531+
532+ return {
513533 type,
514- flattenAlternatives ( type , schemas , new Set ( ancestors ) . add ( schema ) )
515- ) ;
534+ schemas :
535+ mergeAlternatives (
536+ type ,
537+ flattenAlternatives ( type , schemas , new Set ( ancestors ) . add ( schema ) )
538+ ) ?? [ ] ,
539+ discriminator,
540+ } ;
516541}
517542
518543/**
@@ -610,10 +635,10 @@ function flattenAlternatives(
610635 }
611636
612637 if ( schemaOrRef [ alternativeType ] && ! ancestors . has ( schemaOrRef ) ) {
613- const schemas = getSchemaAlternatives ( schemaOrRef , ancestors ) ;
614- if ( schemas ) {
638+ const alternatives = getSchemaAlternatives ( schemaOrRef , ancestors ) ;
639+ if ( alternatives ?. schemas ) {
615640 acc . push (
616- ...schemas . map ( ( schema ) => ( {
641+ ...alternatives . schemas . map ( ( schema ) => ( {
617642 ...schema ,
618643 required : mergeRequiredFields ( schema , latestAncestor ) ,
619644 } ) )
0 commit comments