Skip to content

Commit a6f192b

Browse files
committed
remove Options classes from serializers, fix GraphQLWebsocketRequest transformation
1 parent 4557e27 commit a6f192b

File tree

13 files changed

+190
-203
lines changed

13 files changed

+190
-203
lines changed

src/GraphQL.Client.Abstractions/GraphQLJsonSerializerExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public static TSerializerInterface EnsureAssigned<TSerializerInterface>(this TSe
2222
return jsonSerializer;
2323
}
2424

25-
public static TOptions New<TOptions>(this Action<TOptions> configure) {
26-
var options = Activator.CreateInstance<TOptions>();
25+
public static TOptions New<TOptions>(this Action<TOptions> configure) =>
26+
configure.AndReturn(Activator.CreateInstance<TOptions>());
27+
28+
public static TOptions AndReturn<TOptions>(this Action<TOptions> configure, TOptions options) {
2729
configure(options);
2830
return options;
2931
}

src/GraphQL.Client.Serializer.Newtonsoft/GraphQLWebSocketRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public GraphQLWebSocketRequest()
1717
public GraphQLWebSocketRequest(Abstractions.Websocket.GraphQLWebSocketRequest other) {
1818
Id = other.Id;
1919
Type = other.Type;
20-
Payload = new GraphQLRequest(other.Payload); // create serializer-specific type
20+
Payload = other.Payload != null ? new GraphQLRequest(other.Payload) : null; // create serializer-specific type
2121
}
2222
}
2323
}

src/GraphQL.Client.Serializer.Newtonsoft/NewtonsoftJsonSerializer.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.CompilerServices;
43
using System.Text;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using GraphQL.Client.Abstractions;
87
using GraphQL.Client.Abstractions.Websocket;
98
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Serialization;
1010

1111
namespace GraphQL.Client.Serializer.Newtonsoft
1212
{
1313
public class NewtonsoftJsonSerializer: IGraphQLWebsocketJsonSerializer
1414
{
15-
public NewtonsoftJsonSerializerOptions Options { get; }
15+
public static JsonSerializerSettings DefaultJsonSerializerSettings => new JsonSerializerSettings {
16+
ContractResolver = new CamelCasePropertyNamesContractResolver { IgnoreIsSpecifiedMembers = true },
17+
MissingMemberHandling = MissingMemberHandling.Ignore
18+
};
1619

17-
public NewtonsoftJsonSerializer() : this(new NewtonsoftJsonSerializerOptions()) { }
20+
public JsonSerializerSettings JsonSerializerSettings { get; }
1821

19-
public NewtonsoftJsonSerializer(Action<NewtonsoftJsonSerializerOptions> configure) : this(configure.New()) { }
22+
public NewtonsoftJsonSerializer() : this(DefaultJsonSerializerSettings) { }
2023

21-
public NewtonsoftJsonSerializer(NewtonsoftJsonSerializerOptions options) {
22-
Options = options;
24+
public NewtonsoftJsonSerializer(Action<JsonSerializerSettings> configure) : this(configure.AndReturn(DefaultJsonSerializerSettings)) { }
25+
26+
public NewtonsoftJsonSerializer(JsonSerializerSettings jsonSerializerSettings) {
27+
JsonSerializerSettings = jsonSerializerSettings;
2328
ConfigureMandatorySerializerOptions();
2429
}
2530

2631
private void ConfigureMandatorySerializerOptions() {
2732
// deserialize extensions to Dictionary<string, object>
28-
Options.JsonSerializerSettings.Converters.Insert(0, new GraphQLExtensionsConverter());
33+
JsonSerializerSettings.Converters.Insert(0, new GraphQLExtensionsConverter());
2934
}
3035

3136
public string SerializeToString(GraphQL.GraphQLRequest request) {
32-
return JsonConvert.SerializeObject(new GraphQLRequest(request), Options.JsonSerializerSettings);
37+
return JsonConvert.SerializeObject(new GraphQLRequest(request), JsonSerializerSettings);
3338
}
3439

3540
public byte[] SerializeToBytes(Abstractions.Websocket.GraphQLWebSocketRequest request) {
36-
var json = JsonConvert.SerializeObject(new GraphQLWebSocketRequest(request), Options.JsonSerializerSettings);
41+
var json = JsonConvert.SerializeObject(new GraphQLWebSocketRequest(request), JsonSerializerSettings);
3742
return Encoding.UTF8.GetBytes(json);
3843
}
3944

@@ -43,7 +48,7 @@ public Task<WebsocketResponseWrapper> DeserializeToWebsocketResponseWrapperAsync
4348

4449
public GraphQLWebSocketResponse<GraphQLResponse<TResponse>> DeserializeToWebsocketResponse<TResponse>(byte[] bytes) {
4550
return JsonConvert.DeserializeObject<GraphQLWebSocketResponse<GraphQLResponse<TResponse>>>(Encoding.UTF8.GetString(bytes),
46-
Options.JsonSerializerSettings);
51+
JsonSerializerSettings);
4752
}
4853

4954
public Task<GraphQLResponse<TResponse>> DeserializeFromUtf8StreamAsync<TResponse>(Stream stream, CancellationToken cancellationToken) {
@@ -54,7 +59,7 @@ public Task<GraphQLResponse<TResponse>> DeserializeFromUtf8StreamAsync<TResponse
5459
private Task<T> DeserializeFromUtf8Stream<T>(Stream stream) {
5560
using StreamReader sr = new StreamReader(stream);
5661
using JsonReader reader = new JsonTextReader(sr);
57-
JsonSerializer serializer = JsonSerializer.Create(Options.JsonSerializerSettings);
62+
JsonSerializer serializer = JsonSerializer.Create(JsonSerializerSettings);
5863
return Task.FromResult(serializer.Deserialize<T>(reader));
5964
}
6065

src/GraphQL.Client.Serializer.Newtonsoft/NewtonsoftJsonSerializerOptions.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/GraphQL.Client.Serializer.SystemTextJson/GraphQLWebSocketRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public GraphQLWebSocketRequest()
1717
public GraphQLWebSocketRequest(Abstractions.Websocket.GraphQLWebSocketRequest other) {
1818
Id = other.Id;
1919
Type = other.Type;
20-
Payload = new GraphQLRequest(other.Payload);
20+
Payload = other.Payload != null ? new GraphQLRequest(other.Payload) : null; // create serializer-specific type;
2121
}
2222
}
2323
}

src/GraphQL.Client.Serializer.SystemTextJson/SystemTextJsonSerializer.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,55 @@
33
using System.Text.Json;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using Dahomey.Json;
67
using GraphQL.Client.Abstractions;
78
using GraphQL.Client.Abstractions.Websocket;
89

910
namespace GraphQL.Client.Serializer.SystemTextJson
1011
{
1112
public class SystemTextJsonSerializer: IGraphQLWebsocketJsonSerializer
1213
{
13-
public SystemTextJsonSerializerOptions Options { get; }
14+
public static JsonSerializerOptions DefaultJsonSerializerOptions => new JsonSerializerOptions {
15+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
16+
}.SetupExtensions();
1417

15-
public SystemTextJsonSerializer() : this(new SystemTextJsonSerializerOptions()) { }
18+
public JsonSerializerOptions Options { get; }
1619

17-
public SystemTextJsonSerializer(Action<SystemTextJsonSerializerOptions> configure) : this(configure.New()) { }
20+
public SystemTextJsonSerializer() : this(DefaultJsonSerializerOptions) { }
1821

19-
public SystemTextJsonSerializer(SystemTextJsonSerializerOptions options) {
22+
public SystemTextJsonSerializer(Action<JsonSerializerOptions> configure) : this(configure.AndReturn(DefaultJsonSerializerOptions)) { }
23+
24+
public SystemTextJsonSerializer(JsonSerializerOptions options) {
2025
Options = options;
2126
ConfigureMandatorySerializerOptions();
2227
}
2328

2429
private void ConfigureMandatorySerializerOptions() {
2530
// deserialize extensions to Dictionary<string, object>
26-
Options.JsonSerializerOptions.Converters.Insert(0, new GraphQLExtensionsConverter());
31+
Options.Converters.Insert(0, new GraphQLExtensionsConverter());
2732
// allow the JSON field "data" to match the property "Data" even without JsonNamingPolicy.CamelCase
28-
Options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
33+
Options.PropertyNameCaseInsensitive = true;
2934
}
3035

3136
public string SerializeToString(GraphQL.GraphQLRequest request) {
32-
return JsonSerializer.Serialize(new GraphQLRequest(request), Options.JsonSerializerOptions);
37+
return JsonSerializer.Serialize(new GraphQLRequest(request), Options);
3338
}
3439

3540
public Task<GraphQLResponse<TResponse>> DeserializeFromUtf8StreamAsync<TResponse>(Stream stream, CancellationToken cancellationToken) {
36-
return JsonSerializer.DeserializeAsync<GraphQLResponse<TResponse>>(stream, Options.JsonSerializerOptions, cancellationToken).AsTask();
41+
return JsonSerializer.DeserializeAsync<GraphQLResponse<TResponse>>(stream, Options, cancellationToken).AsTask();
3742
}
3843

3944
public byte[] SerializeToBytes(Abstractions.Websocket.GraphQLWebSocketRequest request) {
40-
return JsonSerializer.SerializeToUtf8Bytes(new GraphQLWebSocketRequest(request), Options.JsonSerializerOptions);
45+
return JsonSerializer.SerializeToUtf8Bytes(new GraphQLWebSocketRequest(request), Options);
4146
}
4247

4348
public Task<WebsocketResponseWrapper> DeserializeToWebsocketResponseWrapperAsync(Stream stream) {
44-
return JsonSerializer.DeserializeAsync<WebsocketResponseWrapper>(stream, Options.JsonSerializerOptions).AsTask();
49+
return JsonSerializer.DeserializeAsync<WebsocketResponseWrapper>(stream, Options).AsTask();
4550
}
4651

4752
public GraphQLWebSocketResponse<GraphQLResponse<TResponse>> DeserializeToWebsocketResponse<TResponse>(byte[] bytes) {
4853
return JsonSerializer.Deserialize<GraphQLWebSocketResponse<GraphQLResponse<TResponse>>>(new ReadOnlySpan<byte>(bytes),
49-
Options.JsonSerializerOptions);
54+
Options);
5055
}
5156

5257
}

src/GraphQL.Client.Serializer.SystemTextJson/SystemTextJsonSerializerOptions.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/GraphQL.Client.Serializer.Tests/NewtonsoftSerializerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public NewtonsoftSerializerTest() : base(new NewtonsoftJsonSerializer()) { }
88

99
public class NewtonsoftSerializeNoCamelCaseTest : BaseSerializeNoCamelCaseTest {
1010
public NewtonsoftSerializeNoCamelCaseTest()
11-
: base(new NewtonsoftJsonSerializer(options => options.JsonSerializerSettings = new JsonSerializerSettings())) { }
11+
: base(new NewtonsoftJsonSerializer(new JsonSerializerSettings())) { }
1212
}
1313
}

tests/GraphQL.Client.Serializer.Tests/SystemTextJsonSerializerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public SystemTextJsonSerializerTests() : base(new SystemTextJsonSerializer()) {
99

1010
public class SystemTextJsonSerializeNoCamelCaseTest : BaseSerializeNoCamelCaseTest {
1111
public SystemTextJsonSerializeNoCamelCaseTest()
12-
: base(new SystemTextJsonSerializer(options => options.JsonSerializerOptions = new JsonSerializerOptions().SetupExtensions())) { }
12+
: base(new SystemTextJsonSerializer(new JsonSerializerOptions().SetupExtensions())) { }
1313
}
1414
}

tests/GraphQL.Client.Tests.Common/GraphQL.Client.Tests.Common.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="FluentAssertions" Version="5.10.0" />
89
<PackageReference Include="GraphQL" Version="2.4.0" />
910
<PackageReference Include="GraphQL.Server.Transports.Subscriptions.Abstractions" Version="3.4.0" />
1011
<PackageReference Include="GraphQL.StarWars" Version="1.0.0" />
1112
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
1213
<PackageReference Include="System.Reactive" Version="4.3.2" />
1314
<PackageReference Include="System.Reactive.Compatibility" Version="4.3.2" />
14-
<PackageReference Include="xunit" Version="2.4.0" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

0 commit comments

Comments
 (0)