Skip to content

Commit 8b3a5fe

Browse files
committed
fix(library): restore nullable schema serialization
Ensure OpenAPI 3.0 schema serialization emits nullable when the model type includes null, even when the type keyword itself is omitted. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 74ec039d-885c-4591-9364-e3edf28be242
1 parent 44375a6 commit 8b3a5fe

7 files changed

Lines changed: 9 additions & 4 deletions

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ where type.HasFlag(flag)
10301030

10311031
private void SerializeNullable(IOpenApiWriter writer, OpenApiSpecVersion version)
10321032
{
1033-
if (IsNullableCompatibility)
1033+
if (version == OpenApiSpecVersion.OpenApi3_0 ? HasNullType || IsNullableCompatibility : IsNullableCompatibility)
10341034
{
10351035
switch (version)
10361036
{

test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ public async Task SerializeAdvancedParameterAsV2JsonWorks()
270270
"name": "name1",
271271
"description": "description1",
272272
"required": true,
273-
"format": "double",
274273
"x-examples": {
275274
"test": {
276275
"summary": "summary3",

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.SerializeReferencedSchemaAsV3JsonWorksAsync_produceTerseOutput=False.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"multipleOf": 3,
44
"maximum": 42,
55
"default": 15,
6+
"nullable": true,
67
"externalDocs": {
78
"url": "http://example.com/externalDocs"
89
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"title":"title1","multipleOf":3,"maximum":42,"default":15,"externalDocs":{"url":"http://example.com/externalDocs"}}
1+
{"title":"title1","multipleOf":3,"maximum":42,"default":15,"nullable":true,"externalDocs":{"url":"http://example.com/externalDocs"}}

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.SerializeReferencedSchemaAsV3WithoutReferenceJsonWorksAsync_produceTerseOutput=False.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"multipleOf": 3,
44
"maximum": 42,
55
"default": 15,
6+
"nullable": true,
67
"externalDocs": {
78
"url": "http://example.com/externalDocs"
89
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"title":"title1","multipleOf":3,"maximum":42,"default":15,"externalDocs":{"url":"http://example.com/externalDocs"}}
1+
{"title":"title1","multipleOf":3,"maximum":42,"default":15,"nullable":true,"externalDocs":{"url":"http://example.com/externalDocs"}}

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ public async Task SerializeAdvancedSchemaNumberAsV3JsonWorks()
240240
"multipleOf": 3,
241241
"maximum": 42,
242242
"default": 15,
243+
"nullable": true,
243244
"externalDocs": {
244245
"url": "http://example.com/externalDocs"
245246
}
@@ -286,6 +287,7 @@ public async Task SerializeAdvancedSchemaObjectAsV3JsonWorks()
286287
"""
287288
{
288289
"title": "title1",
290+
"nullable": true,
289291
"properties": {
290292
"property1": {
291293
"properties": {
@@ -383,6 +385,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
383385
"""
384386
{
385387
"title": "title1",
388+
"nullable": true,
386389
"allOf": [
387390
{
388391
"title": "title2",
@@ -398,6 +401,7 @@ public async Task SerializeAdvancedSchemaWithAllOfAsV3JsonWorks()
398401
},
399402
{
400403
"title": "title3",
404+
"nullable": true,
401405
"properties": {
402406
"property3": {
403407
"properties": {

0 commit comments

Comments
 (0)