-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add support for enum dictionary keys #29
Merged
Nihlus
merged 3 commits into
Remora:main
from
VelvetToroyashi:velvet/feat/dictionary-enum-keys
Mar 19, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
Tests/Remora.Rest.Tests/Tests/Json/StringEnumConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// | ||
// SPDX-FileName: StringEnumConverterTests.cs | ||
// SPDX-FileCopyrightText: Copyright (c) Jarl Gullberg | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// | ||
|
||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Remora.Rest.Json; | ||
using Remora.Rest.Json.Policies; | ||
using Remora.Rest.Tests.Data.DataObjects; | ||
using Xunit; | ||
|
||
namespace Remora.Rest.Tests.Json; | ||
|
||
/// <summary> | ||
/// Tests for <see cref="StringEnumConverter{TEnum}"/>. | ||
/// </summary> | ||
public class StringEnumConverterTests | ||
{ | ||
/// <summary> | ||
/// Tests that the converter can serialize a dictionary where the key is an enum. | ||
/// </summary> | ||
[Fact] | ||
public void CanSerializeDictionaryKeyAsInteger() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection() | ||
.Configure<JsonSerializerOptions> | ||
( | ||
json => | ||
{ | ||
json.PropertyNamingPolicy = new SnakeCaseNamingPolicy(); | ||
json.Converters.Add(new StringEnumConverter<StringifiedEnum>(asInteger: true)); | ||
}) | ||
.BuildServiceProvider(); | ||
|
||
var dictionary = new Dictionary<StringifiedEnum, string> | ||
{ | ||
{ StringifiedEnum.First, "first" }, | ||
{ StringifiedEnum.Second, "second" }, | ||
{ StringifiedEnum.Third, "third" }, | ||
}; | ||
|
||
var jsonOptions = services.GetRequiredService<IOptions<JsonSerializerOptions>>().Value; | ||
|
||
// Act | ||
var result = JsonSerializer.Serialize(dictionary, jsonOptions); | ||
var expected = "{\"0\":\"first\",\"1\":\"second\",\"2\":\"third\"}"; | ||
|
||
// Assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the converter can serialize a dictionary where the key is an enum. | ||
/// </summary> | ||
[Fact] | ||
public void CanSerializeDictionaryKeyAsString() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection() | ||
.Configure<JsonSerializerOptions> | ||
( | ||
json => | ||
{ | ||
json.PropertyNamingPolicy = new SnakeCaseNamingPolicy(); | ||
json.Converters.Add(new StringEnumConverter<StringifiedEnum>(json.PropertyNamingPolicy)); | ||
}) | ||
.BuildServiceProvider(); | ||
|
||
var dictionary = new Dictionary<StringifiedEnum, string> | ||
{ | ||
{ StringifiedEnum.First, "first" }, | ||
{ StringifiedEnum.Second, "second" }, | ||
{ StringifiedEnum.Third, "third" }, | ||
}; | ||
|
||
var jsonOptions = services.GetRequiredService<IOptions<JsonSerializerOptions>>().Value; | ||
|
||
// Act | ||
var result = JsonSerializer.Serialize(dictionary, jsonOptions); | ||
var expected = "{\"first\":\"first\",\"second\":\"second\",\"third\":\"third\"}"; | ||
|
||
// Assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the converter can read a dictionary where the key is an enum. | ||
/// </summary> | ||
[Fact] | ||
public void CanDeserializeDictionaryKeyAsInteger() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection() | ||
.Configure<JsonSerializerOptions> | ||
( | ||
json => | ||
{ | ||
json.PropertyNamingPolicy = new SnakeCaseNamingPolicy(); | ||
json.Converters.Add(new StringEnumConverter<StringifiedEnum>(asInteger: true)); | ||
}) | ||
.BuildServiceProvider(); | ||
|
||
var jsonOptions = services.GetRequiredService<IOptions<JsonSerializerOptions>>().Value; | ||
|
||
var json = "{\"0\":\"first\",\"1\":\"second\",\"2\":\"third\"}"; | ||
|
||
// Act | ||
var result = JsonSerializer.Deserialize<Dictionary<StringifiedEnum, string>>(json, jsonOptions); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal(3, result.Count); | ||
Assert.Equal("first", result[StringifiedEnum.First]); | ||
Assert.Equal("second", result[StringifiedEnum.Second]); | ||
Assert.Equal("third", result[StringifiedEnum.Third]); | ||
} | ||
|
||
/// <summary> | ||
/// Tests that the converter can read a dictionary where the key is an enum. | ||
/// </summary> | ||
[Fact] | ||
public void CanDeserializeDictionaryKeyAsString() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection() | ||
.Configure<JsonSerializerOptions> | ||
( | ||
json => | ||
{ | ||
json.PropertyNamingPolicy = new SnakeCaseNamingPolicy(); | ||
json.Converters.Add(new StringEnumConverter<StringifiedEnum>(json.PropertyNamingPolicy)); | ||
}) | ||
.BuildServiceProvider(); | ||
|
||
var jsonOptions = services.GetRequiredService<IOptions<JsonSerializerOptions>>().Value; | ||
|
||
var json = "{\"first\":\"first\",\"second\":\"second\",\"third\":\"third\"}"; | ||
|
||
// Act | ||
var result = JsonSerializer.Deserialize<Dictionary<StringifiedEnum, string>>(json, jsonOptions); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal(3, result.Count); | ||
Assert.Equal("first", result[StringifiedEnum.First]); | ||
Assert.Equal("second", result[StringifiedEnum.Second]); | ||
Assert.Equal("third", result[StringifiedEnum.Third]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unlikely to work since
Read
looks for a JsonTokenType of String, but PropertyName is its own TokenType.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. Did this because it's what STJ does, but I can look this over again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add unit test to verify :P