@@ -8,58 +8,75 @@ namespace JsonApiDotNetCore.Models
8
8
public sealed class AttrAttribute : Attribute , IResourceField
9
9
{
10
10
/// <summary>
11
- /// Defines a public attribute exposed by the API
11
+ /// Exposes a resource property as a json:api attribute using the configured casing convention and capabilities.
12
12
/// </summary>
13
- ///
14
- /// <param name="publicName">How this attribute is exposed through the API</param>
15
- /// <param name="isImmutable">Prevent PATCH requests from updating the value</param>
16
- /// <param name="isFilterable">Prevent filters on this attribute</param>
17
- /// <param name="isSortable">Prevent this attribute from being sorted by</param>
18
- ///
19
13
/// <example>
20
- ///
21
14
/// <code>
22
15
/// public class Author : Identifiable
23
16
/// {
24
17
/// [Attr]
25
18
/// public string Name { get; set; }
26
19
/// }
27
20
/// </code>
28
- ///
29
21
/// </example>
30
- public AttrAttribute ( string publicName = null , bool isImmutable = false , bool isFilterable = true , bool isSortable = true )
22
+ public AttrAttribute ( )
31
23
{
32
- PublicAttributeName = publicName ;
33
- IsImmutable = isImmutable ;
34
- IsFilterable = isFilterable ;
35
- IsSortable = isSortable ;
36
24
}
37
25
38
- string IResourceField . PropertyName => PropertyInfo . Name ;
39
-
40
26
/// <summary>
41
- /// How this attribute is exposed through the API
27
+ /// Exposes a resource property as a json:api attribute with an explicit name, using configured capabilities.
42
28
/// </summary>
43
- public string PublicAttributeName { get ; internal set ; }
29
+ public AttrAttribute ( string publicName )
30
+ {
31
+ if ( publicName == null )
32
+ {
33
+ throw new ArgumentNullException ( nameof ( publicName ) ) ;
34
+ }
35
+
36
+ if ( string . IsNullOrWhiteSpace ( publicName ) )
37
+ {
38
+ throw new ArgumentException ( "Exposed name cannot be empty or contain only whitespace." , nameof ( publicName ) ) ;
39
+ }
40
+
41
+ PublicAttributeName = publicName ;
42
+ }
44
43
45
44
/// <summary>
46
- /// Prevents PATCH requests from updating the value .
45
+ /// Exposes a resource property as a json:api attribute using the configured casing convention and an explicit set of capabilities .
47
46
/// </summary>
48
- public bool IsImmutable { get ; }
47
+ /// <example>
48
+ /// <code>
49
+ /// public class Author : Identifiable
50
+ /// {
51
+ /// [Attr(AttrCapabilities.AllowFilter | AttrCapabilities.AllowSort)]
52
+ /// public string Name { get; set; }
53
+ /// }
54
+ /// </code>
55
+ /// </example>
56
+ public AttrAttribute ( AttrCapabilities capabilities )
57
+ {
58
+ HasExplicitCapabilities = true ;
59
+ Capabilities = capabilities ;
60
+ }
49
61
50
62
/// <summary>
51
- /// Whether or not this attribute can be filtered on via a query string filters.
52
- /// Attempts to filter on an attribute with `IsFilterable == false` will return
53
- /// an HTTP 400 response.
63
+ /// Exposes a resource property as a json:api attribute with an explicit name and capabilities.
54
64
/// </summary>
55
- public bool IsFilterable { get ; }
65
+ public AttrAttribute ( string publicName , AttrCapabilities capabilities ) : this ( publicName )
66
+ {
67
+ HasExplicitCapabilities = true ;
68
+ Capabilities = capabilities ;
69
+ }
70
+
71
+ string IResourceField . PropertyName => PropertyInfo . Name ;
56
72
57
73
/// <summary>
58
- /// Whether or not this attribute can be sorted on via a query string sort.
59
- /// Attempts to filter on an attribute with `IsSortable == false` will return
60
- /// an HTTP 400 response.
74
+ /// The publicly exposed name of this json:api attribute.
61
75
/// </summary>
62
- public bool IsSortable { get ; }
76
+ public string PublicAttributeName { get ; internal set ; }
77
+
78
+ internal bool HasExplicitCapabilities { get ; }
79
+ public AttrCapabilities Capabilities { get ; internal set ; }
63
80
64
81
/// <summary>
65
82
/// The resource property that this attribute is declared on.
0 commit comments