Skip to content

Commit

Permalink
handle RequiredAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller authored and zcsizmadia committed Apr 22, 2024
1 parent 1f01369 commit 6ce94eb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 1 addition & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<!-- Run "dotnet list package (dash,dash)outdated" to see the latest versions of each package.-->

<ItemGroup Label="Dependencies">
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" Condition="$(TargetFramework.StartsWith('netstandard'))" />
<PackageVersion Include="System.Text.Json" Version="8.0.3" Condition="$(TargetFramework.StartsWith('netstandard'))" />
</ItemGroup>

<ItemGroup Label="Build Dependencies">
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
Expand Down
13 changes: 12 additions & 1 deletion src/ZCS.DataContractResolver/DataContractResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -117,9 +118,19 @@ private static IEnumerable<JsonPropertyInfo> CreateDataMembers(JsonTypeInfo json

if (attr != null)
{
jsonPropertyInfo.IsRequired = attr.IsRequired;
jsonPropertyInfo.Order = attr.Order;
jsonPropertyInfo.ShouldSerialize = !attr.EmitDefaultValue ? ((_, obj) => !IsNullOrDefault(obj)) : null;
}

if (!jsonPropertyInfo.IsRequired)
{
var requiredAttr = memberInfo.GetCustomAttribute<RequiredAttribute>();
if (requiredAttr != null)
{
jsonPropertyInfo.IsRequired = true;
}
}

yield return jsonPropertyInfo;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ZCS.DataContractResolver/ZCS.DataContractResolver.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(ClassLibTargetFrameworks)</TargetFrameworks>
<LangVersion>latest</LangVersion>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Condition="$(TargetFramework.StartsWith('netstandard'))"/>
<PackageReference Include="System.ComponentModel.Annotations" Condition="$(TargetFramework.StartsWith('netstandard'))" />
<PackageReference Include="System.Text.Json" Condition="$(TargetFramework.StartsWith('netstandard'))" />
</ItemGroup>
</Project>
14 changes: 13 additions & 1 deletion test/ZCS.DataContractResolver.Tests/PortedNewtonsoftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ public void SerializeClassWithRequiredObject()
{
var options = new JsonSerializerOptions()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
TypeInfoResolver = System.Text.Json.Serialization.Metadata.DataContractResolver.Default,
};

Expand All @@ -522,6 +521,19 @@ public void SerializeClassWithRequiredObject()
Assert.Contains("AllowNullProperty", json);
}

[Fact]
public void SerializeClassWithoutRequiredObject()
{
var options = new JsonSerializerOptions()
{
TypeInfoResolver = System.Text.Json.Serialization.Metadata.DataContractResolver.Default,
};

const string json = "{}";

Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<RequiredObject>(json, options));
}

[Fact]
public void SerializeObject()
{
Expand Down

0 comments on commit 6ce94eb

Please sign in to comment.