-
-
Notifications
You must be signed in to change notification settings - Fork 248
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
Provide typed Access to Header-Fields #616
Comments
this seems to work. tested with a sample content-disposition value var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddFastEndpoints()
.SwaggerDocument();
var app = bld.Build();
app.UseFastEndpoints(
c => c.Binding.ValueParserFor<ContentDispositionHeaderValue>(
input =>
{
if (ContentDispositionHeaderValue.TryParse(new((StringValues)input), out var res))
return new(true, res);
return new(false, null);
}))
.UseSwaggerGen();
app.Run();
sealed class MyRequest
{
public int Id { get; set; }
[FromHeader("Content-Disposition")]
public ContentDispositionHeaderValue Disposition { get; set; }
}
sealed class MyEndpoint : Endpoint<MyRequest>
{
public override void Configure()
{
Post("test");
AllowAnonymous();
}
public override async Task HandleAsync(MyRequest r, CancellationToken c)
{
await SendAsync(r.Disposition.FileName.Value);
}
} someone's gonna have to write "value parsers" for all of the typed request header types. that ain't gonna be me though 😜 so the requirement is to write a static class with a static property of type also gonna need unit tests for each of the typed header types 😁 |
I think I'll try to implement those converters during the next days... Let's see how it turns out :-) |
i figured out how to bake in the support for parsing typed headers. so no need to manually write parsers for every header type. as long as header types have a consistent public static bool TryParse(StringSegment input, out XXXXXXXXX? parsedValue) just working out the final kinks. will publish a beta for you to test out soon. |
check out using System.Net.Http.Headers; // THIS IS IMPORTANT. DON'T USE Microsoft.Net.Http.Headers
var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddFastEndpoints()
.SwaggerDocument();
var app = bld.Build();
app.UseFastEndpoints()
.UseSwaggerGen();
app.Run();
sealed class MyRequest
{
[FromHeader("Content-Disposition")]
public ContentDispositionHeaderValue Disposition { get; set; }
}
sealed class MyEndpoint : Endpoint<MyRequest>
{
public override void Configure()
{
Post("test");
AllowAnonymous();
}
public override async Task HandleAsync(MyRequest r, CancellationToken c)
{
await SendAsync(r.Disposition.FileName);
}
} |
Wow, that was fast, thanks… I’ll try it as soon as possible… |
initially i made it work with Microsoft.Net.Http.Headers. then i compared it with System.Net.Http.Headers and chose to only support
and please, additional test cases would be much appreciated 🙏🙏🙏 |
Hey I just did some tests today... Unfortunately I was right regarding the list style headers... If I try to do a call using multiple Regarding
Long story short, I'd still prefer to use |
i have no idea how to support lists of typed headers though. other than having to register a json converter for each list type. |
Well it should be rel. straightforward for the
|
i think i got it with FastEndpoints/Src/Library/Binder/BinderExtensions.cs Lines 228 to 240 in 55f4f81
it works with |
Wo, that was fast, thanks! I was just about to finish a generic implementation... But your looks much more straightforward :-) I'll test that one. |
if they had used a common interface for the tryparse methods, the above could have been much simpler :-( |
You can check out my work-in-progress implementation here: drothmaler@bac2a2b |
that looks alright at first glance. does it fully work with all possible header types? didn't wanna go down that route myself because of complicating the expression tree building logic. the downside to my method would be that we need to update it when MS adds new header types. |
Doing some tests right now. Looks good so far, but doesn't make the Binder any prettier, as you said... |
great! let me know how it goes... |
In .NET there is a way to access Request Headers in a typed way, using
Request.GetTypedHeaders()
and theRequestHeaders
wrapper class.It would be really nice to have something similar in FastEndpoints. Especially for those properties, that are actually weighted lists, instead of simple values (like the
Accept
header - to name one prominent example).I know that I could still access the typed headers using
Endpoint.HttpContext.Request.GetTypedHeaders()
, but that feels a bit like working around the framework.I'm not sure how to implement that in a idiomatic way. The quick & dirty solution would probably be to provide a Json converter for
IList<MediaTypeHeaderValue>
,IList<StringWithQualityHeaderValue>
,ContentRangeHeaderValue
and alle the other helper classes, as they are used in theRequestHeaders
class. That way, it would be possible to use similar declarations in custom Request classes.The problem with this, is that it still wouldn't be actually type-safe. Another variant could be to provide some kind of Request base class, that declared all the headers as protected properties. To make it visible in the API-Spec I would than have to override it and annotate it with
[FromHeader]
. But I didn't yet, think that through to be honest. So maybe someone can come up with a better idea.The text was updated successfully, but these errors were encountered: