@@ -550,7 +550,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
550550 }
551551
552552 // type
553- var serializedTypeProperty = TrySerializeTypePropertyForVersion3AndLater ( writer , version , callback ) ;
553+ SerializeTypePropertyForVersion3AndLater ( writer , version , callback ) ;
554554
555555 // allOf
556556 writer . WriteOptionalCollection ( OpenApiConstants . AllOf , AllOf , callback ) ;
@@ -595,13 +595,13 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
595595 writer . WriteOptionalObject ( OpenApiConstants . Default , Default , ( w , d ) => w . WriteAny ( d ) ) ;
596596
597597 // nullable
598- if ( version == OpenApiSpecVersion . OpenApi3_0 && serializedTypeProperty )
598+ if ( version == OpenApiSpecVersion . OpenApi3_0 )
599599 {
600600 // https://spec.openapis.org/oas/v3.0.4.html#fixed-fields-20
601601 // This keyword only takes effect if type is explicitly defined within the same Schema Object.
602602 //
603- // If the user explicitly set IsNullable to true, we serialize it even if redundant .
604- // But if **we** are inferring it (from oneOf/anyOf), we don't serialize it when it's redundant .
603+ // We don't care to avoid an unnecessary serialization .
604+ // So, we attempt to serialize it regardless of whether or not a type property was serialized .
605605 SerializeNullable ( writer , version ) ;
606606 }
607607
@@ -838,7 +838,7 @@ private void SerializeAsV2(
838838 writer . WriteStartObject ( ) ;
839839
840840 // type
841- TrySerializeTypePropertyForVersion2 ( writer ) ;
841+ SerializeTypePropertyForVersion2 ( writer ) ;
842842
843843 // description
844844 writer . WriteProperty ( OpenApiConstants . Description , Description ) ;
@@ -1006,21 +1006,18 @@ private void SerializeAsV2(
10061006 writer . WriteEndObject ( ) ;
10071007 }
10081008
1009- private bool TrySerializeTypePropertyForVersion2 ( IOpenApiWriter writer )
1009+ private void SerializeTypePropertyForVersion2 ( IOpenApiWriter writer )
10101010 {
1011- if ( Type is not { } type )
1011+ if ( Type is not { } type || type == JsonSchemaType . Null )
10121012 {
1013- return false ;
1013+ return ;
10141014 }
10151015
10161016 var typeWithoutNull = type & ~ JsonSchemaType . Null ;
1017- if ( typeWithoutNull != 0 && ! HasMultipleTypes ( typeWithoutNull ) )
1017+ if ( ! HasMultipleTypes ( typeWithoutNull ) )
10181018 {
10191019 writer . WriteProperty ( OpenApiConstants . Type , typeWithoutNull . ToFirstIdentifier ( ) ) ;
1020- return true ;
10211020 }
1022-
1023- return false ;
10241021 }
10251022
10261023 /// <summary>
@@ -1030,18 +1027,18 @@ private bool TrySerializeTypePropertyForVersion2(IOpenApiWriter writer)
10301027 /// true if the Type was serializable using "type" property, and false if
10311028 /// it serialized using anyOf/oneOf or if it couldn't be serialized at all.
10321029 /// </returns>
1033- private bool TrySerializeTypePropertyForVersion3AndLater ( IOpenApiWriter writer , OpenApiSpecVersion version , Action < IOpenApiWriter , IOpenApiSerializable > callback )
1030+ private void SerializeTypePropertyForVersion3AndLater ( IOpenApiWriter writer , OpenApiSpecVersion version , Action < IOpenApiWriter , IOpenApiSerializable > callback )
10341031 {
10351032 if ( Type is not { } type )
10361033 {
1037- return false ;
1034+ return ;
10381035 }
10391036
10401037 if ( version == OpenApiSpecVersion . OpenApi3_0 )
10411038 {
10421039 if ( type == JsonSchemaType . Null )
10431040 {
1044- return false ;
1041+ return ;
10451042 }
10461043
10471044 var typeWithoutNull = type & ~ JsonSchemaType . Null ;
@@ -1061,18 +1058,18 @@ where typeWithoutNull.HasFlag(flag)
10611058 if ( canWriteAsAnyOf )
10621059 {
10631060 writer . WriteOptionalCollection ( OpenApiConstants . AnyOf , ConstructChildSchemasForTypes ( arrayWithoutNull , hasNull ) , callback ) ;
1064- return false ;
1061+ return ;
10651062 }
10661063 else if ( canWriteAsOneOf )
10671064 {
10681065 writer . WriteOptionalCollection ( OpenApiConstants . OneOf , ConstructChildSchemasForTypes ( arrayWithoutNull , hasNull ) , callback ) ;
1069- return false ;
1066+ return ;
10701067 }
10711068 }
10721069 else
10731070 {
10741071 writer . WriteProperty ( OpenApiConstants . Type , arrayWithoutNull [ 0 ] . ToSingleIdentifier ( ) ) ;
1075- return true ;
1072+ return ;
10761073 }
10771074 }
10781075 else
@@ -1089,12 +1086,8 @@ where type.HasFlag(flag)
10891086 {
10901087 writer . WriteProperty ( OpenApiConstants . Type , array [ 0 ] . ToSingleIdentifier ( ) ) ;
10911088 }
1092-
1093- return true ;
10941089 }
10951090
1096- return false ;
1097-
10981091 static OpenApiSchema [ ] ConstructChildSchemasForTypes ( JsonSchemaType [ ] types , bool hasNull )
10991092 {
11001093 var schemas = new OpenApiSchema [ types . Length + ( hasNull ? 1 : 0 ) ] ;
0 commit comments