Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[CLEAN] Synthetic Benchmark PR #64401 - [release/10.0] Fix ModelMetadata null reference exception in emitted XMLComment transformers#91

Open
tomerqodo wants to merge 4 commits intobase_pr_64401_20260121_8909from
clean_pr_64401_20260121_8909
Open

[CLEAN] Synthetic Benchmark PR #64401 - [release/10.0] Fix ModelMetadata null reference exception in emitted XMLComment transformers#91
tomerqodo wants to merge 4 commits intobase_pr_64401_20260121_8909from
clean_pr_64401_20260121_8909

Conversation

@tomerqodo
Copy link

Benchmark PR dotnet#64401

Type: Clean (correct implementation)

Original PR Title: [release/10.0] Fix ModelMetadata null reference exception in emitted XMLComment transformers
Original PR Description: Backport of dotnet#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 dotnet#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?

…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 dotnet#63757
As I think this was the encoding before my change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants