@@ -8,58 +8,75 @@ namespace JsonApiDotNetCore.Models
88 public sealed class AttrAttribute : Attribute , IResourceField
99 {
1010 /// <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.
1212 /// </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- ///
1913 /// <example>
20- ///
2114 /// <code>
2215 /// public class Author : Identifiable
2316 /// {
2417 /// [Attr]
2518 /// public string Name { get; set; }
2619 /// }
2720 /// </code>
28- ///
2921 /// </example>
30- public AttrAttribute ( string publicName = null , bool isImmutable = false , bool isFilterable = true , bool isSortable = true )
22+ public AttrAttribute ( )
3123 {
32- PublicAttributeName = publicName ;
33- IsImmutable = isImmutable ;
34- IsFilterable = isFilterable ;
35- IsSortable = isSortable ;
3624 }
3725
38- string IResourceField . PropertyName => PropertyInfo . Name ;
39-
4026 /// <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.
4228 /// </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+ }
4443
4544 /// <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 .
4746 /// </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+ }
4961
5062 /// <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.
5464 /// </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 ;
5672
5773 /// <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.
6175 /// </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 ; }
6380
6481 /// <summary>
6582 /// The resource property that this attribute is declared on.
0 commit comments