Skip to content

Fix nullable get-only/constructor-bound property schema generation in OpenAPI - #68116

Merged
Youssef1313 merged 6 commits into
dotnet:mainfrom
snemeckayova:dev/snemeckayova/openapi-nullability
Jul 31, 2026
Merged

Youssef1313 merged 6 commits into
dotnet:mainfrom
snemeckayova:dev/snemeckayova/openapi-nullability

Conversation

@snemeckayova

@snemeckayova snemeckayova commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

The IsNullable check in TypeExtensions previously only evaluated NullabilityInfo.WriteState, which is Unknown for get-only properties (no setter). This caused constructor-bound or get-only properties with nullable types (e.g., Todo? Value { get; }) to be incorrectly omitted from nullable schema generation in OpenAPI documents.

Fix: Also check ReadState alongside WriteState when determining nullability, so get-only nullable properties are correctly represented as nullable: true (or oneOf: [null, ...]) in the generated schema.

Changes:
• src/OpenApi/src/Extensions/TypeExtensions.cs — include ReadState in the nullable check
• Tests added for:
• Constructor-bound nullable property is correctly marked nullable in schema
• Non-nullable get-only property is not incorrectly marked nullable

Fixes #67233
Fixes #58192

@snemeckayova
snemeckayova requested a review from Youssef1313 July 30, 2026 15:27
@snemeckayova snemeckayova self-assigned this Jul 30, 2026
Copilot AI review requested due to automatic review settings July 30, 2026 15:27
@snemeckayova
snemeckayova requested a review from a team as a code owner July 30, 2026 15:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes OpenAPI schema nullability for immutable DTO patterns by correctly detecting nullability on get-only / constructor-bound properties, and adds coverage to prevent non-nullable get-only properties from being emitted as nullable.

Changes:

  • Update property nullability detection to consider NullabilityInfo.ReadState in addition to WriteState (fixes get-only cases where WriteState is Unknown).
  • Adjust schema nullability application logic to avoid incorrectly treating certain read-only non-nullable properties as nullable, and to prune null when it was added incorrectly.
  • Add tests covering constructor-bound nullable properties and non-nullable get-only properties.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/OpenApi/src/Extensions/TypeExtensions.cs Fix nullability detection for get-only properties by checking ReadState as well as WriteState.
src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs Refine nullability application/pruning for read-only properties during schema transformation.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.PropertySchemas.cs Add regression tests for nullable ctor-bound get-only properties and non-nullable get-only properties.

shouldApplyNullableSchema = false;
}

if (MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes)
@@ -556,16 +556,36 @@ private static bool IsNonAbstractTypeWithDerivedTypeReference(JsonSchemaExporter
/// <param name="propertyInfo">The <see cref="JsonPropertyInfo" /> associated with the schema.</param>
internal static void ApplyNullabilityContextInfo(this JsonNode schema, JsonPropertyInfo propertyInfo)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if the changes here are only caused by a bug on System.Text.Json side where it shouldn't have made that schema nullable in the first place 👀

I opened dotnet/runtime#131602 with a minimal STJ repro.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine merging this though, but please add some comments and link to the dotnet/runtime issue, so we can track simplifying this logic back once the dotnet/runtime issue gets fixed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this is the case from my understanding, will add the comment there

@Youssef1313 Youssef1313 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with small ask to add code comments. Please update PR title as well.

@snemeckayova snemeckayova changed the title Dev/snemeckayova/openapi nullability Fix nullable get-only/constructor-bound property schema generation in OpenAPI Jul 31, 2026
@Youssef1313
Youssef1313 enabled auto-merge (squash) July 31, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants