Skip to content

Commit af12b0b

Browse files
fix: 🚑️ [JsonIgnore] not being respected
1 parent dd58180 commit af12b0b

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

JsonApiToolkit.Tests/Mapping/EntityMapperTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json.Serialization;
12
using JsonApiToolkit.Mapping;
23
using JsonApiToolkit.Tests.Models;
34

@@ -63,4 +64,46 @@ public void GetIdProperty_IdentifiesPrimaryId()
6364
Assert.NotNull(idProperty);
6465
Assert.Equal("Id", idProperty.Name);
6566
}
67+
68+
[Fact]
69+
public void GetAttributeProperties_ExcludesJsonIgnoreProperties()
70+
{
71+
var attributeProperties = EntityMapper.GetAttributeProperties(typeof(EntityWithJsonIgnore));
72+
var propertyNames = attributeProperties.Select(p => p.Name).ToList();
73+
74+
Assert.Contains("VisibleProperty", propertyNames);
75+
Assert.DoesNotContain("HiddenProperty", propertyNames);
76+
}
77+
78+
[Fact]
79+
public void GetRelationshipProperties_ExcludesJsonIgnoreProperties()
80+
{
81+
var relationshipProperties = EntityMapper.GetRelationshipProperties(
82+
typeof(EntityWithJsonIgnore)
83+
);
84+
var propertyNames = relationshipProperties.Select(p => p.Name).ToList();
85+
86+
Assert.Contains("VisibleRelation", propertyNames);
87+
Assert.DoesNotContain("HiddenRelation", propertyNames);
88+
}
89+
90+
private class EntityWithJsonIgnore
91+
{
92+
public int Id { get; set; }
93+
public string VisibleProperty { get; set; } = string.Empty;
94+
95+
[JsonIgnore]
96+
public string HiddenProperty { get; set; } = string.Empty;
97+
98+
public RelatedEntityForIgnoreTest? VisibleRelation { get; set; }
99+
100+
[JsonIgnore]
101+
public RelatedEntityForIgnoreTest? HiddenRelation { get; set; }
102+
}
103+
104+
private class RelatedEntityForIgnoreTest
105+
{
106+
public int Id { get; set; }
107+
public string Name { get; set; } = string.Empty;
108+
}
66109
}

JsonApiToolkit.Tests/Mapping/JsonApiMapperTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json.Serialization;
12
using JsonApiToolkit.Mapping;
23
using JsonApiToolkit.Models.Documents;
34
using JsonApiToolkit.Models.Metadata;
@@ -177,4 +178,36 @@ public void ToCollectionDocument_WithPagination_IncludesCorrectLinks()
177178
Assert.NotNull(document.Meta);
178179
Assert.True(document.Meta.ContainsKey("pagination"));
179180
}
181+
182+
[Fact]
183+
public void ToResourceObject_ExcludesJsonIgnoreProperties()
184+
{
185+
var entity = new EntityWithIgnoredProperties
186+
{
187+
Id = 1,
188+
VisibleName = "Visible",
189+
SecretPassword = "should-not-appear",
190+
InternalData = "should-not-appear-either",
191+
};
192+
193+
var resourceObject = JsonApiMapper.ToResourceObject(entity, "entities");
194+
195+
Assert.NotNull(resourceObject.Attributes);
196+
Assert.True(resourceObject.Attributes.ContainsKey("visibleName"));
197+
Assert.Equal("Visible", resourceObject.Attributes["visibleName"]);
198+
Assert.False(resourceObject.Attributes.ContainsKey("secretPassword"));
199+
Assert.False(resourceObject.Attributes.ContainsKey("internalData"));
200+
}
201+
202+
private class EntityWithIgnoredProperties
203+
{
204+
public int Id { get; set; }
205+
public string VisibleName { get; set; } = string.Empty;
206+
207+
[JsonIgnore]
208+
public string SecretPassword { get; set; } = string.Empty;
209+
210+
[JsonIgnore]
211+
public string InternalData { get; set; } = string.Empty;
212+
}
180213
}

JsonApiToolkit/Mapping/EntityMapper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using System.Collections.Concurrent;
33
using System.Reflection;
4+
using System.Text.Json.Serialization;
45
using JsonApiToolkit.Extensions;
56

67
namespace JsonApiToolkit.Mapping;
@@ -58,6 +59,7 @@ public static List<PropertyInfo> GetAttributeProperties(Type type)
5859
&& !relationshipNames.Contains(p.Name) // Exclude properties identified as relationships
5960
&& p.CanRead
6061
&& p.GetMethod?.IsPublic == true
62+
&& !HasJsonIgnoreAttribute(p) // Exclude properties marked with [JsonIgnore]
6163
)
6264
.ToList();
6365
}
@@ -79,6 +81,7 @@ public static List<PropertyInfo> GetRelationshipProperties(Type type)
7981
.Where(p =>
8082
p.CanRead
8183
&& p.GetMethod?.IsPublic == true
84+
&& !HasJsonIgnoreAttribute(p) // Exclude properties marked with [JsonIgnore]
8285
&& (
8386
(
8487
typeof(IEnumerable).IsAssignableFrom(p.PropertyType)
@@ -125,6 +128,14 @@ private static bool HasIdProperty(Type? type)
125128
return GetIdProperty(type) != null;
126129
}
127130

131+
/// <summary>
132+
/// Checks if a property has the JsonIgnore attribute.
133+
/// </summary>
134+
private static bool HasJsonIgnoreAttribute(PropertyInfo property)
135+
{
136+
return property.GetCustomAttribute<JsonIgnoreAttribute>() != null;
137+
}
138+
128139
/// <summary>
129140
/// Gets the element type of a collection.
130141
/// </summary>

0 commit comments

Comments
 (0)