Skip to content

Fix handling of get-only non-nullable properties in OpenAPI schema generation - #66584

Closed
marcominerva wants to merge 2 commits into
dotnet:mainfrom
marcominerva:fix-non-nullable-get-only-properties
Closed

marcominerva wants to merge 2 commits into
dotnet:mainfrom
marcominerva:fix-non-nullable-get-only-properties

Conversation

@marcominerva

Copy link
Copy Markdown
Contributor

This pull request improves how nullable reference types are handled in OpenAPI schema generation, ensuring that get-only non-nullable properties are not incorrectly marked as nullable in the generated schemas. The changes include updates to the logic for determining property nullability and add new tests to verify the correct behavior.

Nullability handling improvements:

  • Updated ApplyNullabilityContextInfo in JsonNodeSchemaExtensions.cs to ensure that get-only non-nullable properties are not marked as nullable, and to only consider setter nullability if a setter exists. Also, removed the redundant check for typeof(object) in the componentization logic.
  • Modified ShouldApplyNullablePropertySchema in TypeExtensions.cs to use the property's read-state for get-only properties, ensuring correct nullability detection for properties without setters.

Testing enhancements:

  • Added a new test GetOpenApiSchema_GetOnlyNonNullableProperties_NotMarkedNullable in OpenApiSchemaService.PropertySchemas.cs to verify that get-only non-nullable properties are not marked as nullable, while get-only nullable properties are correctly marked. Introduced a supporting model class for the test.

Fixes #58192

@marcominerva
marcominerva requested a review from a team as a code owner May 5, 2026 09:32
Copilot AI review requested due to automatic review settings May 5, 2026 09:32
@github-actions github-actions Bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label May 5, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 5, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @marcominerva. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

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 ASP.NET Core's OpenAPI schema generation so nullable reference type handling better reflects get-only properties, addressing incorrect nullable output in generated schemas.

Changes:

  • Adjusts property nullability inference in JsonNodeSchemaExtensions to stop treating get-only non-nullable properties as nullable.
  • Changes ShouldApplyNullablePropertySchema to prefer read-state for get-only properties.
  • Adds a regression test covering get-only primitive/collection properties in request-body schema generation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/OpenApi/src/Extensions/TypeExtensions.cs Updates nullable-property detection logic for property schemas.
src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs Refines schema nullability application and null-type cleanup for properties.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.PropertySchemas.cs Adds a regression test for get-only property nullability behavior.

@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels May 5, 2026

@mikekistler mikekistler left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good! 👍

Thanks for this fix.

@marcominerva

Copy link
Copy Markdown
Contributor Author

Hi @mikekistler! Since this PR has been approved, do you think it can be merged?

@mikekistler
mikekistler requested a review from Youssef1313 June 23, 2026 13:46
@Youssef1313

Copy link
Copy Markdown
Member

Hi @marcominerva

Sorry that we missed this PR for long. The linked issue is now closed and seems to be fixed by a different PR. Hence, I'm closing this PR.

Sorry again that we haven't observed your PR earlier.

@Youssef1313 Youssef1313 closed this Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member feature-openapi

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Microsoft.AspNetCore.OpenApi specifies non-nullable get-only properties as nullable

5 participants