Skip to content

Commit 5d71f00

Browse files
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
1 parent 1b6788d commit 5d71f00

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

sample/SampleServer/SampleServer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<IsPackable>false</IsPackable>
66
<TargetFramework>netcoreapp3.1</TargetFramework>
77
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
8-
<LangVersion>8.0</LangVersion>
8+
<LangVersion>latest</LangVersion>
99
</PropertyGroup>
1010

1111
<ItemGroup>

src/JsonRpc/Serialization/Converters/EnumLikeStringConverter.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ public override IEnumLikeString ReadJson(
1313
bool hasExistingValue,
1414
JsonSerializer serializer
1515
) =>
16-
reader.TokenType switch {
17-
JsonToken.String => (IEnumLikeString) Activator.CreateInstance(objectType, (string) reader.Value),
18-
_ => (IEnumLikeString) Activator.CreateInstance(objectType, null)
16+
( reader.TokenType, Nullable.GetUnderlyingType(objectType) ) switch {
17+
(JsonToken.String, null) => (IEnumLikeString) Activator.CreateInstance(objectType, (string) reader.Value),
18+
(JsonToken.String, { } realType) => (IEnumLikeString) Activator.CreateInstance(realType, (string) reader.Value),
19+
(_, { }) => (IEnumLikeString) Activator.CreateInstance(objectType, null),
20+
_ => null
1921
};
2022

2123
public override bool CanRead => true;
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using FluentAssertions;
3+
using OmniSharp.Extensions.DebugAdapter.Client;
4+
using OmniSharp.Extensions.DebugAdapter.Protocol;
5+
using OmniSharp.Extensions.DebugAdapter.Protocol.Models;
6+
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
7+
using Xunit;
8+
9+
namespace Dap.Tests
10+
{
11+
public class EnumLikeConverterTests
12+
{
13+
[Fact]
14+
public void PathFormat_Should_Be_Serializable()
15+
{
16+
var options = new InitializeRequestArguments() {
17+
PathFormat = PathFormat.Uri
18+
};
19+
20+
Action a = () => new DapSerializer().SerializeObject(options);
21+
a.Should().NotThrow();
22+
}
23+
[Fact]
24+
public void PathFormat_Should_Be_Deserializable()
25+
{
26+
Func<InitializeRequestArguments> a = () => new DapSerializer().DeserializeObject<InitializeRequestArguments>("{\"pathformat\": \"Uri\"}");
27+
a.Should().NotThrow().Subject.PathFormat.Should().NotBeNull();
28+
}
29+
[Fact]
30+
public void PathFormat_Should_Be_Deserializable_When_Null()
31+
{
32+
var a = new DapSerializer().DeserializeObject<InitializeRequestArguments>("{\"pathformat\":null}");
33+
a.PathFormat.Should().BeNull();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)