Skip to content

Commit

Permalink
Fixed a bug where nullable enum like classes could not be deserialize…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
david-driscoll authored Apr 16, 2021
1 parent 1b6788d commit 5d71f00
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sample/SampleServer/SampleServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<IsPackable>false</IsPackable>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<LangVersion>8.0</LangVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions test/Dap.Tests/EnumLikeConverterTests.cs
Original file line number Diff line number Diff line change
@@ -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<InitializeRequestArguments> a = () => new DapSerializer().DeserializeObject<InitializeRequestArguments>("{\"pathformat\": \"Uri\"}");
a.Should().NotThrow().Subject.PathFormat.Should().NotBeNull();
}
[Fact]
public void PathFormat_Should_Be_Deserializable_When_Null()
{
var a = new DapSerializer().DeserializeObject<InitializeRequestArguments>("{\"pathformat\":null}");
a.PathFormat.Should().BeNull();
}
}
}

0 comments on commit 5d71f00

Please sign in to comment.