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 2 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
4 changes: 2 additions & 2 deletions sample/SampleServer/SampleServer.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
la<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<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,10 @@ 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)
Copy link
Contributor

@rjmholt rjmholt Apr 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get something like PathFormat? (implying the value can be null), we could see a null from reader.Value I assume.

I would have expected the serialiser to return null in that circumstance rather than new PathFormat(null).

But also, if we do get a PathFormat?, we can't promise a non-nullable IEnumLikeString but rather I would have guessed we need to return IEnumLikeString?.

So in the realType branch, I would have expected something like:

reader.Value is null ? null : (IEnumLikeString) Activator.CreateInstance(realType, (string) reader.Value)

Does that make sense? Or is there something else going on here that makes that not a concern?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds correct, I'll update the pattern

};

public override bool CanRead => true;
Expand Down
30 changes: 30 additions & 0 deletions test/Dap.Tests/EnumLikeConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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()
{
Action a = () => new DapSerializer().DeserializeObject<InitializeRequestArguments>("{\"pathformat\": \"Uri\"}");
a.Should().NotThrow();
}
}
}