Skip to content

Commit

Permalink
Proposed fix for #1765 (#1782)
Browse files Browse the repository at this point in the history
* Fix for #1765
  • Loading branch information
alexeyzimarev committed Mar 13, 2022
1 parent 5550efb commit 17a3532
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
6 changes: 1 addition & 5 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<MinVerSkip>true</MinVerSkip>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="MinVer" Version="2.5.0" PrivateAssets="All"/>
<PackageReference Include="MinVer" Version="3.0.0" PrivateAssets="All"/>
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/RestSharp/Request/RequestContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void AddPostParameters(ParametersCollection? postParameters) {
var formContent = new FormUrlEncodedContent(
_request.Parameters
.Where(x => x.Type == ParameterType.GetOrPost)
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))!
);
Content = formContent;
}
Expand Down
29 changes: 21 additions & 8 deletions src/RestSharp/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public partial class RestClient : IDisposable {

HttpClient HttpClient { get; }

internal RestClientOptions Options { get; }
public RestClientOptions Options { get; }

public RestClient(RestClientOptions options, Action<HttpRequestHeaders>? configureDefaultHeaders = null) {
UseDefaultSerializers();
Expand Down Expand Up @@ -83,19 +83,32 @@ public RestClient(Uri baseUrl) : this(new RestClientOptions { BaseUrl = baseUrl
/// <param name="baseUrl"></param>
public RestClient(string baseUrl) : this(new Uri(Ensure.NotEmptyString(baseUrl, nameof(baseUrl)))) { }

public RestClient(HttpClient httpClient, RestClientOptions? options = null, bool disposeHttpClient = false) {
if (options?.CookieContainer != null) {
public RestClient(HttpClient httpClient, bool disposeHttpClient = false) {
UseDefaultSerializers();

HttpClient = httpClient;
CookieContainer = new CookieContainer();
Options = new RestClientOptions();
_disposeHttpClient = disposeHttpClient;

if (httpClient.BaseAddress != null) {
Options.BaseUrl = httpClient.BaseAddress;
}
}

public RestClient(HttpClient httpClient, RestClientOptions options, bool disposeHttpClient = false) {
if (options.CookieContainer != null) {
throw new ArgumentException("Custom cookie container cannot be added to the HttpClient instance", nameof(options.CookieContainer));
}

UseDefaultSerializers();

HttpClient = httpClient;
Options = options ?? new RestClientOptions();
CookieContainer = new CookieContainer();
Options = options;
_disposeHttpClient = disposeHttpClient;

if (httpClient.BaseAddress != null && Options.BaseUrl == null) {
if (httpClient.BaseAddress != null && options.BaseUrl == null) {
Options.BaseUrl = httpClient.BaseAddress;
}

Expand All @@ -108,7 +121,7 @@ public RestClient(HttpClient httpClient, RestClientOptions? options = null, bool
/// </summary>
/// <param name="handler">Message handler instance to use for HttpClient</param>
/// <param name="disposeHandler">Dispose the handler when disposing RestClient, true by default</param>
public RestClient(HttpMessageHandler handler, bool disposeHandler = true) : this(new HttpClient(handler, disposeHandler), null, true) { }
public RestClient(HttpMessageHandler handler, bool disposeHandler = true) : this(new HttpClient(handler, disposeHandler), true) { }

void ConfigureHttpClient(HttpClient httpClient) {
if (Options.Timeout > 0) httpClient.Timeout = TimeSpan.FromMilliseconds(Options.Timeout);
Expand Down Expand Up @@ -159,7 +172,7 @@ public RestClient AddDefaultParameter(Parameter parameter) {
);

if (!Options.AllowMultipleDefaultParametersWithSameName &&
!MultiParameterTypes.Contains(parameter.Type) &&
!MultiParameterTypes.Contains(parameter.Type) &&
DefaultParameters.Any(x => x.Name == parameter.Name)) {
throw new ArgumentException("A default parameters with the same name has already been added", nameof(parameter));
}
Expand Down Expand Up @@ -218,4 +231,4 @@ public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
}
}

0 comments on commit 17a3532

Please sign in to comment.