Skip to content

Commit 240e64a

Browse files
Fix SystemProperties serialization to ignore null values (#380)
* Fix SystemProperties serialization to ignore null values * Remove extra test methods * chore: bump version to 8.4.2 --------- Co-authored-by: ethan-ozelius-contentful <ethan.ozelius@contentful.com>
1 parent 20e36a1 commit 240e64a

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## Version [8.4.2]
7+
- Remove extra test methods
8+
- Fix SystemProperties serialization to ignore null values
9+
610
## Version [8.4.1]
711

812
- Add version header to UpdateRole method to fix ContentfulException

Contentful.AspNetCore/Contentful.AspNetCore.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Description>Official .NET SDK for the Contentful Content Delivery and Management API for ASP.NET core.</Description>
44
<PackageId>contentful.aspnetcore</PackageId>
55
<NeutralLanguage>en-US</NeutralLanguage>
6-
<VersionPrefix>8.4.1</VersionPrefix>
6+
<VersionPrefix>8.4.2</VersionPrefix>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<Authors>Contentful</Authors>
99
<Copyright>Contentful GmbH.</Copyright>
@@ -13,10 +13,10 @@
1313
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
1414
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1515
<RepositoryType>git</RepositoryType>
16-
<Version>8.4.1</Version>
17-
<AssemblyVersion>8.4.1.0</AssemblyVersion>
16+
<Version>8.4.2</Version>
17+
<AssemblyVersion>8.4.2.0</AssemblyVersion>
1818
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
19-
<FileVersion>8.4.1.0</FileVersion>
19+
<FileVersion>8.4.2.0</FileVersion>
2020
</PropertyGroup>
2121
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
2222
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
@@ -25,7 +25,7 @@
2525
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
2626
</PropertyGroup>
2727
<ItemGroup>
28-
<PackageReference Include="contentful.csharp" Version="8.4.1" />
28+
<PackageReference Include="contentful.csharp" Version="8.4.2" />
2929
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
3030
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
3131
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0" />

Contentful.Core.Tests/Extensions/JsonConversionExtensionsTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Contentful.Core.Extensions;
2+
using Contentful.Core.Models;
23
using Newtonsoft.Json.Linq;
34
using System;
45
using System.Collections.Generic;
@@ -134,6 +135,34 @@ public void JsonConversionExtensionsConvertsEmargoedAssetBodyCorrectly()
134135

135136
}
136137

138+
[Fact]
139+
public void SystemPropertiesSerializationIgnoresNullValues()
140+
{
141+
// Arrange
142+
var systemProperties = new SystemProperties
143+
{
144+
Id = "test-asset-id",
145+
Type = "Link",
146+
LinkType = "Asset"
147+
// All other properties are null and should be ignored
148+
};
149+
150+
// Act
151+
var convertedString = systemProperties.ConvertObjectToJsonString();
152+
153+
// Assert
154+
Assert.Contains("\"id\":\"test-asset-id\"", convertedString);
155+
Assert.Contains("\"type\":\"Link\"", convertedString);
156+
Assert.Contains("\"linkType\":\"Asset\"", convertedString);
157+
// Should not contain any null values
158+
Assert.DoesNotContain("\"archivedAt\":null", convertedString);
159+
Assert.DoesNotContain("\"createdAt\":null", convertedString);
160+
Assert.DoesNotContain("\"updatedAt\":null", convertedString);
161+
Assert.DoesNotContain("\"revision\":null", convertedString);
162+
}
163+
164+
165+
137166
[Theory]
138167
[InlineData(null, null)]
139168
[InlineData(13, 13)]

Contentful.Core/Contentful.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PackageId>contentful.csharp</PackageId>
55
<AssemblyTitle>contentful.net</AssemblyTitle>
66
<NeutralLanguage>en-US</NeutralLanguage>
7-
<VersionPrefix>8.4.1</VersionPrefix>
7+
<VersionPrefix>8.4.2</VersionPrefix>
88
<TargetFramework>netstandard2.0</TargetFramework>
99
<Authors>Contentful</Authors>
1010
<Copyright>Contentful GmbH.</Copyright>
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System;
2-
using Contentful.Core.Models.Management;
3-
4-
namespace Contentful.Core.Models;
5-
1+
using System;
2+
using Contentful.Core.Models.Management;
3+
using Newtonsoft.Json;
4+
5+
namespace Contentful.Core.Models;
6+
67
public class BaseSystemProperties
78
{
89
/// <summary>
@@ -13,6 +14,7 @@ public class BaseSystemProperties
1314
/// <summary>
1415
/// The type of link. Will be null for non link types.
1516
/// </summary>
17+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
1618
public string LinkType { get; set; }
1719

1820
/// <summary>
@@ -23,35 +25,42 @@ public class BaseSystemProperties
2325
/// <summary>
2426
/// The link to the environment of the resource.
2527
/// </summary>
26-
public ContentfulEnvironment Environment { get; set; }
28+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
29+
public ContentfulEnvironment Environment { get; set; }
2730

2831
/// <summary>
2932
/// The link to the space of the resource.
3033
/// </summary>
34+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
3135
public Space Space { get; set; }
3236

3337
/// <summary>
3438
/// The date and time the resource was created. Will be null when not applicable, e.g. for arrays.
3539
/// </summary>
40+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
3641
public DateTime? CreatedAt { get; set; }
3742

3843
/// <summary>
3944
/// The link to the user that created this content. Will only be present for management API call.
4045
/// </summary>
46+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
4147
public User CreatedBy { get; set; }
4248

4349
/// <summary>
4450
/// The date and time the resource was last updated. Will be null when not applicable or when the resource has never been updated.
4551
/// </summary>
52+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
4653
public DateTime? UpdatedAt { get; set; }
4754

4855
/// <summary>
4956
/// The link to the user that last updated this content. Will only be present for management API call.
5057
/// </summary>
58+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
5159
public User UpdatedBy { get; set; }
5260

5361
/// <summary>
5462
/// The current version of the resource. Will only be present for management API calls.
5563
/// </summary>
64+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
5665
public int? Version { get; set; }
5766
}

Contentful.Core/Models/SystemProperties.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Contentful.Core.Models.Management;
2+
using Newtonsoft.Json;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -14,81 +15,97 @@ public class SystemProperties : BaseSystemProperties
1415
/// <summary>
1516
/// The published version of the resource. Will be null for non-versioned types.
1617
/// </summary>
18+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
1719
public int? Revision { get; set; }
1820

1921
/// <summary>
2022
/// The date and time the resource was deleted. This field will only be present for <seealso cref="SyncResult"/> deleted items.
2123
/// </summary>
24+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2225
public DateTime? DeletedAt { get; set; }
2326

2427
/// <summary>
2528
/// The locale of the resource. Will only have a value for <seealso cref="Asset"/> and <seealso cref="Entry{T}"/> resource types.
2629
/// </summary>
30+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2731
public string Locale { get; set; }
2832

2933
/// <summary>
3034
/// The <seealso cref="ContentType"/> of the resource. Only applicable for <seealso cref="Entry{T}"/> resource types.
3135
/// </summary>
36+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
3237
public ContentType ContentType { get; set; }
3338

3439
/// <summary>
3540
/// The number of times the resource has been published.
3641
/// </summary>
42+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
3743
public int? PublishedCounter { get; set; }
3844

3945
/// <summary>
4046
/// The published version of the resource. Will only be present for management API calls.
4147
/// </summary>
48+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
4249
public int? PublishedVersion { get; set; }
4350

4451
/// <summary>
4552
/// The user that published the resource. Will only be present for management API calls.
4653
/// </summary>
54+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
4755
public User PublishedBy { get; set; }
4856

4957
/// <summary>
5058
/// When the resource was last published. Will only be present for management API calls.
5159
/// </summary>
60+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
5261
public DateTime? PublishedAt { get; set; }
5362

5463
/// <summary>
5564
/// The number of times the resource has been published. Will only be present for management API calls.
5665
/// </summary>
66+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
5767
public int? PublishCounter { get; set; }
5868

5969
/// <summary>
6070
/// When the resource was first published. Will only be present for management API calls.
6171
/// </summary>
72+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
6273
public DateTime? FirstPublishedAt { get; set; }
6374

6475
/// <summary>
6576
/// The date and time the resource was archived. Will only be present for management API calls.
6677
/// </summary>
78+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
6779
public DateTime? ArchivedAt { get; set; }
6880

6981
/// <summary>
7082
/// The version that is currently archived. Will only be present for management API calls.
7183
/// </summary>
84+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
7285
public int? ArchivedVersion { get; set; }
7386

7487
/// <summary>
7588
/// The link to the user that last archived this content. Will only be present for management API call.
7689
/// </summary>
90+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
7791
public User ArchivedBy { get; set; }
7892

7993
/// <summary>
8094
/// The organization the resource links to. Will only be present for certain management API calls.
8195
/// </summary>
96+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
8297
public Organization Organization { get; set; }
8398

8499
/// <summary>
85100
/// The usage period the resource links to. Will only be present for certain management API calls.
86101
/// </summary>
87-
public UsagePeriod UsagePeriod { get; set; }
102+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
103+
public UsagePeriod UsagePeriod { get; set; }
88104

89105
/// <summary>
90106
/// The link to the status that the current object had. Used only for resources that have a status.
91107
/// </summary>
108+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
92109
public Status Status { get; set; }
93110
}
94111
}

0 commit comments

Comments
 (0)