@@ -476,10 +476,7 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
476
476
writer . WriteOptionalCollection ( OpenApiConstants . Enum , Enum , ( nodeWriter , s ) => nodeWriter . WriteAny ( s ) ) ;
477
477
478
478
// type
479
- if ( Type is not null )
480
- {
481
- SerializeTypeProperty ( Type , writer , version ) ;
482
- }
479
+ SerializeTypeProperty ( Type , writer , version ) ;
483
480
484
481
// allOf
485
482
writer . WriteOptionalCollection ( OpenApiConstants . AllOf , AllOf , callback ) ;
@@ -660,10 +657,7 @@ internal void SerializeAsV2(
660
657
writer . WriteStartObject ( ) ;
661
658
662
659
// type
663
- if ( Type is not null )
664
- {
665
- SerializeTypeProperty ( Type , writer , OpenApiSpecVersion . OpenApi2_0 ) ;
666
- }
660
+ SerializeTypeProperty ( Type , writer , OpenApiSpecVersion . OpenApi2_0 ) ;
667
661
668
662
// description
669
663
writer . WriteProperty ( OpenApiConstants . Description , Description ) ;
@@ -794,8 +788,11 @@ internal void SerializeAsV2(
794
788
795
789
private void SerializeTypeProperty ( JsonSchemaType ? type , IOpenApiWriter writer , OpenApiSpecVersion version )
796
790
{
797
- var flagsCount = CountEnumSetFlags ( type ) ;
798
- if ( flagsCount is 1 )
791
+ if ( type is null )
792
+ {
793
+ return ;
794
+ }
795
+ if ( ! HasMultipleTypes ( type . Value ) )
799
796
{
800
797
// check whether nullable is true for upcasting purposes
801
798
if ( version is OpenApiSpecVersion . OpenApi3_1 && ( Nullable || Extensions . ContainsKey ( OpenApiConstants . NullableExtension ) ) )
@@ -804,61 +801,50 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer,
804
801
}
805
802
else
806
803
{
807
- writer . WriteProperty ( OpenApiConstants . Type , type . ToIdentifier ( ) ) ;
804
+ writer . WriteProperty ( OpenApiConstants . Type , type . Value . ToIdentifier ( ) ) ;
808
805
}
809
806
}
810
- else if ( flagsCount > 1 )
807
+ else
811
808
{
812
809
// type
813
810
if ( version is OpenApiSpecVersion . OpenApi2_0 || version is OpenApiSpecVersion . OpenApi3_0 )
814
811
{
815
- DowncastTypeArrayToV2OrV3 ( type , writer , version , flagsCount ) ;
812
+ DowncastTypeArrayToV2OrV3 ( type . Value , writer , version ) ;
816
813
}
817
814
else
818
815
{
819
- if ( type is not null )
816
+ var list = new List < JsonSchemaType > ( ) ;
817
+ foreach ( JsonSchemaType flag in jsonSchemaTypeValues )
820
818
{
821
- var list = new List < JsonSchemaType ? > ( ) ;
822
- foreach ( JsonSchemaType flag in System . Enum . GetValues ( typeof ( JsonSchemaType ) ) )
819
+ if ( type . Value . HasFlag ( flag ) )
823
820
{
824
- if ( type . Value . HasFlag ( flag ) )
825
- {
826
- list . Add ( flag ) ;
827
- }
821
+ list . Add ( flag ) ;
828
822
}
823
+ }
829
824
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 ( ) ) ) ;
832
826
}
833
827
}
834
828
}
835
829
836
- private static int CountEnumSetFlags ( JsonSchemaType ? schemaType )
830
+ private static bool IsPowerOfTwo ( int x )
837
831
{
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
+ }
852
834
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 ;
854
840
}
855
841
856
842
private void UpCastSchemaTypeToV31 ( JsonSchemaType ? type , IOpenApiWriter writer )
857
843
{
858
844
// create a new array and insert the type and "null" as values
859
845
Type = type | JsonSchemaType . Null ;
860
846
var list = new List < string > ( ) ;
861
- foreach ( JsonSchemaType ? flag in System . Enum . GetValues ( typeof ( JsonSchemaType ) ) )
847
+ foreach ( JsonSchemaType ? flag in jsonSchemaTypeValues )
862
848
{
863
849
// Check if the flag is set in 'type' using a bitwise AND operation
864
850
if ( Type . Value . HasFlag ( flag ) )
@@ -870,7 +856,9 @@ private void UpCastSchemaTypeToV31(JsonSchemaType? type, IOpenApiWriter writer)
870
856
writer . WriteOptionalCollection ( OpenApiConstants . Type , list , ( w , s ) => w . WriteValue ( s ) ) ;
871
857
}
872
858
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 )
874
862
{
875
863
/* If the array has one non-null value, emit Type as string
876
864
* If the array has one null value, emit x-nullable as true
@@ -882,23 +870,12 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite
882
870
? OpenApiConstants . NullableExtension
883
871
: OpenApiConstants . Nullable ;
884
872
885
- if ( flagsCount is 1 )
873
+ if ( ! HasMultipleTypes ( schemaType ^ JsonSchemaType . Null ) && ( schemaType & JsonSchemaType . Null ) == JsonSchemaType . Null ) // checks for two values and one is null
886
874
{
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 )
899
876
{
900
877
// 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 )
902
879
{
903
880
// Write the non-null flag value to the writer
904
881
writer . WriteProperty ( OpenApiConstants . Type , flag . ToIdentifier ( ) ) ;
@@ -909,6 +886,17 @@ private void DowncastTypeArrayToV2OrV3(JsonSchemaType? schemaType, IOpenApiWrite
909
886
writer . WriteProperty ( nullableProp , true ) ;
910
887
}
911
888
}
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
+ }
912
900
}
913
901
}
914
902
}
0 commit comments