@@ -111,19 +111,12 @@ public string? ExclusiveMinimum
111111 /// <inheritdoc />
112112 public JsonSchemaType ? Type { get ; set ; }
113113
114- /// <summary>
115- /// This property is internal and is only used to mark the "nullable" state when doing deserialization.
116- /// It's only set during deserialization to flag whether we encountered "nullable" or not.
117- /// And it's read:
118- /// 1. During FinalizeDeserialization to determine whether the Type should be changed to include null or not.
119- /// 2. When serializing nullable, if IsNullable was false and IsNullableFromDeserialization was true, we will still serialize "nullable".
120- /// </summary>
121- internal bool IsNullableFromDeserialization { get ; set ; }
114+ internal bool Nullable { get ; set ; }
122115
123- // x-nullable is filtered out by deserializers, but keep the check here in case it gets added from user code.
124- internal bool IsNullable
125- => HasTrueNullableExtension || ( Type . HasValue && Type . Value . HasFlag ( JsonSchemaType . Null ) ) ;
116+ private bool NullableOrHasNullType
117+ => Nullable || ( Type . HasValue && Type . Value . HasFlag ( JsonSchemaType . Null ) ) ;
126118
119+ // x-nullable is filtered out by deserializers, but keep the check here in case it gets added from user code.
127120 private bool HasTrueNullableExtension
128121 => Extensions is not null &&
129122 Extensions . TryGetValue ( OpenApiConstants . NullableExtension , out var nullExtRawValue ) &&
@@ -802,16 +795,13 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
802795
803796 internal void FinalizeDeserialization ( OpenApiSpecVersion version )
804797 {
805- if ( version is OpenApiSpecVersion . OpenApi2_0 )
798+ if ( version is OpenApiSpecVersion . OpenApi2_0 && HasTrueNullableExtension )
806799 {
807- if ( HasTrueNullableExtension )
808- {
809- Extensions ! . Remove ( OpenApiConstants . NullableExtension ) ;
810- IsNullableFromDeserialization = true ;
811- }
800+ Extensions ! . Remove ( OpenApiConstants . NullableExtension ) ;
801+ Nullable = true ;
812802 }
813803
814- if ( IsNullableFromDeserialization && Type is not null && Type != 0 )
804+ if ( Nullable && Type is not null && Type != 0 )
815805 {
816806 Type |= JsonSchemaType . Null ;
817807 }
@@ -1031,7 +1021,7 @@ private bool TrySerializeTypeProperty(IOpenApiWriter writer, OpenApiSpecVersion
10311021 }
10321022 break ;
10331023 default :
1034- WriteUnifiedSchemaType ( IsNullable ? typeToUse . Value | JsonSchemaType . Null : typeToUse . Value , writer ) ;
1024+ WriteUnifiedSchemaType ( typeToUse . Value , writer ) ;
10351025 return true ;
10361026 }
10371027
@@ -1072,7 +1062,7 @@ where type.HasFlag(flag)
10721062
10731063 private void SerializeNullable ( IOpenApiWriter writer , OpenApiSpecVersion version , bool hasNullInComposition = false )
10741064 {
1075- if ( IsNullable || IsNullableFromDeserialization || hasNullInComposition )
1065+ if ( NullableOrHasNullType || hasNullInComposition )
10761066 {
10771067 switch ( version )
10781068 {
0 commit comments