@@ -476,10 +476,7 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
476476 writer . WriteOptionalCollection ( OpenApiConstants . Enum , Enum , ( nodeWriter , s ) => nodeWriter . WriteAny ( s ) ) ;
477477
478478 // type
479- if ( Type is not null )
480- {
481- SerializeTypeProperty ( Type , writer , version ) ;
482- }
479+ SerializeTypeProperty ( Type , writer , version ) ;
483480
484481 // allOf
485482 writer . WriteOptionalCollection ( OpenApiConstants . AllOf , AllOf , callback ) ;
@@ -660,10 +657,7 @@ internal void SerializeAsV2(
660657 writer . WriteStartObject ( ) ;
661658
662659 // type
663- if ( Type is not null )
664- {
665- SerializeTypeProperty ( Type , writer , OpenApiSpecVersion . OpenApi2_0 ) ;
666- }
660+ SerializeTypeProperty ( Type , writer , OpenApiSpecVersion . OpenApi2_0 ) ;
667661
668662 // description
669663 writer . WriteProperty ( OpenApiConstants . Description , Description ) ;
@@ -794,8 +788,11 @@ internal void SerializeAsV2(
794788
795789 private void SerializeTypeProperty ( JsonSchemaType ? type , IOpenApiWriter writer , OpenApiSpecVersion version )
796790 {
797- var flagsCount = CountEnumSetFlags ( type ) ;
798- if ( flagsCount is 1 )
791+ if ( type is null )
792+ {
793+ return ;
794+ }
795+ if ( ! HasMultipleTypes ( type . Value ) )
799796 {
800797 // check whether nullable is true for upcasting purposes
801798 if ( version is OpenApiSpecVersion . OpenApi3_1 && ( Nullable || Extensions . ContainsKey ( OpenApiConstants . NullableExtension ) ) )
@@ -804,61 +801,50 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer,
804801 }
805802 else
806803 {
807- writer . WriteProperty ( OpenApiConstants . Type , type . ToIdentifier ( ) ) ;
804+ writer . WriteProperty ( OpenApiConstants . Type , type . Value . ToIdentifier ( ) ) ;
808805 }
809806 }
810- else if ( flagsCount > 1 )
807+ else
811808 {
812809 // type
813810 if ( version is OpenApiSpecVersion . OpenApi2_0 || version is OpenApiSpecVersion . OpenApi3_0 )
814811 {
815- DowncastTypeArrayToV2OrV3 ( type , writer , version , flagsCount ) ;
812+ DowncastTypeArrayToV2OrV3 ( type . Value , writer , version ) ;
816813 }
817814 else
818815 {
819- if ( type is not null )
816+ var list = new List < JsonSchemaType > ( ) ;
817+ foreach ( JsonSchemaType flag in jsonSchemaTypeValues )
820818 {
821- var list = new List < JsonSchemaType ? > ( ) ;
822- foreach ( JsonSchemaType flag in System . Enum . GetValues ( typeof ( JsonSchemaType ) ) )
819+ if ( type . Value . HasFlag ( flag ) )
823820 {
824- if ( type . Value . HasFlag ( flag ) )
825- {
826- list . Add ( flag ) ;
827- }
821+ list . Add ( flag ) ;
828822 }
823+ }
829824
830- writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s . ToIdentifier ( ) ) ) ;
831- }
825+ writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s . ToIdentifier ( ) ) ) ;
832826 }
833827 }
834828 }
835829
836- private static int CountEnumSetFlags ( JsonSchemaType ? schemaType )
830+ private static bool IsPowerOfTwo ( int x )
837831 {
838- int count = 0 ;
839-
840- if ( schemaType != null )
841- {
842- // Check each flag in the enum
843- foreach ( JsonSchemaType value in System . Enum . GetValues ( typeof ( JsonSchemaType ) ) )
844- {
845- // Check if the flag is set
846- if ( schemaType . Value . HasFlag ( value ) )
847- {
848- count ++ ;
849- }
850- }
851- }
832+ return x != 0 && ( x & ( x - 1 ) ) == 0 ;
833+ }
852834
853- return count ;
835+ private static bool HasMultipleTypes ( JsonSchemaType schemaType )
836+ {
837+ var schemaTypeNumeric = ( int ) schemaType ;
838+ return ! IsPowerOfTwo ( schemaTypeNumeric ) && // Boolean, Integer, Number, String, Array, Object
839+ schemaTypeNumeric != ( int ) JsonSchemaType . Null ;
854840 }
855841
856842 private void UpCastSchemaTypeToV31 ( JsonSchemaType ? type , IOpenApiWriter writer )
857843 {
858844 // create a new array and insert the type and "null" as values
859845 Type = type | JsonSchemaType . Null ;
860846 var list = new List < string > ( ) ;
861- foreach ( JsonSchemaType ? flag in System . Enum . GetValues ( typeof ( JsonSchemaType ) ) )
847+ foreach ( JsonSchemaType ? flag in jsonSchemaTypeValues )
862848 {
863849 // Check if the flag is set in 'type' using a bitwise AND operation
864850 if ( Type . Value . HasFlag ( flag ) )
@@ -870,7 +856,9 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer)
870856 writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s ) ) ;
871857 }
872858
873- private void DowncastTypeArrayToV2OrV3 ( JsonSchemaType ? schemaType , IOpenApiWriter writer , OpenApiSpecVersion version , int flagsCount )
859+ private static readonly Array jsonSchemaTypeValues = System . Enum . GetValues ( typeof ( JsonSchemaType ) ) ;
860+
861+ private void DowncastTypeArrayToV2OrV3 ( JsonSchemaType schemaType , IOpenApiWriter writer , OpenApiSpecVersion version )
874862 {
875863 /* If the array has one non-null value, emit Type as string
876864 * If the array has one null value, emit x-nullable as true
@@ -882,23 +870,12 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite
882870 ? OpenApiConstants . NullableExtension
883871 : OpenApiConstants . Nullable ;
884872
885- if ( flagsCount is 1 )
873+ if ( ! HasMultipleTypes ( schemaType ^ JsonSchemaType . Null ) && ( schemaType & JsonSchemaType . Null ) == JsonSchemaType . Null ) // checks for two values and one is null
886874 {
887- if ( schemaType is JsonSchemaType . Null )
888- {
889- writer . WriteProperty ( nullableProp , true ) ;
890- }
891- else
892- {
893- writer . WriteProperty ( OpenApiConstants . Type , schemaType . ToIdentifier ( ) ) ;
894- }
895- }
896- else if ( flagsCount is 2 && ( schemaType & JsonSchemaType . Null ) == JsonSchemaType . Null ) // checks for two values and one is null
897- {
898- foreach ( JsonSchemaType ? flag in System . Enum . GetValues ( typeof ( JsonSchemaType ) ) )
875+ foreach ( JsonSchemaType ? flag in jsonSchemaTypeValues )
899876 {
900877 // Skip if the flag is not set or if it's the Null flag
901- if ( schemaType . Value . HasFlag ( flag ) && flag != JsonSchemaType . Null )
878+ if ( schemaType . HasFlag ( flag ) && flag != JsonSchemaType . Null )
902879 {
903880 // Write the non-null flag value to the writer
904881 writer . WriteProperty ( OpenApiConstants . Type , flag . ToIdentifier ( ) ) ;
@@ -909,6 +886,17 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite
909886 writer . WriteProperty ( nullableProp , true ) ;
910887 }
911888 }
889+ else if ( ! HasMultipleTypes ( schemaType ) )
890+ {
891+ if ( schemaType is JsonSchemaType . Null )
892+ {
893+ writer . WriteProperty ( nullableProp , true ) ;
894+ }
895+ else
896+ {
897+ writer . WriteProperty ( OpenApiConstants . Type , schemaType . ToIdentifier ( ) ) ;
898+ }
899+ }
912900 }
913901 }
914902}
0 commit comments