-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Detect specifical character in EnumConverter.cs #76873
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
eiriktsarpalis
merged 14 commits into
dotnet:main
from
SilentCC:enumconverter-commar-detec
Oct 26, 2022
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
566967b
add specifical char flag and fail earlier before invoke naming policy
SilentCC 43bec4c
add unit test
SilentCC 4d841ac
improve unit test and special char check
SilentCC 5c9c2ae
fix format error
SilentCC e818b0b
fix unit test
SilentCC fbdb3f6
take comments and improve unit test
SilentCC 0970fca
use string.IndexOfAny
SilentCC 6baad7b
throw exception in constructor and improve unit test
SilentCC cbfeb9e
add custome exception message
SilentCC c642b71
take comments
SilentCC 1449408
Update src/libraries/System.Text.Json/tests/System.Text.Json.FSharp.T…
eiriktsarpalis a3887a5
Update src/libraries/System.Text.Json/tests/System.Text.Json.FSharp.T…
eiriktsarpalis 8b3949a
Update src/libraries/System.Text.Json/tests/System.Text.Json.FSharp.T…
eiriktsarpalis 04ab3a9
Update src/libraries/System.Text.Json/src/System/Text/Json/Serializat…
eiriktsarpalis 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
60 changes: 60 additions & 0 deletions
60
src/libraries/System.Text.Json/tests/System.Text.Json.FSharp.Tests/EnumTests.fs
This file contains hidden or 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,60 @@ | ||
| module System.Text.Json.Tests.FSharp.EnumTests | ||
|
|
||
| open System | ||
| open System.Reflection | ||
| open System.Text.Json | ||
| open System.Text.Json.Serialization | ||
| open Xunit | ||
|
|
||
| [<Flags>] | ||
| type BadEnum = | ||
| | ``There's a comma, in my name`` = 1 | ||
| | ``There's a comma, even here`` = 2 | ||
| | ``ThisisagoodEnumValue`` = 4 | ||
|
|
||
| let badEnum = BadEnum.``There's a comma, in my name`` ||| BadEnum.``There's a comma, even here`` | ||
| let badEnumJsonStr = $"\"{badEnum}\"" | ||
|
|
||
| let badEnumWithGoodValue = BadEnum.ThisisagoodEnumValue | ||
| let badEnumWithGoodValueJsonStr = $"\"{badEnumWithGoodValue}\"" | ||
|
|
||
| [<Flags>] | ||
| type GoodEnum = | ||
| | Thereisnocommainmyname_1 = 1 | ||
| | Thereisnocommaevenhere_2 = 2 | ||
|
|
||
| let goodEnum = GoodEnum.Thereisnocommainmyname_1 ||| GoodEnum.Thereisnocommaevenhere_2 | ||
| let goodEnumJsonStr = $"\"{goodEnum}\"" | ||
|
|
||
| let options = new JsonSerializerOptions() | ||
| options.Converters.Add(new JsonStringEnumConverter()) | ||
|
|
||
| [<Fact>] | ||
| let ``Deserialize With Exception If Enum Contains Special Char`` () = | ||
| let ex = Assert.Throws<TargetInvocationException>(fun () -> JsonSerializer.Deserialize<BadEnum>(badEnumJsonStr, options) |> ignore) | ||
| Assert.Equal(typeof<InvalidOperationException>, ex.InnerException.GetType()) | ||
| Assert.Equal("Enum type 'BadEnum' uses unsupported identifer name 'There's a comma, in my name'.", ex.InnerException.Message) | ||
|
|
||
|
|
||
| [<Fact>] | ||
| let ``Serialize With Exception If Enum Contains Special Char`` () = | ||
| let ex = Assert.Throws<TargetInvocationException>(fun () -> JsonSerializer.Serialize(badEnum, options) |> ignore) | ||
| Assert.Equal(typeof<InvalidOperationException>, ex.InnerException.GetType()) | ||
| Assert.Equal("Enum type 'BadEnum' uses unsupported identifer name 'There's a comma, in my name'.", ex.InnerException.Message) | ||
|
|
||
| [<Fact>] | ||
| let ``Successful Deserialize Normal Enum`` () = | ||
| let actual = JsonSerializer.Deserialize<GoodEnum>(goodEnumJsonStr, options) | ||
| Assert.Equal(GoodEnum.Thereisnocommainmyname_1 ||| GoodEnum.Thereisnocommaevenhere_2, actual) | ||
|
|
||
| [<Fact>] | ||
| let ``Fail Deserialize Good Value Of Bad Enum Type`` () = | ||
| let ex = Assert.Throws<TargetInvocationException>(fun () -> JsonSerializer.Deserialize<BadEnum>(badEnumWithGoodValueJsonStr, options) |> ignore) | ||
| Assert.Equal(typeof<InvalidOperationException>, ex.InnerException.GetType()) | ||
| Assert.Equal("Enum type 'BadEnum' uses unsupported identifer name 'There's a comma, in my name'.", ex.InnerException.Message) | ||
|
|
||
| [<Fact>] | ||
| let ``Fail Serialize Good Value Of Bad Enum Type`` () = | ||
| let ex = Assert.Throws<TargetInvocationException>(fun () -> JsonSerializer.Serialize(badEnumWithGoodValue, options) |> ignore) | ||
| Assert.Equal(typeof<InvalidOperationException>, ex.InnerException.GetType()) | ||
| Assert.Equal("Enum type 'BadEnum' uses unsupported identifer name 'There's a comma, in my name'.", ex.InnerException.Message) |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.