-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Fixing minimal api header array binding #55803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing minimal api header array binding #55803
Conversation
…reated correctly with tests
…om/MattyLeslie/aspnetcore into fixingMinimalApiHeaderArrayBinding
…n `SplitAndTrim(string[] values)`
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
@BrennanConroy, may I please request a review on this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does RequestDelegateGenerator support this already? Or does it need to be fixed as well?
Co-authored-by: Brennan <brecon@microsoft.com>
Co-authored-by: Brennan <brecon@microsoft.com>
} | ||
private static void EmitHeaderValueParsing(CodeWriter codeWriter) | ||
{ | ||
codeWriter.WriteLine("var headerParameters = handler.Method.GetParameters().Where(p => p.GetCustomAttribute<FromHeaderAttribute>() != null).ToList();"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When emitting code at compile-time, we want to do the discovery of parameters like this via static analysis.
You'll observe that in other parts of the Request Delegate Generator (RDG) code, we generate an EndpointParameter
object that we construct after statically analyzing the arguments then we use the information in that data model to emit the generated code. We don't need to re-compute things like the headerName dynamically like this.
You can find the pre-existing code where we emit the binding logic for header parameters here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review, I will be working on this.
@MattyLeslie Thanks for taking a stab at this! I posted some comments about the approach to take for this change in RDG based on the existing implementation. Let me know if you have questions about it. |
} | ||
|
||
var result = new List<string>(); | ||
Span<Range> parts = stackalloc Range[16]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem correct. If there are more than 16 values in a single header it will miss them. Maybe just revert to the previous code for now. We can look at improving the perf later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @BrennanConroy, I will revert that, I am going to mark this as draft as I'm still working on this.
@MattyLeslie Would you please let me know if you are going to work on this fix? |
Yeah if your PR works they're welcome to take it. I just haven't had the time I had when I started it. |
Ensuring Minimal APIs and controllers treat header arrays the same.
Summary:
Passing comma-separated list of values to the header was treated differently by controller action and Minimal API endpoint during binding to array. For controller action they were parsed as separate values while for Minimal APIs - as single one. This PR aimed to ensure they both were parsed as separate values by adapting the binding logic within
RequestDelegateFactory
Changes:
BindParameterFromExpression
now handles comma-separated values appropriately and returns them as separate values.RequestDelegateFactoryTests
to ensureRequestDelegate_ShouldHandleSingleHeaderValue()
RequestDelegateFactoryTests
to ensureRequestDelegate_ShouldHandleCommaSeparatedHeaderValues
Fixes #54978 by ensuring minimal API's and Controllers treat header arrays the same.