Skip to content

Fixed a bug where nullable enum like classes could not be deserialized correctly #564

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

Merged
merged 3 commits into from
Apr 16, 2021
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
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();
}
}
}