@@ -487,14 +487,7 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
487487            writer . WriteOptionalCollection ( OpenApiConstants . Enum ,  Enum ,  ( nodeWriter ,  s )  =>  nodeWriter . WriteAny ( s ) ) ; 
488488
489489            // type 
490-             if  ( Type ? . GetType ( )  ==  typeof ( string ) ) 
491-             { 
492-                 writer . WriteProperty ( OpenApiConstants . Type ,  ( string ) Type ) ; 
493-             } 
494-             else 
495-             { 
496-                 writer . WriteOptionalCollection ( OpenApiConstants . Type ,  ( string [ ] ) Type ,  ( w ,  s )  =>  w . WriteRaw ( s ) ) ; 
497-             } 
490+             SerializeTypeProperty ( Type ,  writer ,  version ) ; 
498491
499492            // allOf 
500493            writer . WriteOptionalCollection ( OpenApiConstants . AllOf ,  AllOf ,  ( w ,  s )  =>  s . SerializeAsV3 ( w ) ) ; 
@@ -537,7 +530,10 @@ public void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version,
537530            writer . WriteOptionalObject ( OpenApiConstants . Default ,  Default ,  ( w ,  d )  =>  w . WriteAny ( d ) ) ; 
538531
539532            // nullable 
540-             writer . WriteProperty ( OpenApiConstants . Nullable ,  Nullable ,  false ) ; 
533+             if  ( version  is  OpenApiSpecVersion . OpenApi3_0 ) 
534+             { 
535+                 writer . WriteProperty ( OpenApiConstants . Nullable ,  Nullable ,  false ) ; 
536+             } 
541537
542538            // discriminator 
543539            writer . WriteOptionalObject ( OpenApiConstants . Discriminator ,  Discriminator ,  ( w ,  s )  =>  s . SerializeAsV3 ( w ) ) ; 
@@ -674,7 +670,14 @@ internal void SerializeAsV2(
674670            writer . WriteStartObject ( ) ; 
675671
676672            // type 
677-             writer . WriteProperty ( OpenApiConstants . Type ,  ( string ) Type ) ; 
673+             if  ( Type  is  string [ ]  array ) 
674+             { 
675+                 DowncastTypeArrayToV2OrV3 ( array ,  writer ,  OpenApiSpecVersion . OpenApi2_0 ) ; 
676+             } 
677+             else  
678+             { 
679+                 writer . WriteProperty ( OpenApiConstants . Type ,  ( string ) Type ) ; 
680+             } 
678681
679682            // description 
680683            writer . WriteProperty ( OpenApiConstants . Description ,  Description ) ; 
@@ -803,6 +806,35 @@ internal void SerializeAsV2(
803806            writer . WriteEndObject ( ) ; 
804807        } 
805808
809+         private  void  SerializeTypeProperty ( object  type ,  IOpenApiWriter  writer ,  OpenApiSpecVersion  version ) 
810+         { 
811+             if  ( type ? . GetType ( )  ==  typeof ( string ) ) 
812+             { 
813+                 // check whether nullable is true for upcasting purposes 
814+                 if  ( Nullable  ||  Extensions . ContainsKey ( OpenApiConstants . NullableExtension ) ) 
815+                 { 
816+                     // create a new array and insert the type and "null" as values 
817+                     Type  =  new [ ]  {  ( string ) Type ,  OpenApiConstants . Null  } ; 
818+                 } 
819+                 else 
820+                 { 
821+                     writer . WriteProperty ( OpenApiConstants . Type ,  ( string ) Type ) ; 
822+                 } 
823+             } 
824+             if  ( Type  is  string [ ]  array ) 
825+             { 
826+                 // type 
827+                 if  ( version  is  OpenApiSpecVersion . OpenApi3_0 ) 
828+                 { 
829+                     DowncastTypeArrayToV2OrV3 ( array ,  writer ,  OpenApiSpecVersion . OpenApi3_0 ) ; 
830+                 } 
831+                 else 
832+                 { 
833+                     writer . WriteOptionalCollection ( OpenApiConstants . Type ,  ( string [ ] ) Type ,  ( w ,  s )  =>  w . WriteRaw ( s ) ) ; 
834+                 } 
835+             } 
836+         } 
837+ 
806838        private  object  DeepCloneType ( object  type ) 
807839        { 
808840            if  ( type  ==  null ) 
@@ -826,5 +858,38 @@ private object DeepCloneType(object type)
826858
827859            return  null ; 
828860        } 
861+ 
862+         private  void  DowncastTypeArrayToV2OrV3 ( string [ ]  array ,  IOpenApiWriter  writer ,  OpenApiSpecVersion  version ) 
863+         { 
864+             /* If the array has one non-null value, emit Type as string 
865+             * If the array has one null value, emit x-nullable as true 
866+             * If the array has two values, one null and one non-null, emit Type as string and x-nullable as true 
867+             * If the array has more than two values or two non-null values, do not emit type 
868+             * */ 
869+ 
870+             var  nullableProp  =  version . Equals ( OpenApiSpecVersion . OpenApi2_0 )  
871+                 ?  OpenApiConstants . NullableExtension 
872+                 :  OpenApiConstants . Nullable ; 
873+ 
874+             if  ( array . Length  is  1 ) 
875+             { 
876+                 var  value  =  array [ 0 ] ; 
877+                 if  ( value  is  OpenApiConstants . Null ) 
878+                 { 
879+                     writer . WriteProperty ( nullableProp ,  true ) ; 
880+                 } 
881+                 else 
882+                 { 
883+                     writer . WriteProperty ( OpenApiConstants . Type ,  value ) ; 
884+                 } 
885+             } 
886+             else  if  ( array . Length  is  2  &&  array . Contains ( OpenApiConstants . Null ) ) 
887+             { 
888+                 // Find the non-null value and write it out 
889+                 var  nonNullValue  =  array . First ( v =>  v  !=  OpenApiConstants . Null ) ; 
890+                 writer . WriteProperty ( OpenApiConstants . Type ,  nonNullValue ) ; 
891+                 writer . WriteProperty ( nullableProp ,  true ) ; 
892+             } 
893+         } 
829894    } 
830895} 
0 commit comments