Repository navigation
Fix OpenAPI nullability for ignored setters - #68454
Conversation
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
There was a problem hiding this comment.
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
ShouldApplyNullablePropertySchemato 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
| private static JsonPropertyInfo GetValueProperty<TModel>() | ||
| => Assert.Single( | ||
| JsonSerializerOptions.Default.GetTypeInfo(typeof(TModel)).Properties, | ||
| propertyInfo => propertyInfo.Name == "Value"); |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
We should eventually just use jsonPropertyInfo.Is[Get|Set]Nullable when dotnet/runtime#131602 is fixed.
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:System.Text.Json ignores that private setter unless
[JsonInclude]is present, soJsonPropertyInfo.Setisnull. Reflection still sees[AllowNull]and reports a nullable write state. The componentized OpenAPI path trusted the reflection result and generatedoneOf: [null, $ref], even though the effective JSON contract cannot writenullthrough 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:
The tests cover all three setter behaviors that matter here:
[AllowNull]stays nullable[JsonInclude]stays nullableHow this was verified
This came out of a historical regression review against the exact merged head of #68116.
GetOpenApiSchema_subset passes 22/22 on this branch.Microsoft.AspNetCore.OpenApi.Testsproject 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 fromReadState, so this gate does not remove constructor-bound nullability.