Skip to content

Honor nullable collection elements in OpenAPI schemas - #68982

Open
snemeckayova wants to merge 4 commits into
dotnet:mainfrom
snemeckayova:snemeckayova/#68322-openapi-fix-nullability-componentized-type
Open

Honor nullable collection elements in OpenAPI schemas#68982
snemeckayova wants to merge 4 commits into
dotnet:mainfrom
snemeckayova:snemeckayova/#68322-openapi-fix-nullability-componentized-type

Conversation

@snemeckayova

Copy link
Copy Markdown
Contributor

Description

Updates OpenAPI schema generation to preserve nullable annotations for collection elements and dictionary values.
Nullable inline element schemas include null in their type. Nullable componentized elements use a nullable oneOf wrapper around the component reference. The same behavior is applied to model properties, dictionary values, and array response elements, including asynchronous endpoint return types.

Fixes #68322

@Youssef1313 Youssef1313 added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Sep 2, 2026
@snemeckayova
snemeckayova marked this pull request as ready for review September 3, 2026 11:39
Copilot AI lite review requested due to automatic review settings September 3, 2026 11:39

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.

🟢 Approval recommended

The changes directly address the described nullability loss, are consistently applied across properties/items/additionalProperties, and are backed by targeted tests plus updated integration snapshots.

Pull request overview

This PR updates ASP.NET Core’s OpenAPI schema generation to correctly preserve C# nullable annotations for collection element schemas (arrays/lists) and dictionary value schemas, including when those element/value schemas are componentized (moved into #/components/schemas/*). This aligns generated documents with expected OpenAPI nullability semantics and fixes the reported regression in #68322.

Changes:

  • Propagates “nullable element/value” intent through schema generation via x-is-nullable-property metadata for componentized item/value schemas and resolves them into oneOf nullable wrappers.
  • Adds response-specific handling so nullable array element return types (including async returns) produce nullable items schemas.
  • Extends test coverage and updates integration snapshots across OpenAPI 3.0/3.1/3.2 outputs.
File summaries
File Description
src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs Ensures nullable wrappers are applied when resolving references for properties, array items, and dictionary additionalProperties.
src/OpenApi/src/Services/OpenApiDocumentService.cs Applies nullable-item handling for response schemas when the action return type’s array element is nullable.
src/OpenApi/src/Extensions/TypeExtensions.cs Adds response array-element nullability detection using NullabilityInfoContext and refactors response nullability logic into a shared helper.
src/OpenApi/src/Extensions/JsonNodeSchemaExtensions.cs Applies nullable annotations to collection element schemas and dictionary value schemas during JsonNode-based schema generation, including componentized cases via metadata.
src/OpenApi/test/.../OpenApiSchemaService.ResponseSchemas.cs Adds a focused test for nullable componentized array elements in response schemas.
src/OpenApi/test/.../OpenApiSchemaService.PropertySchemas.cs Adds a focused test for nullable componentized collection elements in request model property schemas.
src/OpenApi/test/.../snapshots/*.verified.txt Updates expected OpenAPI document snapshots to reflect preserved element/value nullability across versions.
Review details
  • Files reviewed: 10/10 changed files
  • Comments generated: 0
  • Review effort level: Lite

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI: nullability is dropped for collection elements whose type is componentized

3 participants