Skip to content

Commit 4a87c0e

Browse files
authored
Mark StringContent mediaType parameters as nullable (#103442)
* Mark StringContent mediaType parameters as nullable * Update tests
1 parent 81e56ac commit 4a87c0e

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

src/libraries/System.Net.Http/ref/System.Net.Http.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,10 @@ protected override void SerializeToStream(System.IO.Stream stream, System.Net.Tr
476476
public partial class StringContent : System.Net.Http.ByteArrayContent
477477
{
478478
public StringContent(string content) : base (default(byte[])) { }
479-
public StringContent(string content, System.Net.Http.Headers.MediaTypeHeaderValue mediaType) : base (default(byte[])) { }
479+
public StringContent(string content, System.Net.Http.Headers.MediaTypeHeaderValue? mediaType) : base (default(byte[])) { }
480480
public StringContent(string content, System.Text.Encoding? encoding) : base (default(byte[])) { }
481-
public StringContent(string content, System.Text.Encoding? encoding, System.Net.Http.Headers.MediaTypeHeaderValue mediaType) : base (default(byte[])) { }
482-
public StringContent(string content, System.Text.Encoding? encoding, string mediaType) : base (default(byte[])) { }
481+
public StringContent(string content, System.Text.Encoding? encoding, System.Net.Http.Headers.MediaTypeHeaderValue? mediaType) : base (default(byte[])) { }
482+
public StringContent(string content, System.Text.Encoding? encoding, string? mediaType) : base (default(byte[])) { }
483483
protected override System.Threading.Tasks.Task SerializeToStreamAsync(System.IO.Stream stream, System.Net.TransportContext? context, System.Threading.CancellationToken cancellationToken) { throw null; }
484484
}
485485
}

src/libraries/System.Net.Http/src/System/Net/Http/StringContent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public StringContent(string content)
2727
/// <summary>Creates a new instance of the <see cref="StringContent"/> class.</summary>
2828
/// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
2929
/// <param name="mediaType">The media type to use for the content.</param>
30-
public StringContent(string content, MediaTypeHeaderValue mediaType)
30+
public StringContent(string content, MediaTypeHeaderValue? mediaType)
3131
: this(content, DefaultStringEncoding, mediaType)
3232
{
3333
}
@@ -45,7 +45,7 @@ public StringContent(string content, Encoding? encoding)
4545
/// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
4646
/// <param name="encoding">The encoding to use for the content.</param>
4747
/// <param name="mediaType">The media type to use for the content.</param>
48-
public StringContent(string content, Encoding? encoding, string mediaType)
48+
public StringContent(string content, Encoding? encoding, string? mediaType)
4949
: base(GetContentByteArray(content, encoding))
5050
{
5151
Debug.Assert(DefaultStringEncoding.WebName == "utf-8");
@@ -77,7 +77,7 @@ public StringContent(string content, Encoding? encoding, string mediaType)
7777
/// <param name="content">The content used to initialize the <see cref="StringContent"/>.</param>
7878
/// <param name="encoding">The encoding to use for the content.</param>
7979
/// <param name="mediaType">The media type to use for the content.</param>
80-
public StringContent(string content, Encoding? encoding, MediaTypeHeaderValue mediaType)
80+
public StringContent(string content, Encoding? encoding, MediaTypeHeaderValue? mediaType)
8181
: base(GetContentByteArray(content, encoding))
8282
{
8383
Headers.ContentType = mediaType;

src/libraries/System.Net.Http/tests/FunctionalTests/HttpRequestMessageTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public async Task HttpRequest_StringContent_WithoutMediaType()
275275
await LoopbackServer.CreateServerAsync(async (server, uri) =>
276276
{
277277
var request = new HttpRequestMessage(HttpMethod.Post, uri);
278-
request.Content = new StringContent("", null, ((MediaTypeHeaderValue)null)!);
278+
request.Content = new StringContent("", null, (MediaTypeHeaderValue)null);
279279

280280
Task<HttpResponseMessage> requestTask = client.SendAsync(request);
281281
await server.AcceptConnectionAsync(async connection =>

src/libraries/System.Net.Http/tests/FunctionalTests/StringContentTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void Ctor_PassNullHeaderValueForMediaType_NoMediaTypeUsed()
131131
{
132132
string sourceString = "\u00C4\u00E4\u00FC\u00DC";
133133
Encoding defaultStringEncoding = Encoding.GetEncoding("utf-8");
134-
var content = new StringContent(sourceString, defaultStringEncoding, ((Headers.MediaTypeHeaderValue)null)!);
134+
var content = new StringContent(sourceString, defaultStringEncoding, (MediaTypeHeaderValue)null);
135135

136136
// If no media header value is passed-in, there is none
137137
Assert.Null(content.Headers.ContentType);

0 commit comments

Comments
 (0)