Skip to content

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Nov 17, 2025

Backport of #63872 to release/10.0

/cc @captainsafia @desjoerd

OpenAPI: Fix ModelMetadata null reference exception in emitted XMLComment transformers

Description

OpenAPI XML comment support walks ParameterDescriptions and previously assumed that parameterDescription.ModelMetadata was always non-null. For controller route parameters (including those coming from [Route] on the controller or inherited controllers), ModelMetadata can be null, which caused a NullReferenceException in the generated XML comment operation transformer while trying to inspect metadata.MetadataKind.

This change:

  • Updates the emitted transformer code to guard against null before checking MetadataKind and other members:
var metadata = parameterDescription.ModelMetadata;
if (metadata is not null
    && metadata.MetadataKind == ModelMetadataKind.Property
    && metadata.ContainerType is { } containerType
    && metadata.PropertyName is { } propertyName)
{
    // existing logic to look up XML comments
}
  • Regenerates the corresponding source-generator snapshots so the emitted code matches the new null-check.
  • Adds a new controller-based test (SupportsRouteParametersFromControllers) that verifies:
    • A controller route parameter defined via [Route("[controller]")] and [HttpGet("{userId}")] does not throw during OpenAPI document generation.
    • The XML <summary> on the route parameter is correctly applied to the OpenAPI parameter description in the generated document.

Fixes #64185

Customer Impact

Without this fix, apps that:

  • Use controllers with route parameters supplied via [Route] on the controller (common in versioned APIs), and
  • Enable XML documentation-based OpenAPI support via AddOpenApi,

will see OpenAPI document generation fail with a NullReferenceException when hitting their OpenAPI endpoint or UI. This prevents customers from using the built-in OpenAPI + XML comments experience in affected controller scenarios and can break development and diagnostics workflows that depend on the generated spec.

With this fix:

  • OpenAPI document generation no longer throws when ModelMetadata is null for a parameter.
  • XML comments are still applied where metadata is available.
  • There are no behavior changes for parameters that already had non-null ModelMetadata.

Regression?

  • Yes
  • No

This is a bug in the new XML documentation–backed OpenAPI support introduced in .NET 10; there wasn’t a previously working version of this feature for this specific scenario.

Risk

  • High
  • Medium
  • Low

The change is limited to:

  • Adding a null check before accessing ModelMetadata in the generated XML comment transformers.
  • Updating source-generator snapshots and adding a focused test.

If ModelMetadata is non-null, behavior is unchanged. When it is null (previously a hard crash), the transformer now simply skips property-based XML comment lookup for that parameter, which is a safe and expected fallback. The change is localized to OpenAPI XML comment handling and does not affect other MVC or OpenAPI paths.

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

…ment transformers

When a route parameter does not have a bound model it would case a null reference exception because the ModelMetadata on the ParameterDescriptor was null.
This is fixed by adding a null reference check

Fixes #63757
As I think this was the encoding before my change
@github-actions github-actions bot requested review from a team and captainsafia as code owners November 17, 2025 18:52
@captainsafia captainsafia changed the title [release/10.0] OpenAPI: Fix ModelMetadata null reference exception in emitted XMLComment transformers [release/10.0] Fix ModelMetadata null reference exception in emitted XMLComment transformers Nov 17, 2025
@captainsafia captainsafia added the Servicing-consider Shiproom approval is required for the issue label Nov 17, 2025
@rbhanda rbhanda added this to the 10.0.1 milestone Nov 17, 2025
@captainsafia
Copy link
Member

Approved via email.

@captainsafia captainsafia added Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Nov 17, 2025
@wtgodbe wtgodbe merged commit c5ec3bf into release/10.0 Nov 17, 2025
28 checks passed
@wtgodbe wtgodbe deleted the backport/pr-63872-to-release/10.0 branch November 17, 2025 22:40
@dotnet-policy-service dotnet-policy-service bot modified the milestone: 10.0.1 Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants