-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
15 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
specs/Qowaiv.CodeGeneration.Specs/OpenApi/JSON_serialization_specs.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,67 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Open_API.JSON_serialization_specs; | ||
|
||
public class Serializes | ||
{ | ||
[Test] | ||
public void Model_with_derived_types() | ||
{ | ||
var json = @"{""Items"":[{""Value1"":""1.34534"",""Type"":""Derived1""},{""Value2"":""2.85464"",""Type"":""Derived2""}]}"; | ||
|
||
var model = JsonSerializer.Deserialize<MyContainer>(json); | ||
|
||
model.Should().BeEquivalentTo(new MyContainer | ||
{ | ||
Items = | ||
[ | ||
new Derived1{ Type = "Derived1", Value1 = "1.34534" }, | ||
new Derived2{ Type = "Derived2", Value2 = "2.85464" }, | ||
] | ||
}); | ||
} | ||
} | ||
|
||
public class Deserializes | ||
{ | ||
[Test] | ||
public void Model_with_derived_types() | ||
{ | ||
var model = new MyContainer | ||
{ | ||
Items = | ||
[ | ||
new Derived1{ Type = "Derived1", Value1 = "1.34534" }, | ||
new Derived2{ Type = "Derived2", Value2 = "2.85464" }, | ||
] | ||
}; | ||
|
||
var json = JsonSerializer.Serialize(model); | ||
|
||
|
||
Console.WriteLine(json); | ||
} | ||
} | ||
|
||
|
||
internal sealed record MyContainer | ||
{ | ||
public MyBase[] Items { get; init; } = []; | ||
} | ||
|
||
[JsonDerivedType(typeof(Derived1))] | ||
[JsonDerivedType(typeof(Derived2))] | ||
internal record MyBase | ||
{ | ||
public string? Type { get; init; } | ||
} | ||
internal sealed record Derived1 : MyBase | ||
{ | ||
public string? Value1 { get; init; } | ||
} | ||
|
||
internal sealed record Derived2 : MyBase | ||
{ | ||
public string? Value2 { get; init; } | ||
} |
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,6 @@ | ||
namespace Qowaiv.CodeGeneration; | ||
|
||
public sealed class Nill | ||
{ | ||
public static readonly Nill Value = new(); | ||
} |
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
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