Repository navigation
Bind inherited [AsParameters] properties in the Request Delegate Generator - #70854
Draft
mzakaryanst wants to merge 4 commits into
Draft
mzakaryanst wants to merge 4 commits into
mzakaryanst wants to merge 4 commits into
Conversation
…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
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bind inherited
[AsParameters]properties in the Request Delegate GeneratorDescription
Bug
With
EnableRequestDelegateGeneratoron,[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=xreturnsPage=2 PageSize=5 Name=xwith the runtime binder. It returnsPage=0 PageSize=0 Name=xwith the generator. The issue has the full repro.A
requiredproperty in a base class is worse: the generated object initializer does not set it, so the build fails with error CS9035.Cause
TryGetAsParametersConstructorinEndpointParameter.cscollects the properties withtype.GetMembers(). This call returns only the members that the type declares. The runtime binder usesType.GetProperties(). That call also returns the public properties of the base types.Fix
The new helper
GetPublicPropertieswalks the type and its base types. It follows the rules ofType.GetProperties():new, the helper uses only the most derived declaration.The object initializer path also skips static properties. An object initializer cannot set a static property or an indexer, and
RequestDelegateFactoryfails on both withInvalidProgramException. 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:
GetSetMethod(nonPublic: false) != nullinRequestDelegateFactory. Arequiredproperty 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.ParameterBindingMethodCache.FindConstructor. Before, two properties whose names differ only by case (for exampleIDin a base class andIdin 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:internalsetter, or with aprotected internalsetter in the same assembly.A
requiredproperty keeps its old behavior: the generator still assigns it.Tests
The new tests in
RequestDelegateCreationTests.AsParameters.csrun two times: one time with the generator (CompileTimeCreationTests) and one time with the runtime binder (RuntimeCreationTests). They cover these cases:new, and a property that the derived class overrides.[FromQuery(Name = ...)]and[FromHeader(Name = ...)].requiredproperty.The tests also check the
IParameterBindingMetadataof the inherited and hidden properties.A metadata reference hides private and internal members from the generator. Thus one more test in
CompileTimeCreationTests.AsParameters.csdeclares a hierarchy in source. It covers private and internal setters, arequiredproperty 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
newand 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 atypeof(T).GetProperty(name)call that throwsAmbiguousMatchException. 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 inheritedrequiredproperty 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 aninternalsetter, or a getter-only override, which the runtime binder also does not assign.Fixes #70852