-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #582 from SteveDunn/581-stj-guid-exception
The System.Text.JsonConverter now throws a JsonException
- Loading branch information
Showing
1,438 changed files
with
13,985 additions
and
4,287 deletions.
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
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,34 @@ | ||
using System.Text.Json; | ||
|
||
namespace ConsumerTests.BugFixTests.BugFix581; | ||
|
||
public class ObjectContainerForVo | ||
{ | ||
public required ObjectType Ot { get; set; } | ||
} | ||
|
||
[ValueObject<Guid>(Conversions.Default)] | ||
public readonly partial struct ObjectType | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Fixes bug https://github.com/SteveDunn/Vogen/issues/581 where GUIDs that are deserialized | ||
/// with null, throw a NullReferenceException instead of System.Text.JsonException. | ||
/// </summary> | ||
public class Tests | ||
{ | ||
[Fact] | ||
public void Should_throw_Stj_exception_with_null_passed() | ||
{ | ||
Action a = () => JsonSerializer.Deserialize<ObjectContainerForVo>("""{"Ot":null}"""); | ||
a.Should().ThrowExactly<JsonException>().WithMessage("The JSON value could not be converted to ConsumerTests.BugFixTests.BugFix581.ObjectType. Path: $.Ot | LineNumber: 0 | BytePositionInLine: 10."); | ||
} | ||
|
||
[Fact] | ||
public void Should_throw_Stj_exception_with_invalid_guid_passed() | ||
{ | ||
Action a = () => JsonSerializer.Deserialize<ObjectContainerForVo>("""{"Ot":"Trevor woz ere"}"""); | ||
a.Should().ThrowExactly<JsonException>().WithMessage("The JSON value could not be converted to ConsumerTests.BugFixTests.BugFix581.ObjectType. Path: $.Ot | LineNumber: 0 | BytePositionInLine: 22."); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/SnapshotTests/BugFixes/Bug581_StjDeserializer_throws_correct_exception.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,43 @@ | ||
using System.Threading.Tasks; | ||
using Shared; | ||
using VerifyXunit; | ||
using Vogen; | ||
|
||
namespace SnapshotTests.BugFixes; | ||
|
||
// See https://github.com/SteveDunn/Vogen/issues/581 | ||
[UsesVerify] | ||
public class Bug581_StjDeserializer_throws_correct_exception | ||
{ | ||
// The System.Text.Json converter called Guid.Parse, which in turn threw a NullReferenceException. | ||
// It should now use Guid.TryParse and return an STJ exception. | ||
// This test just verifies that the correct code is generated. The consumer tests verify the runtime | ||
// behaviour. | ||
[Fact] | ||
public async Task Test() | ||
{ | ||
var source = """ | ||
using System; | ||
using Vogen; | ||
using System.Text.Json; | ||
[ValueObject(typeof(Guid))] | ||
public partial struct Vo | ||
{ | ||
} | ||
"""; | ||
|
||
await RunTest(source); | ||
} | ||
|
||
|
||
private static Task RunTest(string source) => | ||
new SnapshotRunner<ValueObjectGenerator>() | ||
.WithSource(source) | ||
.IgnoreInitialCompilationErrors() | ||
.RunOnAllFrameworks(); | ||
|
||
} |
Oops, something went wrong.