Skip to content

Commit 4be0862

Browse files
Add IncludeAsAttributeAttribute to include IDs as attributes in JSON:API resource objects
1 parent 5452912 commit 4be0862

1 file changed

Lines changed: 40 additions & 25 deletions

File tree

JsonApiToolkit/Mapping/EntityMapper.cs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ private static readonly ConcurrentDictionary<
5656
);
5757
}
5858

59+
/// <summary>
60+
/// Tag to be used on Ids that should be included as attributes in a JSON:API resource object.
61+
/// </summary>
62+
/// <remarks>
63+
/// Overrides the default behavior of excluding ID properties from attribute mapping.
64+
/// </remarks>
65+
[AttributeUsage(AttributeTargets.Property)]
66+
public class IncludeAsAttributeAttribute : Attribute;
67+
5968
/// <summary>
6069
/// Identifies the properties that should be mapped as attributes in a JSON:API resource object.
6170
/// </summary>
@@ -90,16 +99,19 @@ public static List<PropertyInfo> GetAttributeProperties(Type type)
9099

91100
return t.GetProperties()
92101
.Where(p =>
93-
!p.Name.EndsWith("Id")
94-
&& p.Name != "Id"
95-
&& !relationshipNames.Contains(p.Name)
96-
&& p.CanRead
97-
&& p.GetMethod?.IsPublic == true
98-
&& (
99-
p.PropertyType == typeof(string)
100-
|| (
101-
!typeof(IEnumerable).IsAssignableFrom(p.PropertyType)
102-
|| p.PropertyType == typeof(string)
102+
p.GetCustomAttribute<IncludeAsAttributeAttribute>() != null
103+
|| (
104+
!p.Name.EndsWith("Id")
105+
&& p.Name != "Id"
106+
&& !relationshipNames.Contains(p.Name)
107+
&& p.CanRead
108+
&& p.GetMethod?.IsPublic == true
109+
&& (
110+
p.PropertyType == typeof(string)
111+
|| (
112+
!typeof(IEnumerable).IsAssignableFrom(p.PropertyType)
113+
|| p.PropertyType == typeof(string)
114+
)
103115
)
104116
)
105117
)
@@ -134,21 +146,24 @@ public static List<PropertyInfo> GetRelationshipProperties(Type type)
134146
{
135147
return t.GetProperties()
136148
.Where(p =>
137-
p.CanRead
138-
&& p.GetMethod?.IsPublic == true
139-
&& (
140-
(
141-
typeof(IEnumerable).IsAssignableFrom(p.PropertyType)
142-
&& p.PropertyType != typeof(string)
143-
)
144-
|| (
145-
!p.PropertyType.IsPrimitive
146-
&& !p.PropertyType.IsValueType
147-
&& p.PropertyType != typeof(string)
148-
&& p.PropertyType != typeof(DateTime)
149-
&& p.PropertyType != typeof(DateTime?)
150-
&& p.PropertyType != typeof(Guid)
151-
&& p.PropertyType != typeof(Guid?)
149+
p.GetCustomAttribute<IncludeAsAttributeAttribute>() != null
150+
|| (
151+
p.CanRead
152+
&& p.GetMethod?.IsPublic == true
153+
&& (
154+
(
155+
typeof(IEnumerable).IsAssignableFrom(p.PropertyType)
156+
&& p.PropertyType != typeof(string)
157+
)
158+
|| (
159+
!p.PropertyType.IsPrimitive
160+
&& !p.PropertyType.IsValueType
161+
&& p.PropertyType != typeof(string)
162+
&& p.PropertyType != typeof(DateTime)
163+
&& p.PropertyType != typeof(DateTime?)
164+
&& p.PropertyType != typeof(Guid)
165+
&& p.PropertyType != typeof(Guid?)
166+
)
152167
)
153168
)
154169
)

0 commit comments

Comments
 (0)