-
-
Notifications
You must be signed in to change notification settings - Fork 228
fix: _get_kwargs bool headers are now converted to strings #553
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
fix: _get_kwargs bool headers are now converted to strings #553
Conversation
I didn't fully grasp how to integrate my openapi spec to the existing one. |
Thanks for providing the fix! I've been pondering it since you opened the PR and I'm not sure what the correct path forward is. Providing "True" and "False" as header values is very pythonic and is likely not what general APIs are expecting when they ask for a bool in a header. More likely they want the JSON-ish "true" and "false" or "1" and "0" (as I've seen in some implementations). There is no guidance in the spec as far as I can see. I've seen conflicting suggestions in other web resources (like the Mozilla developer's guide) suggesting that "?1" and "?0" are the correct values for booleans in headers.. but I've never seen a server implementation that knows how to represent those. My inclination is to just make it a generation error to have a boolean anywhere in a header so as to not cause obscure runtime errors when people try to use them. Technically nothing but strings should be supported, but OpenAPI does provide guidance on transforming objects, arrays, and numbers into strings based on the spec. I don't think this generator will transform all of that properly today though. |
I looked at the json specification. One tags of the project is fastapi, which generated this openapi spec. We shouldn't make our quest for perfection prevent a partial solution. |
Closed by mistake, my hand tapped on close by accident |
I generated using .net 6 minimal API another OpenApiSpec that had a Boolean header. using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.MapGet("/", () => "Hello World!");
app.MapPost("/boolHead", ([FromHeader(Name = "bool-header")] bool header) => header);
app.UseSwaggerUI();
app.Run(); |
Boolean headers are allowed by OpenAPI (and indeed FastAPI), unfortunately, there's just no guidance in the OpenAPI spec about how to send them. This is far from the first time we've had to decide on how to narrow the spec for this project because the spec was not specific enough, just always feels bad to do it.
Indeed, I'm just always cognizant of the fact that breaking changes are major problems down the line. So once we commit to a solution here, it's going to be hard to change course. It would be nice to pick the most broadly accepted solution so we don't regret it later.
Since there is no universal guidance on passing types other than strings to headers, falling back to "how JSON does it" seems like a safe bet. Can you verify that lowercase "true" and "false" are also accepted by those frameworks? Header values are generally considered case-sensitive, so I'd rather stick to all lower case as it might help future compatibility. |
I added the lowercasing to the parameters and tested it on fastapi and .net core 6 |
Codecov Report
@@ Coverage Diff @@
## main #553 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 48 48
Lines 1661 1661
=========================================
Hits 1661 1661
Continue to review full report at Codecov.
|
Thanks for all the work on this @John98Zakaria! I'm going to take it over in #556 because I feel like having each type express how it can be put in a header is more maintainable long-term than the one special case for bools. Gonna be a much larger change 😅 but better to do it now than come back later I think. |
Closes #552. BREAKING CHANGE: Header values will be explicitly transformed or omitted instead of blindly passed to httpx as-is. Co-authored-by: John Sorial <[email protected]> Co-authored-by: dbanty <[email protected]>
Closes #552. BREAKING CHANGE: Header values will be explicitly transformed or omitted instead of blindly passed to httpx as-is. Co-authored-by: John Sorial <[email protected]> Co-authored-by: dbanty <[email protected]>
Closes #552. BREAKING CHANGE: Header values will be explicitly transformed or omitted instead of blindly passed to httpx as-is. Co-authored-by: John Sorial <[email protected]> Co-authored-by: dbanty <[email protected]>
fixes : #552