Skip to content

Fix OpenAPI nullability for ignored setters - #68454

Merged
PureWeen merged 1 commit into
dotnet:mainfrom
PureWeen:pureween-prove-openapi-nullability-fix
Aug 12, 2026
Merged

PureWeen merged 1 commit into
dotnet:mainfrom
PureWeen:pureween-prove-openapi-nullability-fix

Conversation

@PureWeen

Copy link
Copy Markdown
Member

What this fixes

This is a follow-up to #68116 and the System.Text.Json behavior tracked by dotnet/runtime#131602.

PR #68116 fixed nullable get-only and constructor-bound properties, but it left one componentized-schema case inconsistent. Consider a non-nullable property with an [AllowNull] private setter:

private Todo? _value;

[AllowNull]
public Todo Value
{
    get => _value ?? new Todo(0, string.Empty, false, default);
    private set => _value = value;
}

System.Text.Json ignores that private setter unless [JsonInclude] is present, so JsonPropertyInfo.Set is null. Reflection still sees [AllowNull] and reports a nullable write state. The componentized OpenAPI path trusted the reflection result and generated oneOf: [null, $ref], even though the effective JSON contract cannot write null through that setter.

What changed

Nullable read state is still honored unconditionally. That preserves nullable get-only and constructor-bound properties.

Nullable write state is now honored only when System.Text.Json exposes an effective setter:

return nullabilityInfo.ReadState == NullabilityState.Nullable ||
    (jsonPropertyInfo.Set is not null && nullabilityInfo.WriteState == NullabilityState.Nullable);

The tests cover all three setter behaviors that matter here:

  • an ignored private setter stays non-nullable
  • a public setter with [AllowNull] stays nullable
  • a private setter with [JsonInclude] stays nullable

How this was verified

This came out of a historical regression review against the exact merged head of #68116.

  • The trimmed regression suite was red on the frozen PR head: 2 passed, 1 failed. Only the ignored-private-setter case failed.
  • The same suite passed 3/3 with this change.
  • An 18-case constructor/accessor matrix across OpenAPI 3.0, 3.1, and 3.2 went from 15/18 on the frozen code to 18/18 with this change.
  • The focused GetOpenApiSchema_ subset passes 22/22 on this branch.
  • The full Microsoft.AspNetCore.OpenApi.Tests project passes with 1,028 passed, 5 skipped, and 0 failed.

The matrix also confirmed that setter-less constructor-bound properties report WriteState == Unknown. Their nullable behavior comes from ReadState, so this gate does not remove constructor-bound nullability.

Only honor nullable write-state when System.Text.Json exposes an effective setter, while preserving nullable read-state for constructor-bound properties.

Add regression coverage for ignored private setters and controls for public and JsonInclude setters.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: e9878319-42ec-4220-b30b-55fb3b251e4e
Copilot AI lite review requested due to automatic review settings August 12, 2026 13:49
@PureWeen
PureWeen requested a review from a team as a code owner August 12, 2026 13:49

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

This PR updates OpenAPI schema nullability inference for componentized schemas to avoid treating [AllowNull] as nullable when System.Text.Json ignores the setter (i.e., JsonPropertyInfo.Set is null). It aligns the componentized-schema path with the effective JSON contract exposed by System.Text.Json, while still honoring nullable read state for get-only and constructor-bound properties.

Changes:

  • Refine ShouldApplyNullablePropertySchema to honor nullable write state only when System.Text.Json exposes an effective setter (jsonPropertyInfo.Set is not null), while still honoring nullable read state unconditionally.
  • Add targeted regression tests covering: ignored private setter, public setter, and [JsonInclude] private setter behaviors.
Show a summary per file
File Description
src/OpenApi/src/Extensions/TypeExtensions.cs Gates nullable write-state handling on presence of an effective STJ setter to prevent incorrect null in componentized schemas.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.PropertySchemas.cs Adds regression tests to validate nullability behavior across the three relevant setter scenarios.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment on lines +732 to +735
private static JsonPropertyInfo GetValueProperty<TModel>()
=> Assert.Single(
JsonSerializerOptions.Default.GetTypeInfo(typeof(TModel)).Properties,
propertyInfo => propertyInfo.Name == "Value");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don’t think nameof is a good fit for this helper. It’s generic across three unrelated model types, so nameof(TModel.Value) doesn’t compile, and naming one concrete model would only protect that model. Also, JsonPropertyInfo.Name is the effective JSON name, while nameof returns the CLR member name and wouldn’t track [JsonPropertyName] or a naming policy. If the name stops matching, Assert.Single will fail loudly, so I’d prefer to keep this as-is.

@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.

We should eventually just use jsonPropertyInfo.Is[Get|Set]Nullable when dotnet/runtime#131602 is fixed.

@PureWeen
PureWeen merged commit d04c380 into dotnet:main Aug 12, 2026
27 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants