Skip to content

Commit 193b6ca

Browse files
committed
Fix the media type issue with .NET 7 #1969
1 parent a63d57f commit 193b6ca

File tree

6 files changed

+17
-11
lines changed

6 files changed

+17
-11
lines changed

src/RestSharp/Parameters/Parameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract record Parameter(string? Name, object? Value, ParameterType Type
2121
/// <summary>
2222
/// MIME content type of the parameter
2323
/// </summary>
24-
public string? ContentType { get; protected init; }
24+
public string ContentType { get; protected init; } = "text/plain";
2525

2626
/// <summary>
2727
/// Return a human-readable representation of this parameter

src/RestSharp/Request/BodyExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace RestSharp;
1717

18+
using System.Diagnostics.CodeAnalysis;
19+
1820
static class BodyExtensions {
1921
public static bool TryGetBodyParameter(this RestRequest request, out BodyParameter? bodyParameter) {
2022
bodyParameter = request.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody) as BodyParameter;
@@ -23,5 +25,5 @@ public static bool TryGetBodyParameter(this RestRequest request, out BodyParamet
2325

2426
public static bool HasFiles(this RestRequest request) => request.Files.Count > 0;
2527

26-
public static bool IsEmpty(this ParametersCollection? parameters) => parameters == null || parameters.Count == 0;
27-
}
28+
public static bool IsEmpty([NotNullWhen(false)]this ParametersCollection? parameters) => parameters == null || parameters.Count == 0;
29+
}

src/RestSharp/Request/RequestContent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ void AddPostParameters(ParametersCollection? postParameters) {
151151

152152
if (Content is MultipartFormDataContent mpContent) {
153153
// we got the multipart form already instantiated, just add parameters to it
154-
foreach (var postParameter in postParameters!) {
154+
foreach (var postParameter in postParameters) {
155155
var parameterName = postParameter.Name!;
156156

157157
mpContent.Add(
158-
new StringContent(postParameter.Value!.ToString()!, _client.Options.Encoding, postParameter.ContentType),
158+
new StringContent(postParameter.Value?.ToString() ?? "", _client.Options.Encoding, postParameter.ContentType),
159159
_request.MultipartFormQuoteParameters ? $"\"{parameterName}\"" : parameterName
160160
);
161161
}

src/RestSharp/RestSharp.csproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup Condition="$(TargetFramework) != 'net6.0' And $(TargetFramework) != 'net7.0'">
3-
<PackageReference Include="System.Text.Json" Version="5.0.1" />
3+
<PackageReference Include="System.Text.Json" Version="5.0.1"/>
44
</ItemGroup>
55
<ItemGroup>
6-
<None Remove="RestSharp.csproj.DotSettings" />
6+
<None Remove="RestSharp.csproj.DotSettings"/>
77
</ItemGroup>
88
<ItemGroup Condition="'$(TargetFramework)' == 'net471'">
9-
<Reference Include="System.Net.Http" />
10-
<Reference Include="System.Web" />
9+
<Reference Include="System.Net.Http"/>
10+
<Reference Include="System.Web"/>
11+
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All"/>
12+
</ItemGroup>
13+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
14+
<PackageReference Include="Nullable" Version="1.3.1" PrivateAssets="All"/>
1115
</ItemGroup>
1216
</Project>

test/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<IsTestProject>true</IsTestProject>
55
<IsPackable>false</IsPackable>
6-
<TargetFrameworks>net472;net6.0</TargetFrameworks>
6+
<TargetFrameworks>net472;net6.0;net7.0</TargetFrameworks>
77
<Nullable>disable</Nullable>
88
</PropertyGroup>
99

test/RestSharp.Tests.Integrated/RestSharp.Tests.Integrated.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Nullable>disable</Nullable>
4-
<TargetFrameworks>net6</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
55
</PropertyGroup>
66
<ItemGroup>
77
<ProjectReference Include="..\..\src\RestSharp.Serializers.Xml\RestSharp.Serializers.Xml.csproj"/>

0 commit comments

Comments
 (0)