Skip to content

Bind inherited [AsParameters] properties in the Request Delegate Generator - #70854

Draft
mzakaryanst wants to merge 4 commits into
dotnet:mainfrom
mzakaryanst:fix/rdg-asparameters-inherited-properties
Draft

mzakaryanst wants to merge 4 commits into
dotnet:mainfrom
mzakaryanst:fix/rdg-asparameters-inherited-properties

Conversation

@mzakaryanst

@mzakaryanst mzakaryanst commented Nov 25, 2026 •

Copy link
Copy Markdown

Bind inherited [AsParameters] properties in the Request Delegate Generator

Description

Bug

With EnableRequestDelegateGenerator on, [AsParameters] binds only the properties that the parameter type declares itself. The generator never binds a property that a base class declares. That property keeps its default value. The runtime binder (RequestDelegateFactory) binds the same property. The generator reports no diagnostic.

For example, GET /items?Page=2&PageSize=5&Name=x returns Page=2 PageSize=5 Name=x with the runtime binder. It returns Page=0 PageSize=0 Name=x with the generator. The issue has the full repro.

A required property in a base class is worse: the generated object initializer does not set it, so the build fails with error CS9035.

Cause

TryGetAsParametersConstructor in EndpointParameter.cs collects the properties with type.GetMembers(). This call returns only the members that the type declares. The runtime binder uses Type.GetProperties(). That call also returns the public properties of the base types.

Fix

The new helper GetPublicProperties walks the type and its base types. It follows the rules of Type.GetProperties():

  • It includes the public properties that a base type declares.
  • If a derived type overrides a property or hides it with new, the helper uses only the most derived declaration.
  • A private property hides an inherited property only if the type itself declares it as an instance property. Reflection does the same.
  • It does not return static properties of base types, as reflection does not. It does not return indexers.

The object initializer path also skips static properties. An object initializer cannot set a static property or an indexer, and RequestDelegateFactory fails on both with InvalidProgramException. Before, a public static property with a setter caused error CS1914 in the generated code, and a writable indexer caused invalid code. A constructor parameter can still match a static property that the type declares, as before.

Constructor matching, the emitted assignments, and the emitted parameter metadata all read this one list. Thus they all use the same set of properties.

Two more changes keep the generator in line with the runtime binder now that base types are in the list:

  • The parameterless constructor path assigns a property only if it has a public setter. This is the same rule as GetSetMethod(nonPublic: false) != null in RequestDelegateFactory. A required property is also assigned, because the object initializer must set it. Before, a private or protected setter on a type from source caused error CS0272 in the generated code.
  • The constructor lookup table is built only for a parameterized constructor, as in ParameterBindingMethodCache.FindConstructor. Before, two properties whose names differ only by case (for example ID in a base class and Id in the derived class) made the generator throw.

This change also fixes a related case. Before, a positional record that passes a parameter to a positional base record got the RDG006 warning, and the generator did not intercept the endpoint. Now the generator matches the constructor parameter to the inherited property.

Behavior change

The public-setter rule also applies to properties that the [AsParameters] type declares itself. The generator used to assign these properties, and now it does not, as the runtime binder never did:

  • A property with an internal setter, or with a protected internal setter in the same assembly.
  • A getter-only override of a settable property.

A required property keeps its old behavior: the generator still assigns it.

Tests

The new tests in RequestDelegateCreationTests.AsParameters.cs run two times: one time with the generator (CompileTimeCreationTests) and one time with the runtime binder (RuntimeCreationTests). They cover these cases:

  • A property from a base class.
  • A property from a grandparent class.
  • A property that the derived class hides with new, and a property that the derived class overrides.
  • Inherited properties with [FromQuery(Name = ...)] and [FromHeader(Name = ...)].
  • Inherited properties that are get-only, that have a private, protected, protected internal or private protected setter, or that a getter-only override replaces. The binder does not assign them.
  • An inherited required property.
  • An inherited property whose name differs only by case from a property of the derived class.
  • A record constructor parameter that matches an inherited property.
  • A constructor parameter that matches a static property. This worked before, and the test keeps it working.

The tests also check the IParameterBindingMetadata of the inherited and hidden properties.

A metadata reference hides private and internal members from the generator. Thus one more test in CompileTimeCreationTests.AsParameters.cs declares a hierarchy in source. It covers private and internal setters, a required property with an internal setter, a static property, a writable indexer, a private base property, a private static property, and a private instance property of the derived type.

No baseline file changes.

Not changed

If a derived class hides a property with new and a different property type, both binders fail when the endpoint is built. The runtime binder adds the same parameter name two times. The generator emits a typeof(T).GetProperty(name) call that throws AmbiguousMatchException. That was true before this PR, and this PR does not change it.

Backport

Please consider a backport of this fix to release/10.0, the current LTS release. Apps that turn the generator on get wrong values for inherited [AsParameters] properties, and nothing warns them. An inherited required property breaks the build. The fix is small, and the shared generator and runtime-binder tests cover it. The servicing risk is the behavior change above: the generator stops assigning a property with an internal setter, or a getter-only override, which the runtime binder also does not assign.

Fixes #70852

…rator

The generator collected [AsParameters] properties with type.GetMembers().
That call returns only the members that the type declares. Properties from
a base class were never bound and kept their default values. The runtime
binder uses Type.GetProperties() and binds them.

Walk the base types in the same way as Type.GetProperties(). Include
inherited public properties. Use the most derived declaration when a
property is overridden or hidden. Skip static and private properties that
a base type declares. Also skip a property with a private or protected
setter, because the generated code cannot call that setter.

Fixes dotnet#70852
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Nov 25, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Assign only properties with a public setter, which is the rule that
RequestDelegateFactory uses. Do not return static properties or indexers,
because an object initializer cannot set them. Do not let a private
property of a base type hide a property of a type above it.

Build the constructor lookup table only for a parameterized constructor,
as ParameterBindingMethodCache.FindConstructor does. Two inherited
properties whose names differ only by case no longer make the generator
throw.

Add tests for these cases. One compile-time test declares the hierarchy in
source, because the generator cannot see private members of the shared
test types from metadata.
@mzakaryanst

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Assign a required property even when its setter is not public, because
the object initializer must set it. Without this, a required property with
an internal setter caused error CS9035 in the generated code.

Let a private static property not hide an inherited instance property,
as reflection does. Rename the helper to GetPublicInstanceProperties,
remove the System.Object check (object declares no properties), and
enumerate the base types only one time.

Update the tests: one assertion per value in the source-declared test, an
inherited required property, a private protected setter in the shared
test, and constants for the custom binding names.
Return the static properties that the type declares again, so that a
constructor parameter can match them as before and as
ParameterBindingMethodCache.FindConstructor allows. Only the object
initializer path skips static properties. Static properties of base types
are still not returned, as in reflection.

Build the property list with an ImmutableArray builder, use one null
check for the setter, and add a test for a constructor parameter that
matches a static property.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request Delegate Generator does not bind [AsParameters] properties declared in a base class

1 participant