Repository navigation
Fix nullable get-only/constructor-bound property schema generation in OpenAPI - #68116
Conversation
There was a problem hiding this comment.
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.ReadStatein addition toWriteState(fixes get-only cases whereWriteStateisUnknown). - Adjust schema nullability application logic to avoid incorrectly treating certain read-only non-nullable properties as nullable, and to prune
nullwhen 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) | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Seems like this is the case from my understanding, will add the comment there
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