From 5d71f0058953a36cca1b56e7b7aba09953dfee24 Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Thu, 15 Apr 2021 22:13:52 -0400 Subject: [PATCH] Fixed a bug where nullable enum like classes could not be deserialized correctly (#564) * Fixed a bug where nullable enum like classes could not be deserialized correctly * Updated sample project version * Added test for null cases as well --- sample/SampleServer/SampleServer.csproj | 2 +- .../Converters/EnumLikeStringConverter.cs | 8 +++-- test/Dap.Tests/EnumLikeConverterTests.cs | 36 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 test/Dap.Tests/EnumLikeConverterTests.cs diff --git a/sample/SampleServer/SampleServer.csproj b/sample/SampleServer/SampleServer.csproj index 2c1484e22..ce86be19e 100644 --- a/sample/SampleServer/SampleServer.csproj +++ b/sample/SampleServer/SampleServer.csproj @@ -5,7 +5,7 @@ false netcoreapp3.1 win7-x64 - 8.0 + latest diff --git a/src/JsonRpc/Serialization/Converters/EnumLikeStringConverter.cs b/src/JsonRpc/Serialization/Converters/EnumLikeStringConverter.cs index 54908ccff..6ff70ff86 100644 --- a/src/JsonRpc/Serialization/Converters/EnumLikeStringConverter.cs +++ b/src/JsonRpc/Serialization/Converters/EnumLikeStringConverter.cs @@ -13,9 +13,11 @@ public override IEnumLikeString ReadJson( bool hasExistingValue, JsonSerializer serializer ) => - reader.TokenType switch { - JsonToken.String => (IEnumLikeString) Activator.CreateInstance(objectType, (string) reader.Value), - _ => (IEnumLikeString) Activator.CreateInstance(objectType, null) + ( reader.TokenType, Nullable.GetUnderlyingType(objectType) ) switch { + (JsonToken.String, null) => (IEnumLikeString) Activator.CreateInstance(objectType, (string) reader.Value), + (JsonToken.String, { } realType) => (IEnumLikeString) Activator.CreateInstance(realType, (string) reader.Value), + (_, { }) => (IEnumLikeString) Activator.CreateInstance(objectType, null), + _ => null }; public override bool CanRead => true; diff --git a/test/Dap.Tests/EnumLikeConverterTests.cs b/test/Dap.Tests/EnumLikeConverterTests.cs new file mode 100644 index 000000000..00e7b5465 --- /dev/null +++ b/test/Dap.Tests/EnumLikeConverterTests.cs @@ -0,0 +1,36 @@ +using System; +using FluentAssertions; +using OmniSharp.Extensions.DebugAdapter.Client; +using OmniSharp.Extensions.DebugAdapter.Protocol; +using OmniSharp.Extensions.DebugAdapter.Protocol.Models; +using OmniSharp.Extensions.DebugAdapter.Protocol.Requests; +using Xunit; + +namespace Dap.Tests +{ + public class EnumLikeConverterTests + { + [Fact] + public void PathFormat_Should_Be_Serializable() + { + var options = new InitializeRequestArguments() { + PathFormat = PathFormat.Uri + }; + + Action a = () => new DapSerializer().SerializeObject(options); + a.Should().NotThrow(); + } + [Fact] + public void PathFormat_Should_Be_Deserializable() + { + Func a = () => new DapSerializer().DeserializeObject("{\"pathformat\": \"Uri\"}"); + a.Should().NotThrow().Subject.PathFormat.Should().NotBeNull(); + } + [Fact] + public void PathFormat_Should_Be_Deserializable_When_Null() + { + var a = new DapSerializer().DeserializeObject("{\"pathformat\":null}"); + a.PathFormat.Should().BeNull(); + } + } +}