Closed
Description
This is a follow-up on #81506 (comment). We introduced some inconsistencies and unintentional nullability changes in StringContent
's constructors. Chronologically:
- Add StringContent ctor providing tighter control over charset #17036 proposed new constructors before the introduction of nullable references to C#
- Annotate System.Net.Http for nullable #33268 changed all
mediaType
args to be nullable - We approving the new constructors, the comment with the fianal API suggestion did not takke into account the nullability changes implemented in Annotate System.Net.Http for nullable #33268, falsely "recommending" that nullability should be "reverted". It also proposed the argument
MediaTypeHeaderValue mediaType
to be non-nullable, which is inconsistent with the then-existing API-s. - First try at implementing newly suggested ctor for providing tighter control over charset #63231 implemented the proposal in this form, and also altered existing
mediaType
args to be non-nullable- On top of that, the new constructors taking
MediaTypeHeaderValue mediaType
don't do null-check on that arg, simply allowing the property to be set to null
- On top of that, the new constructors taking
#81722 provides a minimal fix, however, since this was an unintentional change, I suggest to revisit it for .NET 8.0, relaxing nullability for all mediaType
declarations, explicitly setting text/plain
when null is being passed. This should be fairly trivial to implement.
public class StringContent : ByteArrayContent
{
- public StringContent(string content, MediaTypeHeaderValue mediaType);
+ public StringContent(string content, MediaTypeHeaderValue? mediaType);
- public StringContent(string content, Encoding? encoding, string mediaType);
+ public StringContent(string content, Encoding? encoding, string? mediaType);
- public StringContent(string content, Encoding? encoding, MediaTypeHeaderValue mediaType);
+ public StringContent(string content, Encoding? encoding, MediaTypeHeaderValue? mediaType);
}