Skip to content

Commit 94b606d

Browse files
Copilotbaywet
andauthored
fix(reader): preserve nullable Null flag when type appears after nullable in V3.1/V3.2 deserializers
Agent-Logs-Url: https://github.com/microsoft/OpenAPI.NET/sessions/40596ef1-e9f2-4d1b-b9e2-0650b96d73b4 Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
1 parent b75709a commit 94b606d

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,17 @@ internal static partial class OpenApiV31Deserializer
198198
"type",
199199
(o, n, doc) =>
200200
{
201+
// Preserve any Null flag set by a preceding "nullable: true" handler
202+
var preserveNull = o.Type.HasValue && o.Type.Value.HasFlag(JsonSchemaType.Null);
201203
if (n is ValueNode)
202204
{
203-
o.Type = n.GetScalarValue()?.ToJsonSchemaType();
205+
var parsedType = n.GetScalarValue()?.ToJsonSchemaType();
206+
o.Type = preserveNull ? parsedType | JsonSchemaType.Null : parsedType;
204207
}
205208
else
206209
{
207210
var list = n.CreateSimpleList((n2, p) => n2.GetScalarValue(), doc);
208-
JsonSchemaType combinedType = 0;
211+
JsonSchemaType combinedType = preserveNull ? JsonSchemaType.Null : 0;
209212
foreach(var type in list.Where(static t => t is not null).Select(static t => t!.ToJsonSchemaType()))
210213
{
211214
combinedType |= type;

src/Microsoft.OpenApi/Reader/V32/OpenApiSchemaDeserializer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,17 @@ internal static partial class OpenApiV32Deserializer
198198
"type",
199199
(o, n, doc) =>
200200
{
201+
// Preserve any Null flag set by a preceding "nullable: true" handler
202+
var preserveNull = o.Type.HasValue && o.Type.Value.HasFlag(JsonSchemaType.Null);
201203
if (n is ValueNode)
202204
{
203-
o.Type = n.GetScalarValue()?.ToJsonSchemaType();
205+
var parsedType = n.GetScalarValue()?.ToJsonSchemaType();
206+
o.Type = preserveNull ? parsedType | JsonSchemaType.Null : parsedType;
204207
}
205208
else
206209
{
207210
var list = n.CreateSimpleList((n2, p) => n2.GetScalarValue(), doc);
208-
JsonSchemaType combinedType = 0;
211+
JsonSchemaType combinedType = preserveNull ? JsonSchemaType.Null : 0;
209212
foreach(var type in list.Where(static t => t is not null).Select(static t => t!.ToJsonSchemaType()))
210213
{
211214
combinedType |= type;

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ public void ParseSchemaWithTypeArrayWorks()
132132
Assert.Equivalent(expected, actual);
133133
}
134134

135+
[Theory]
136+
[InlineData(@"{ ""nullable"": true, ""type"": ""string"" }")]
137+
[InlineData(@"{ ""type"": ""string"", ""nullable"": true }")]
138+
public void ParseSchemaWithNullableBeforeOrAfterTypePreservesNullFlag(string schemaJson)
139+
{
140+
// Act
141+
var schema = OpenApiModelFactory.Parse<OpenApiSchema>(schemaJson, OpenApiSpecVersion.OpenApi3_1, new(), out _, "json", SettingsFixture.ReaderSettings);
142+
143+
// Assert
144+
Assert.Equal(JsonSchemaType.String | JsonSchemaType.Null, schema.Type);
145+
}
146+
135147
[Fact]
136148
public void TestSchemaCopyConstructorWithTypeArrayWorks()
137149
{

0 commit comments

Comments
 (0)