@@ -6,7 +6,11 @@ import type {
66 SchemaType ,
77 SchemaWithRequired ,
88} from '../../../openApi/shared/types/schema' ;
9- import { discriminatorValues } from '../../../openApi/shared/utils/discriminator' ;
9+ import {
10+ convertDiscriminatorValue ,
11+ type DiscriminatorPropertyType ,
12+ discriminatorValues ,
13+ } from '../../../openApi/shared/utils/discriminator' ;
1014import { isTopLevelComponent , refToName } from '../../../utils/ref' ;
1115import type { ReferenceObject , SchemaObject } from '../types/spec' ;
1216
@@ -27,6 +31,52 @@ export const getSchemaType = ({
2731 return ;
2832} ;
2933
34+ /**
35+ * Finds the type of a discriminator property by looking it up in the provided schemas.
36+ * Searches through properties and allOf chains to find the property definition.
37+ */
38+ const findDiscriminatorPropertyType = ( {
39+ context,
40+ propertyName,
41+ schemas,
42+ } : {
43+ context : Context ;
44+ propertyName : string ;
45+ schemas : ReadonlyArray < SchemaObject | ReferenceObject > ;
46+ } ) : DiscriminatorPropertyType => {
47+ for ( const schema of schemas ) {
48+ const resolved = '$ref' in schema ? context . resolveRef < SchemaObject > ( schema . $ref ) : schema ;
49+
50+ // Check direct properties
51+ const property = resolved . properties ?. [ propertyName ] ;
52+ if ( property ) {
53+ const resolvedProperty =
54+ '$ref' in property ? context . resolveRef < SchemaObject > ( property . $ref ) : property ;
55+ if (
56+ resolvedProperty . type === 'boolean' ||
57+ resolvedProperty . type === 'integer' ||
58+ resolvedProperty . type === 'number'
59+ ) {
60+ return resolvedProperty . type ;
61+ }
62+ }
63+
64+ // Check allOf chains
65+ if ( resolved . allOf ) {
66+ const foundType = findDiscriminatorPropertyType ( {
67+ context,
68+ propertyName,
69+ schemas : resolved . allOf ,
70+ } ) ;
71+ if ( foundType !== 'string' ) {
72+ return foundType ;
73+ }
74+ }
75+ }
76+
77+ return 'string' ;
78+ } ;
79+
3080/**
3181 * Recursively finds discriminators in a schema, including nested allOf compositions.
3282 * This is needed when a schema extends another schema via allOf, and that parent
@@ -482,10 +532,16 @@ const parseAllOf = ({
482532 // Use allValues if we found children, otherwise use the original values
483533 const finalValues = allValues . length > 0 ? allValues : values ;
484534
485- const valueSchemas : ReadonlyArray < IR . SchemaObject > = finalValues . map ( ( value ) => ( {
486- const : value ,
487- type : 'string' ,
488- } ) ) ;
535+ // Detect the actual type of the discriminator property
536+ const propertyType = findDiscriminatorPropertyType ( {
537+ context,
538+ propertyName : discriminator . propertyName ,
539+ schemas : compositionSchemas ,
540+ } ) ;
541+
542+ const valueSchemas : ReadonlyArray < IR . SchemaObject > = finalValues . map ( ( value ) =>
543+ convertDiscriminatorValue ( value , propertyType ) ,
544+ ) ;
489545
490546 const discriminatorProperty : IR . SchemaObject =
491547 valueSchemas . length > 1
@@ -674,6 +730,14 @@ const parseAnyOf = ({
674730
675731 const compositionSchemas = schema . anyOf ;
676732
733+ const discriminatorPropertyType = schema . discriminator
734+ ? findDiscriminatorPropertyType ( {
735+ context,
736+ propertyName : schema . discriminator . propertyName ,
737+ schemas : compositionSchemas ,
738+ } )
739+ : undefined ;
740+
677741 for ( const compositionSchema of compositionSchemas ) {
678742 let irCompositionSchema = schemaToIrSchema ( {
679743 context,
@@ -684,10 +748,10 @@ const parseAnyOf = ({
684748 // `$ref` should be defined with discriminators
685749 if ( schema . discriminator && irCompositionSchema . $ref != null ) {
686750 const values = discriminatorValues ( irCompositionSchema . $ref , schema . discriminator . mapping ) ;
687- const valueSchemas : ReadonlyArray < IR . SchemaObject > = values . map ( ( value ) => ( {
688- const : value ,
689- type : 'string' ,
690- } ) ) ;
751+
752+ const valueSchemas : ReadonlyArray < IR . SchemaObject > = values . map ( ( value ) =>
753+ convertDiscriminatorValue ( value , discriminatorPropertyType ! ) ,
754+ ) ;
691755 const irDiscriminatorSchema : IR . SchemaObject = {
692756 properties : {
693757 [ schema . discriminator . propertyName ] :
@@ -834,6 +898,14 @@ const parseOneOf = ({
834898
835899 const compositionSchemas = schema . oneOf ;
836900
901+ const discriminatorPropertyType = schema . discriminator
902+ ? findDiscriminatorPropertyType ( {
903+ context,
904+ propertyName : schema . discriminator . propertyName ,
905+ schemas : compositionSchemas ,
906+ } )
907+ : undefined ;
908+
837909 for ( const compositionSchema of compositionSchemas ) {
838910 let irCompositionSchema = schemaToIrSchema ( {
839911 context,
@@ -844,10 +916,10 @@ const parseOneOf = ({
844916 // `$ref` should be defined with discriminators
845917 if ( schema . discriminator && irCompositionSchema . $ref != null ) {
846918 const values = discriminatorValues ( irCompositionSchema . $ref , schema . discriminator . mapping ) ;
847- const valueSchemas : ReadonlyArray < IR . SchemaObject > = values . map ( ( value ) => ( {
848- const : value ,
849- type : 'string' ,
850- } ) ) ;
919+
920+ const valueSchemas : ReadonlyArray < IR . SchemaObject > = values . map ( ( value ) =>
921+ convertDiscriminatorValue ( value , discriminatorPropertyType ! ) ,
922+ ) ;
851923 const irDiscriminatorSchema : IR . SchemaObject = {
852924 properties : {
853925 [ schema . discriminator . propertyName ] :
0 commit comments