Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
$type: 2,
anotherLeafProperty: 2,
baseProperty: 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
$type: LeafContract,
leafProperty: 2,
baseProperty: 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
using VerifyXunit;
using Xunit;

namespace Aviationexam.GeneratedJsonConverters.SourceGenerator.Target.Tests;

public class BaseContractSerializationTests
{
private JsonSerializerOptions CreateJsonSerializerOptions()
{
IJsonTypeInfoResolver jsonTypeInfoResolver = MyJsonSerializerContext.Default;
foreach (var jsonTypeInfoConfiguration in MyJsonSerializerContext.GetPolymorphicJsonTypeInfoConfigurations())
{
jsonTypeInfoResolver = jsonTypeInfoResolver.WithAddedModifier(jsonTypeInfoConfiguration);
}

var jsonOptions = new JsonSerializerOptions(MyJsonSerializerContext.Default.Options)
{
TypeInfoResolver = jsonTypeInfoResolver,
};

return jsonOptions;
}

[Theory]
[MemberData(nameof(BaseJsonContractData))]
public void DeserializeBaseContractWorks(string json, Type targetType)
{
var baseContract = JsonSerializer.Deserialize<BaseContract>(
json,
MyJsonSerializerContext.Default.Options
CreateJsonSerializerOptions()
);

Assert.NotNull(baseContract);
Expand Down Expand Up @@ -43,12 +62,43 @@ public void SerializeBaseContractWorks(BaseContract contract, string expectedJso
{
var json = JsonSerializer.Serialize(
contract,
MyJsonSerializerContext.Default.Options
CreateJsonSerializerOptions()
);

Assert.Equal(expectedJson, json);
}


[Fact]
public Task SerializeLeafContractWorks()
{
var json = JsonSerializer.Serialize(
new LeafContract
{
BaseProperty = 1,
LeafProperty = 2
},
CreateJsonSerializerOptions()
);

return Verifier.VerifyJson(json);
}

[Fact]
public Task SerializeAnotherLeafContractWorks()
{
var json = JsonSerializer.Serialize(
new AnotherLeafContract
{
BaseProperty = 1,
AnotherLeafProperty = 2
},
CreateJsonSerializerOptions()
);

return Verifier.VerifyJson(json);
}

public static IEnumerable<object[]> BaseJsonContractData()
{
yield return
Expand Down Expand Up @@ -137,55 +187,52 @@ public static IEnumerable<object[]> BaseJsonContractData()
];
}

public static IEnumerable<object[]> LeafContractData()
public static TheoryData<BaseContract, string> LeafContractData() => new()
{
yield return
[
{
new LeafContract
{
BaseProperty = 1,
LeafProperty = 2
},
// language=json
"""
{
"$type": "LeafContract",
"leafProperty": 2,
"baseProperty": 1
}
""".Replace("\r\n", Environment.NewLine),
];
yield return
[
{
"$type": "LeafContract",
"leafProperty": 2,
"baseProperty": 1
}
""".Replace("\r\n", Environment.NewLine)
},
{
new AnotherLeafContract
{
BaseProperty = 1,
AnotherLeafProperty = 2
},
// language=json
"""
{
"$type": 2,
"anotherLeafProperty": 2,
"baseProperty": 1
}
""".Replace("\r\n", Environment.NewLine),
];
yield return
[
{
"$type": 2,
"anotherLeafProperty": 2,
"baseProperty": 1
}
""".Replace("\r\n", Environment.NewLine)
},
{
new GenericLeafContract
{
BaseProperty = 1,
Property = 2
},
// language=json
"""
{
"$type": "GenericLeafContract",
"property": 2,
"baseProperty": 1
}
""".Replace("\r\n", Environment.NewLine),
];
}
{
"$type": "GenericLeafContract",
"property": 2,
"baseProperty": 1
}
""".Replace("\r\n", Environment.NewLine)
}
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Aviationexam.GeneratedJsonConverters.SourceGenerator.Target.ContractWithCustomDelimiter;
using System;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
using VerifyTests;
using VerifyXunit;
Expand All @@ -10,13 +11,29 @@ namespace Aviationexam.GeneratedJsonConverters.SourceGenerator.Target.Tests;

public partial class BaseContractWithCustomDelimiterSerializationTests
{
private JsonSerializerOptions CreateMyJsonSerializerOptions()
{
IJsonTypeInfoResolver jsonTypeInfoResolver = MyJsonSerializerContext.Default;
foreach (var jsonTypeInfoConfiguration in MyJsonSerializerContext.GetPolymorphicJsonTypeInfoConfigurations())
{
jsonTypeInfoResolver = jsonTypeInfoResolver.WithAddedModifier(jsonTypeInfoConfiguration);
}

var jsonOptions = new JsonSerializerOptions(MyJsonSerializerContext.Default.Options)
{
TypeInfoResolver = jsonTypeInfoResolver,
};

return jsonOptions;
}

[Theory]
[MemberData(nameof(BaseJsonContractData))]
public void DeserializeBaseContractWorks(string json, Type targetType)
{
var baseContract = JsonSerializer.Deserialize<BaseContractWithCustomDelimiter>(
json,
MyJsonSerializerContext.Default.Options
CreateMyJsonSerializerOptions()
);

Assert.NotNull(baseContract);
Expand All @@ -35,12 +52,27 @@ public Task SerializeBaseContractWorks(int testId, BaseContractWithCustomDelimit
{
var json = JsonSerializer.Serialize(
contract,
MyJsonSerializerContext.Default.Options
CreateMyJsonSerializerOptions()
);

var settings = new VerifySettings();
settings.UseParameters(testId);

return Verifier.VerifyJson(json, settings);
}

[Fact]
public Task SerializeLeafContractWithCustomDelimiterWorks()
{
var json = JsonSerializer.Serialize(
new LeafContractWithCustomDelimiter
{
BaseProperty = 1,
LeafProperty = 2
},
CreateMyJsonSerializerOptions()
);

return Verifier.VerifyJson(json);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Aviationexam.GeneratedJsonConverters.SourceGenerator.Target.ContractWithCustomDelimiter;
using System;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
using VerifyTests;
using VerifyXunit;
Expand All @@ -10,13 +11,29 @@ namespace Aviationexam.GeneratedJsonConverters.SourceGenerator.Target.Tests;

public partial class BaseContractWithCustomDelimiterSerializationTests
{
private JsonSerializerOptions CreateMySecondJsonSerializerOptions()
{
IJsonTypeInfoResolver jsonTypeInfoResolver = MySecondJsonSerializerContext.Default;
foreach (var jsonTypeInfoConfiguration in MySecondJsonSerializerContext.GetPolymorphicJsonTypeInfoConfigurations())
{
jsonTypeInfoResolver = jsonTypeInfoResolver.WithAddedModifier(jsonTypeInfoConfiguration);
}

var jsonOptions = new JsonSerializerOptions(MySecondJsonSerializerContext.Default.Options)
{
TypeInfoResolver = jsonTypeInfoResolver,
};

return jsonOptions;
}

[Theory]
[MemberData(nameof(BaseJsonContractData))]
public void DeserializeBaseContractWorks_withMySecondJsonSerializerContext(string json, Type targetType)
{
var baseContract = JsonSerializer.Deserialize<BaseContractWithCustomDelimiter>(
json,
MySecondJsonSerializerContext.Default.Options
CreateMySecondJsonSerializerOptions()
);

Assert.NotNull(baseContract);
Expand All @@ -35,7 +52,7 @@ public Task SerializeBaseContractWorks_withMySecondJsonSerializerContext(int tes
{
var json = JsonSerializer.Serialize(
contract,
MySecondJsonSerializerContext.Default.Options
CreateMySecondJsonSerializerOptions()
);

var settings = new VerifySettings();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
myCustomDelimiter: LeafContractWithCustomDelimiter,
leafProperty: 2,
baseProperty: 1
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net8.0;net9.0;net10.0</TargetFrameworks>
<LangVersion>14.0</LangVersion>
<Nullable>enable</Nullable>
<ProjectName>Aviationexam.GeneratedJsonConverters.SourceGenerator.Target</ProjectName>
Expand Down Expand Up @@ -29,4 +29,9 @@
<ProjectReference Include="..\Aviationexam.GeneratedJsonConverters.SourceGenerator\Aviationexam.GeneratedJsonConverters.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="PolySharp" PrivateAssets="all" />
<PackageReference Include="System.Text.Json" PrivateAssets="all" />
</ItemGroup>

</Project>
Loading
Loading