-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Handle parameters in Accepts mediatypes in DefaultProblemDetailsWriter #54158
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
Handle parameters in Accepts mediatypes in DefaultProblemDetailsWriter #54158
Conversation
_problemDetailsJsonMediaType.IsSubsetOf(acceptHeaderValue)) | ||
//One of the media types needs to be checked if it's a subset of the accepted header value | ||
//Application/json is a subset of */* but */* is not a subset of application/json | ||
if(acceptHeaderValue.IsSubsetOf(_jsonMediaType) || acceptHeaderValue.IsSubsetOf(_problemDetailsJsonMediaType) || _jsonMediaType.IsSubsetOf(acceptHeaderValue)) |
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.
Is there a reason you left the third clause (_jsonMediaType.IsSubsetOf(acceptHeaderValue)
) in this check?
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.
Yes. If the accepted media header is a superset of jsonMediaHeader it will return false when it should return true. With the absence of the check: the test for case */* will fail.
@@ -600,6 +600,8 @@ await writer.WriteAsync(new ProblemDetailsContext() | |||
[InlineData("application/*")] |
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.
Are we meant to be able to write this at all then? cc @captainsafia
Seems like this shouldn't work.
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.
According to the RFC, it looks like applicaton/*
is a content-type that we should match against.
The asterisk "" character is used to group media types into ranges, with "/" indicating all media types and "type/" indicating all subtypes of that type. The media-range can include media type parameters that are applicable to that range.
I believe some of the weirdness here might be related to the way that comparisons happen for parameters in the IsSubsetOf extension method. Previously, it was checking to see if all the parameters defined in acceptsHeaderValue
appeared in _jsonMediaType
. Since _jsonMediaType
has no parameters defined, this would always return false, hence the inversion eeded.
We need to retain the _jsonMediaType.IsSubsetOf(acceptHeaderValue)
because MediaTypeHeaderValue
handles comparisons for subtypes differently (it requires the argument provided to be more permissive).
TL;DR: I think the issue here is the confusing IsSubsetOf
APIs on MediaTypeHeaderValue
.
Thanks @MythoclastBM! I left some comments. Apart from some minor style stuff, I'm curious why you left an extra clause in the check that was there before. I assumed you'd just flip the two existing ones. |
@@ -600,6 +600,8 @@ await writer.WriteAsync(new ProblemDetailsContext() | |||
[InlineData("application/*")] | |||
[InlineData("application/json")] | |||
[InlineData("application/problem+json")] | |||
[InlineData("application/json; charset=utf-8")] | |||
[InlineData("application/json; v=1.0")] | |||
public void CanWrite_ReturnsTrue_WhenJsonAccepted(string contentType) |
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.
To cover our bases, can we add a check for */*
as well?
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.
Already do.
Co-authored-by: Safia Abdalla <[email protected]>
Will we see this change in a patch release of .Net 8? Or do we have to wait for .Net 9? |
@tore-hammervoll This change isn't being back ported. We typically only backport things if there is a big enough demand for it. If this is blocking you, let me know. |
We got around most cases of this issue by ensuring we write the problems with aspnetcore/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs Line 194 in aefa4ca
The only case I've discovered that falls back to the |
I see. If this becomes a serious problem, you can file an issue on the repo and we can make a decision around backporting with that specific context in mind. |
Address #52577
Fixed issue where DefaultProblemDetailWriter doesn't handle media type subsets. Added test cases including media type subsets. The corresponds with Issue#52577 tagged help wanted.