Support JsonStringEnumConverter. Fixes #19086 (#20446)

* Fix serializing enums. Fixes #19086

* Add E2E test
This commit is contained in:
Steve Sanderson 2020-04-02 16:09:34 +01:00 committed by GitHub
parent dd2f0c9d90
commit 113cb5422d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 0 deletions

View File

@ -24,4 +24,15 @@
<type fullname="WebAssembly.Net.Http.HttpClient.FetchCredentialsOption" />
<type fullname="WebAssembly.Net.Http.HttpClient.WasmHttpMessageHandler" />
</assembly>
<assembly fullname="System.Text.Json">
<!-- S.T.J. uses Activator.CreateInstance to instantiate converters, so we need to preserve default constructors.
For safety, do this for all converter types, even though most of them are preserved anyway due to being referenced
statically. It's only JsonStringEnumConverter that currently isn't referenced statically.
The following is a workaround for https://github.com/dotnet/aspnetcore/issues/19086.
The underlying issue is reported in the runtime repo at https://github.com/dotnet/runtime/issues/34449 -->
<type fullname="System.Text.Json.Serialization.*Converter">
<method signature="System.Void .ctor()" />
</type>
</assembly>
</linker>

View File

@ -0,0 +1,39 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using BasicTestApp;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
using Microsoft.AspNetCore.E2ETesting;
using OpenQA.Selenium;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
{
public class JsonSerializationTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>>
{
public JsonSerializationTest(
BrowserFixture browserFixture,
ToggleExecutionModeServerFixture<Program> serverFixture,
ITestOutputHelper output)
: base(browserFixture, serverFixture, output)
{
}
protected override void InitializeAsyncCore()
{
Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client);
Browser.MountTestComponent<JsonSerializationCases>();
Browser.Exists(By.Id("json-serialization-cases"));
}
[Fact]
public void JsonSerializationCasesWork()
{
Browser.Equal("Lord Smythe", () => Browser.FindElement(By.Id("deserialized-name")).Text);
Browser.Equal("68", () => Browser.FindElement(By.Id("deserialized-age")).Text);
Browser.Equal("Vexed", () => Browser.FindElement(By.Id("deserialized-mood")).Text);
}
}
}

View File

@ -46,6 +46,7 @@
<option value="BasicTestApp.InputEventComponent">Input events</option>
<option value="BasicTestApp.InteropComponent">Interop component</option>
<option value="BasicTestApp.InteropOnInitializationComponent">Interop on initialization</option>
<option value="BasicTestApp.JsonSerializationCases">JSON serialization</option>
<option value="BasicTestApp.KeyCasesComponent">Key cases</option>
<option value="BasicTestApp.KeyPressEventComponent">Key press event</option>
<option value="BasicTestApp.LaggyTypingComponent">Laggy typing</option>

View File

@ -0,0 +1,35 @@
@using System.Text.Json
@using System.Text.Json.Serialization
<h3 id="json-serialization-cases">JSON serialization cases</h3>
<p>
Most aspects of System.Text.Json don't need to be tested here, as that's an external library. However some cases
are worth verifying under WebAssembly/linking specifically.
</p>
<p>Name: <strong id="deserialized-name">@deserializedPerson.Name</strong></p>
<p>Age: <strong id="deserialized-age">@deserializedPerson.Age</strong></p>
<p>Mood: <strong id="deserialized-mood">@deserializedPerson.Mood</strong></p>
@code {
Person deserializedPerson;
protected override void OnInitialized()
{
// Round-trip some data
var input = new Person { Name = "Lord Smythe", Age = 68, Mood = EmotionalState.Vexed };
var serializedJson = JsonSerializer.Serialize(input);
deserializedPerson = JsonSerializer.Deserialize<Person>(serializedJson);
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public EmotionalState Mood { get; set; }
}
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum EmotionalState { Sombre, Vexed, Irate, Tormented }
}