File tree 2 files changed +41
-17
lines changed
src/JsonApiDotNetCore/Configuration
test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph
2 files changed +41
-17
lines changed Original file line number Diff line number Diff line change @@ -171,27 +171,22 @@ private IReadOnlyCollection<AttrAttribute> GetAttributes(Type resourceClrType)
171
171
172
172
foreach ( PropertyInfo property in resourceClrType . GetProperties ( ) )
173
173
{
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
-
190
174
var attribute = property . GetCustomAttribute < AttrAttribute > ( true ) ;
191
175
192
176
if ( attribute == null )
193
177
{
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
+ }
195
190
}
196
191
197
192
SetPublicName ( attribute , property ) ;
Original file line number Diff line number Diff line change @@ -289,6 +289,28 @@ public void Resolves_correct_type_for_lazy_loading_proxy()
289
289
resourceType . ClrType . Should ( ) . Be ( typeof ( ResourceOfInt32 ) ) ;
290
290
}
291
291
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
+
292
314
[ UsedImplicitly ( ImplicitUseTargetFlags . Members ) ]
293
315
private sealed class ResourceWithHasOneRelationship : Identifiable < int >
294
316
{
@@ -376,4 +398,11 @@ public class ResourceOfInt32 : Identifiable<int>
376
398
[ HasMany ]
377
399
public IList < ResourceOfInt32 > Children { get ; set ; } = new List < ResourceOfInt32 > ( ) ;
378
400
}
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
+ }
379
408
}
You can’t perform that action at this time.
0 commit comments