From d512d4ce4fa327d6c594eb6ca6b4f74e9a3ffde2 Mon Sep 17 00:00:00 2001 From: Kendall Bennett Date: Fri, 18 Mar 2022 11:48:06 -0400 Subject: [PATCH] Allow forcing only a single custom serializer to be used to clear out any others so the defaults can be changed like UseJson() and UseXml() --- src/RestSharp/RestClientExtensions.cs | 21 ++++++++--- test/RestSharp.Tests/RestClientTests.cs | 46 ++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/RestSharp/RestClientExtensions.cs b/src/RestSharp/RestClientExtensions.cs index db38adc08..da1c96db8 100644 --- a/src/RestSharp/RestClientExtensions.cs +++ b/src/RestSharp/RestClientExtensions.cs @@ -14,6 +14,7 @@ using System.Runtime.CompilerServices; using RestSharp.Extensions; +using RestSharp.Serializers; namespace RestSharp; @@ -352,8 +353,8 @@ [EnumeratorCancellation] CancellationToken cancellationToken /// /// Sets the to only use JSON /// - /// The client instance - /// + /// Client instance to work with + /// Reference to the client instance public static RestClient UseJson(this RestClient client) { client.Serializers.Remove(DataFormat.Xml); client.AssignAcceptedContentTypes(); @@ -363,11 +364,23 @@ public static RestClient UseJson(this RestClient client) { /// /// Sets the to only use XML /// - /// - /// + /// Client instance to work with + /// Reference to the client instance public static RestClient UseXml(this RestClient client) { client.Serializers.Remove(DataFormat.Json); client.AssignAcceptedContentTypes(); return client; } + + /// + /// Sets the to only use the passed in custom serializer + /// + /// Client instance to work with + /// Function that returns the serializer instance + /// Reference to the client instance + public static RestClient UseOnlySerializer(this RestClient client, Func serializerFactory) { + client.Serializers.Clear(); + client.UseSerializer(serializerFactory); + return client; + } } \ No newline at end of file diff --git a/test/RestSharp.Tests/RestClientTests.cs b/test/RestSharp.Tests/RestClientTests.cs index 035f34e71..7d7a675c2 100644 --- a/test/RestSharp.Tests/RestClientTests.cs +++ b/test/RestSharp.Tests/RestClientTests.cs @@ -1,4 +1,6 @@ -namespace RestSharp.Tests; +using RestSharp.Serializers.Json; + +namespace RestSharp.Tests; public class RestClientTests { const string BaseUrl = "http://localhost:8888/"; @@ -58,4 +60,46 @@ public void BuildUri_should_build_with_passing_link_as_Uri_with_set_BaseUrl() { // assert new Uri(baseUrl, relative).Should().Be(builtUri); } + + [Fact] + public void UseJson_leaves_only_json_serializer() { + // arrange + var baseUrl = new Uri(BaseUrl); + + // act + var client = new RestClient(baseUrl); + client.UseJson(); + + // assert + Assert.Single(client.Serializers); + Assert.True(client.Serializers.ContainsKey(DataFormat.Json)); + } + + [Fact] + public void UseXml_leaves_only_json_serializer() { + // arrange + var baseUrl = new Uri(BaseUrl); + + // act + var client = new RestClient(baseUrl); + client.UseXml(); + + // assert + Assert.Single(client.Serializers); + Assert.True(client.Serializers.ContainsKey(DataFormat.Xml)); + } + + [Fact] + public void UseOnlySerializer_leaves_only_custom_serializer() { + // arrange + var baseUrl = new Uri(BaseUrl); + + // act + var client = new RestClient(baseUrl); + client.UseOnlySerializer(() => new SystemTextJsonSerializer()); + + // assert + Assert.Single(client.Serializers); + Assert.True(client.Serializers.ContainsKey(DataFormat.Json)); + } } \ No newline at end of file