-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Unexpected breaking change in .NET 7 #81506
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionThe StringContent constructor:
has changed behaviour from .NET6 to .NET7. We had a particular code-path that ended up passing The fix for this would be changing StringContent (lines 47-48) from:
to:
Reproduction StepsCreate a unit test project, targeting both .NET6 and 7:
And add the following test:
From the CLI run Expected behaviorThe behaviour on .NET6 and 7 Actual behaviorThe .NET7 version throws an exception:
Regression?Yes, works in .NET6 Known WorkaroundsCall the constructor that only takes Configuration
Other informationNo response
|
Sorry, forgot to link to the .NET 6 version of the code: MediaTypeHeaderValue headerValue = new MediaTypeHeaderValue((mediaType == null) ? DefaultMediaType : mediaType); Can see here that it used to check for |
Looks like it got changed in #63231 |
It seems like nullability of "mediaType" has changed to disallow nulls. Do you use NRT? Compiler would certainly issue a warning here. |
Indeed, unfortunately we haven't enabled NRT in this project. It's a future goal, but is not a quick thing for an older, large, code-base. I'm not saying what we are doing is correct, but it does feel odd that the behaviour changed and also that the two overrides: StringContent(string content, Encoding encoding)
StringContent(string content, Encoding encoding, string mediaType) don't behave the same way if mediaType == null - the first one will default mediaType to "text/plain" but the other no longer defaults. Was just an unexpected change is all - it may have been by-design, but it wasn't included in the breaking changes list as far as I could see. Partly raised the issue so other people that google it may find a quick answer. |
This seems like a bug. @dotnet/ncl, anyone know why that changed? |
Feels like an oversight in #63231
should be -: this(content, encoding, new MediaTypeHeaderValue(mediaType, (encoding ?? DefaultStringEncoding).WebName))
+: this(content, encoding, new MediaTypeHeaderValue(mediaType ?? DefaultMediaType, (encoding ?? DefaultStringEncoding).WebName)) to match previous behavior. |
That PR also changed the nullability of StringContent(string content, Encoding? encoding, string mediaType) to be non-nullable. I don't see any reasoning as to why that happened - I don't see it discussed in the API suggestion issue. |
@MihaZupan yes, exactly that fix is what I propose. Thanks 🙏 (Please say if you want a pr, though figured it was quicker for someone who already has access to do) |
Given I don't see any discussion to the contrary on the issue/PR, I'm going to assume it was not intentional. |
Is there a larger issue here? We currently allow passing a null
(even though the parameter isn't attributed as nullable). That would seem to mean you get subtly different behavior if you pass a null string mediaType vs a null MediaTypeHeaderValue mediaType? |
I've created a PR (#81722). Unfortunately it turns out I am not smart* enough to actually get the framework libraries running locally 😵💫 , so although I added a unit test I don't actually know if it passes... it really should though! (I'll try it on linux at some point to see if I have any more luck on there). I created the PR against the main branch, but wasn't sure if it should really have been against the .NET7 branch? Please let me know if so and I can update it. @stephentoub Yes, you are correct in that if you pass var sc = new StringContent("", Encoding.Default, (MediaTypeHeaderValue)null);
Assert.AreEqual("plain/text", sc.Headers.ContentType); However, that override was not available in .NET6, so it isn't a breaking change. Agree that makes the API behaviour unexpected though. *EDIT actually did managed to get the unit tests running in the end and it does pass. |
Triage: Looks like 3 more people ran into it ... that would make a solid case for backport as it is a regression. |
@karelz Yes, I have encountered this problem, but after a long time, I can't remember how I circumvented this problem in the first place. |
@karelz Can confirm that we are encountering this exact issue in my organization in the upgrade to .NET 7 from .NET 6. |
…ringContent #81506 (#83425) Co-authored-by: Simon Lovely <[email protected]>
Fixed for 7.0.x servicing in #83425 |
This breaks my library RestClient.Net The tests pass for 3.1 , 5, 6 but fail for 7. You can see the failing test here: |
@antonfirsov @karelz is there a workaround for this at the moment? |
You can workaround the issue by passing |
@MihaZupan, thanks, but that is not working in my case. I content type is text/plain but I still get the error |
@MihaZupan, I think I figured out the issue. I'm using a HTTP mocking framework. The issue is not on the request. It's the same issue with the response. I forced the mock to return a media type and now it seems to be working. Thanks. |
@MelbourneDeveloper is the response also covered by the same PR, or would need a new one? |
@Meligy, I'm not sure. I haven't dug into any of this yet. |
This bug manifests itself in popular libs like Octokit octokit/octokit.net#2659 |
@mcintyre321 I believe this fix is scheduled to be in release 7.0.6 (see #83425). The latest version is currently 7.0.5. |
Description
The StringContent constructor:
has changed behaviour from .NET6 to .NET7. We had a particular code-path that ended up passing
null
into this constructor for the mediaType, which worked as expected in .NET6 and set the mime type to "text/plain". In .NET7 the same call throwsArgumentException
.The fix for this would be changing StringContent (lines 47-48) from:
to:
(https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/StringContent.cs#L47)
Reproduction Steps
Create a unit test project, targeting both .NET6 and 7:
And add the following test:
From the CLI run
dotnet test
. The unit test passes for .NET6, but throws an error in .NET7:Expected behavior
The behaviour on .NET6 and 7
Actual behavior
The .NET7 version throws an exception:
Regression?
Yes, works in .NET6
Known Workarounds
Call the constructor that only takes
string content, Encoding encoding
parameters.Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: