Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Newtonsoft.Json MapConverter #602

Merged
merged 9 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <!--TODO: remove? since generated on CI-->
</PropertyGroup>

<PropertyGroup Condition="'$(IsPackable)' == 'true' AND '$(GitVersion_SemVer)' != ''">
<!-- <PropertyGroup Condition="'$(IsPackable)' == 'true' AND '$(GitVersion_SemVer)' != ''">
<GetVersion>false</GetVersion>
<WriteVersionInfoToBuildLog>false</WriteVersionInfoToBuildLog>
<UpdateAssemblyInfo>false</UpdateAssemblyInfo>
Expand All @@ -23,7 +23,7 @@
<FileVersion Condition=" '$(FileVersion)' == '' ">$(GitVersion_AssemblySemFileVer)</FileVersion>
<RepositoryBranch Condition=" '$(RepositoryBranch)' == '' ">$(GitVersion_BranchName)</RepositoryBranch>
<RepositoryCommit Condition=" '$(RepositoryCommit)' == '' ">$(GitVersion_Sha)</RepositoryCommit>
</PropertyGroup>
</PropertyGroup> -->

<PropertyGroup Condition="'$(IsPackable)' != 'true'">
<NoWarn>$(NoWarn);1591</NoWarn>
Expand All @@ -33,10 +33,10 @@
<None Include="..\..\assets\logo.64x64.png" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\..\LICENSE.txt" Pack="true" PackagePath="\" Visible="false" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Update="GitVersionTask" Version="5.6.7">
<!-- <PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" /> -->
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
]
},
"gitversion.tool": {
"version": "5.10.3",
"version": "5.12.0",
"commands": [
"dotnet-gitversion"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net8</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
12 changes: 6 additions & 6 deletions src/GraphQL.Client.Serializer.Newtonsoft/MapConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public override void WriteJson(JsonWriter writer, Map value, JsonSerializer seri
throw new NotImplementedException(
"This converter currently is only intended to be used to read a JSON object into a strongly-typed representation.");

public override Map ReadJson(JsonReader reader, Type objectType, Map existingValue, bool hasExistingValue, JsonSerializer serializer)
public override Map? ReadJson(JsonReader reader, Type objectType, Map existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var rootToken = JToken.ReadFrom(reader);
if (rootToken is JObject)
return rootToken.Type switch
{
return (Map)ReadDictionary(rootToken, new Map());
}
else
throw new ArgumentException("This converter can only parse when the root element is a JSON Object.");
JTokenType.Object => (Map)ReadDictionary(rootToken, new Map()),
JTokenType.Null => null,
_ => throw new ArgumentException("This converter can only parse when the root element is a JSON Object.")
};
}

private object? ReadToken(JToken? token) =>
Expand Down
4 changes: 4 additions & 0 deletions src/GraphQL.Client/GraphQLHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Diagnostics;
#pragma warning disable IDE0005
// see https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/implicit-global-using-netfx
using System.Net.Http;
#pragma warning restore IDE0005
using GraphQL.Client.Abstractions;
using GraphQL.Client.Abstractions.Websocket;
using GraphQL.Client.Http.Websocket;
Expand Down
4 changes: 4 additions & 0 deletions src/GraphQL.Client/GraphQLHttpClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Net;
#pragma warning disable IDE0005
// see https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/implicit-global-using-netfx
using System.Net.Http;
#pragma warning restore IDE0005
using System.Net.Http.Headers;
using System.Net.WebSockets;
using GraphQL.Client.Http.Websocket;
Expand Down
4 changes: 4 additions & 0 deletions src/GraphQL.Client/GraphQLHttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#pragma warning disable IDE0005
// see https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/implicit-global-using-netfx
using System.Net.Http;
#pragma warning restore IDE0005
using System.Net.Http.Headers;
using System.Text;
using GraphQL.Client.Abstractions;
Expand Down
4 changes: 4 additions & 0 deletions src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Diagnostics;
#pragma warning disable IDE0005
// see https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/implicit-global-using-netfx
using System.Net.Http;
#pragma warning restore IDE0005
using System.Net.WebSockets;
using System.Reactive;
using System.Reactive.Disposables;
Expand Down
64 changes: 49 additions & 15 deletions tests/GraphQL.Client.Serializer.Tests/ConsistencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ namespace GraphQL.Client.Serializer.Tests;

public class ConsistencyTests
{
[Fact]
public void MapConvertersShouldBehaveConsistent()
{
const string json = @"{
[Theory]
[InlineData(@"{
""array"": [
""some stuff"",
""something else""
Expand All @@ -27,7 +25,26 @@ public void MapConvertersShouldBehaveConsistent()
{""number"": 1234.567},
{""number"": 567.8}
]
}";
}")]
[InlineData("null")]
public void MapConvertersShouldBehaveConsistent(string json)
{
//const string json = @"{
// ""array"": [
// ""some stuff"",
// ""something else""
// ],
// ""string"": ""this is a string"",
// ""boolean"": true,
// ""number"": 1234.567,
// ""nested object"": {
// ""prop1"": false
// },
// ""arrayOfObjects"": [
// {""number"": 1234.567},
// {""number"": 567.8}
// ]
// }";

var newtonsoftSerializer = new NewtonsoftJsonSerializer();
var systemTextJsonSerializer = new SystemTextJsonSerializer();
Expand All @@ -45,16 +62,33 @@ public void MapConvertersShouldBehaveConsistent()
.RespectingRuntimeTypes());
}

private void CompareMaps(Dictionary<string, object> first, Dictionary<string, object> second)
/// <summary>
/// Regression test for https://github.com/graphql-dotnet/graphql-client/issues/601
/// </summary>
[Fact]
public void MapConvertersShouldBeAbleToDeserializeNullValues()
{
foreach (var keyValuePair in first)
{
second.Should().ContainKey(keyValuePair.Key);
second[keyValuePair.Key].Should().BeOfType(keyValuePair.Value.GetType());
if (keyValuePair.Value is Dictionary<string, object> map)
CompareMaps(map, (Dictionary<string, object>)second[keyValuePair.Key]);
else
keyValuePair.Value.Should().BeEquivalentTo(second[keyValuePair.Key]);
}
var newtonsoftSerializer = new NewtonsoftJsonSerializer();
var systemTextJsonSerializer = new SystemTextJsonSerializer();
string json = "null";

JsonConvert.DeserializeObject<Map>(json, newtonsoftSerializer.JsonSerializerSettings).Should().BeNull();
System.Text.Json.JsonSerializer.Deserialize<Map>(json, systemTextJsonSerializer.Options).Should().BeNull();
}

private void CompareMaps(Dictionary<string, object>? first, Dictionary<string, object>? second)
{
if (first is null)
second.Should().BeNull();
else
foreach (var keyValuePair in first)
{
second.Should().ContainKey(keyValuePair.Key);
second[keyValuePair.Key].Should().BeOfType(keyValuePair.Value.GetType());
if (keyValuePair.Value is Dictionary<string, object> map)
CompareMaps(map, (Dictionary<string, object>)second[keyValuePair.Key]);
else
keyValuePair.Value.Should().BeEquivalentTo(second[keyValuePair.Key]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="../tests.props" />

<PropertyGroup>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="../tests.props" />

<PropertyGroup>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="../tests.props" />

<PropertyGroup>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/GraphQL.Server.Test/GraphQL.Server.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net8</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion tests/IntegrationTestServer/IntegrationTestServer.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7</TargetFramework>
<TargetFramework>net8</TargetFramework>
<StartupObject>IntegrationTestServer.Program</StartupObject>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down