Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 284
Fix handling of nullable enums for 3.0#2920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Vincent Biret (baywet)
merged 4 commits into
microsoft:main
from
Youssef1313:nullable-enumJul 3, 2026
+124
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -521,6 +521,7 @@ | ||
| IList<IOpenApiSchema>? effectiveOneOf = OneOf; | ||
| IList<IOpenApiSchema>? effectiveAnyOf = AnyOf; | ||
| bool hasNullInComposition = false; | ||
| bool hasOneOfNullAndSingleEnumWith3_0 = false; | ||
| JsonSchemaType? inferredType = null; | ||
| if (version == OpenApiSpecVersion.OpenApi3_0) | ||
| @@ -531,6 +532,9 @@ | ||
| (effectiveAnyOf, var inferredAnyOf, var nullInAnyOf) = ProcessCompositionForNull(AnyOf); | ||
| hasNullInComposition |= nullInAnyOf; | ||
| inferredType = inferredAnyOf ?? inferredType; | ||
| hasOneOfNullAndSingleEnumWith3_0 = nullInOneOf && effectiveOneOf is { Count: 1 } && | ||
| effectiveOneOf[0].Enum is { Count: > 0 }; | ||
| } | ||
| // type | ||
| @@ -543,7 +547,27 @@ | ||
| writer.WriteOptionalCollection(OpenApiConstants.AnyOf, effectiveAnyOf, callback); | ||
| // oneOf | ||
| writer.WriteOptionalCollection(OpenApiConstants.OneOf, effectiveOneOf, callback); | ||
| if (hasOneOfNullAndSingleEnumWith3_0) | ||
| { | ||
| writer.WriteRequiredCollection(OpenApiConstants.OneOf, effectiveOneOf!, (writer, element) => | ||
| { | ||
| var clonedToMutateEnum = element.CreateShallowCopy(); | ||
| if (clonedToMutateEnum is OpenApiSchema { Enum: { } existingEnum } concreteCloned) | ||
| { | ||
| concreteCloned.Enum = [.. existingEnum, JsonNullSentinel.JsonNull]; | ||
| callback(writer, clonedToMutateEnum); | ||
| } | ||
| else | ||
| { | ||
| callback(writer, element); | ||
| } | ||
| }); | ||
| } | ||
| else | ||
| { | ||
| writer.WriteOptionalCollection(OpenApiConstants.OneOf, effectiveOneOf, callback); | ||
| } | ||
| // not | ||
| writer.WriteOptionalObject(OpenApiConstants.Not, Not, callback); | ||
| @@ -1070,10 +1094,32 @@ | ||
| foreach (var schema in nonNullSchemas) | ||
| { | ||
| commonType |= schema.Type.GetValueOrDefault() & ~JsonSchemaType.Null; | ||
| if (schema.Type.HasValue) | ||
| { | ||
| commonType |= schema.Type.Value & ~JsonSchemaType.Null; | ||
| } | ||
| else if (schema.Enum is { Count: > 0 }) | ||
| { | ||
| foreach (var enumValue in schema.Enum.Where(x => x is not null)) | ||
| { | ||
| var currentType = enumValue.GetValueKind() switch | ||
| { | ||
| JsonValueKind.Array => JsonSchemaType.Array, | ||
| JsonValueKind.String => JsonSchemaType.String, | ||
| JsonValueKind.Number => JsonSchemaType.Number, | ||
| JsonValueKind.True or JsonValueKind.False => JsonSchemaType.Boolean, | ||
| JsonValueKind.Null => (JsonSchemaType)0, | ||
| _ => JsonSchemaType.Object, | ||
| }; | ||
| commonType |= currentType; | ||
| } | ||
baywet marked this conversation as resolved.
Dismissed
Uh oh!There was an error while loading. Please reload this page. | ||
| commonType |= JsonSchemaType.String; | ||
| } | ||
Youssef1313 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return (nonNullSchemas, commonType, true); | ||
| return (nonNullSchemas, commonType == 0 ? null : commonType, true); | ||
| } | ||
| else | ||
| { | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,7 +5,10 @@ | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.IO; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using System.Text.Json.Schema; | ||
| using System.Text.Json.Serialization; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using VerifyXunit; | ||
| @@ -1853,6 +1856,78 @@ public void DeserializeContainsExtensionsInV3AssignsContainsProperties() | ||
| Assert.True(schema.Extensions is null || !schema.Extensions.ContainsKey(OpenApiConstants.MinContainsExtension)); | ||
| } | ||
| [Fact] | ||
| public async Task SerializeNullableEnumWith3_0() | ||
| { | ||
| // https://spec.openapis.org/oas/v3.0.4.html#fixed-fields-20 | ||
| // Documentation for nullable states: | ||
| // This keyword only takes effect if type is explicitly defined within the same Schema Object. | ||
| // So, we want to ensure that we emit the type property if we will be adding nullable property. | ||
| // In addition, we need to still keep 'null' in the enum array. | ||
| // Otherwise, validators will consider null as invalid even if nullable is set to true. | ||
| // It's unclear if it's an issue of the validators or not, but it's safer to do it that way. | ||
| var schema = CreateNullableEnumSchema(); | ||
| var result = await schema.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_0); | ||
| var expected = """ | ||
| { | ||
| "type": "string", | ||
| "oneOf": [ | ||
| { | ||
| "enum": [ | ||
| "A", | ||
| "B", | ||
| null | ||
| ] | ||
baywet marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| ], | ||
| "nullable": true | ||
| } | ||
| """; | ||
| Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(result))); | ||
| } | ||
| [Theory] | ||
| [InlineData(OpenApiSpecVersion.OpenApi3_1)] | ||
| [InlineData(OpenApiSpecVersion.OpenApi3_2)] | ||
| public async Task SerializeNullableEnumWith3_1_And_Later(OpenApiSpecVersion version) | ||
| { | ||
| var schema = CreateNullableEnumSchema(); | ||
| var result = await schema.SerializeAsJsonAsync(version); | ||
| var expected = """ | ||
| { | ||
| "oneOf": [ | ||
| { | ||
| "type": "null" | ||
| }, | ||
| { | ||
| "enum": [ | ||
| "A", | ||
| "B" | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| """; | ||
| Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(result))); | ||
| } | ||
| private OpenApiSchema CreateNullableEnumSchema() | ||
| { | ||
| var schema = new OpenApiSchema(); | ||
| schema.OneOf ??= []; | ||
| schema.OneOf.Add(new OpenApiSchema() { Type = JsonSchemaType.Null }); | ||
| schema.OneOf.Add(new OpenApiSchema() | ||
| { | ||
| Enum = new List<JsonNode> | ||
| { | ||
| JsonValue.Create("A"), | ||
| JsonValue.Create("B") | ||
| } | ||
| }); | ||
| return schema; | ||
| } | ||
| internal class SchemaVisitor : OpenApiVisitorBase | ||
| { | ||
| public List<string> Titles = new(); | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.