Skip to content

Commit 85908ef

Browse files
author
Bart Koelman
committed
Fixed: Override IIdentifiable.Id with custom capabilities no longer worked
1 parent c79da9e commit 85908ef

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,22 @@ private IReadOnlyCollection<AttrAttribute> GetAttributes(Type resourceClrType)
171171

172172
foreach (PropertyInfo property in resourceClrType.GetProperties())
173173
{
174-
// Although strictly not correct, 'id' is added to the list of attributes for convenience.
175-
// For example, it enables to filter on ID, without the need to special-case existing logic.
176-
// And when using sparse fields, it silently adds 'id' to the set of attributes to retrieve.
177-
if (property.Name == nameof(Identifiable<object>.Id))
178-
{
179-
var idAttr = new AttrAttribute
180-
{
181-
PublicName = FormatPropertyName(property),
182-
Property = property,
183-
Capabilities = _options.DefaultAttrCapabilities
184-
};
185-
186-
IncludeField(attributesByName, idAttr);
187-
continue;
188-
}
189-
190174
var attribute = property.GetCustomAttribute<AttrAttribute>(true);
191175

192176
if (attribute == null)
193177
{
194-
continue;
178+
if (property.Name == nameof(Identifiable<object>.Id))
179+
{
180+
// Although strictly not correct, 'id' is added to the list of attributes for convenience.
181+
// For example, it enables to filter on ID, without the need to special-case existing logic.
182+
// And when using sparse fieldsets, it silently adds 'id' to the set of attributes to retrieve.
183+
184+
attribute = new AttrAttribute();
185+
}
186+
else
187+
{
188+
continue;
189+
}
195190
}
196191

197192
SetPublicName(attribute, property);

test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ public void Resolves_correct_type_for_lazy_loading_proxy()
289289
resourceType.ClrType.Should().Be(typeof(ResourceOfInt32));
290290
}
291291

292+
[Fact]
293+
public void Can_override_capabilities_on_Id_property()
294+
{
295+
// Arrange
296+
var options = new JsonApiOptions
297+
{
298+
DefaultAttrCapabilities = AttrCapabilities.None
299+
};
300+
301+
var builder = new ResourceGraphBuilder(options, NullLoggerFactory.Instance);
302+
303+
// Act
304+
builder.Add<ResourceWithIdOverride, long>();
305+
306+
// Assert
307+
IResourceGraph resourceGraph = builder.Build();
308+
ResourceType resourceType = resourceGraph.GetResourceType<ResourceWithIdOverride>();
309+
310+
AttrAttribute idAttribute = resourceType.Attributes.Single(attr => attr.Property.Name == nameof(Identifiable<object>.Id));
311+
idAttribute.Capabilities.Should().Be(AttrCapabilities.AllowFilter);
312+
}
313+
292314
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
293315
private sealed class ResourceWithHasOneRelationship : Identifiable<int>
294316
{
@@ -376,4 +398,11 @@ public class ResourceOfInt32 : Identifiable<int>
376398
[HasMany]
377399
public IList<ResourceOfInt32> Children { get; set; } = new List<ResourceOfInt32>();
378400
}
401+
402+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
403+
public sealed class ResourceWithIdOverride : Identifiable<long>
404+
{
405+
[Attr(Capabilities = AttrCapabilities.AllowFilter)]
406+
public override long Id { get; set; }
407+
}
379408
}

0 commit comments

Comments
 (0)