6
6
using System . Globalization ;
7
7
using System . Linq ;
8
8
using System . Linq . Expressions ;
9
+ using System . Reflection ;
9
10
using Microsoft . AspNetCore . JsonPatch . Adapters ;
10
11
using Microsoft . AspNetCore . JsonPatch . Converters ;
11
12
using Microsoft . AspNetCore . JsonPatch . Exceptions ;
15
16
using Newtonsoft . Json ;
16
17
using Newtonsoft . Json . Serialization ;
17
18
19
+ #if NET
20
+ using Microsoft . AspNetCore . Builder ;
21
+ using Microsoft . AspNetCore . Http . Metadata ;
22
+ #endif
23
+
18
24
namespace Microsoft . AspNetCore . JsonPatch ;
19
25
20
26
// Implementation details: the purpose of this type of patch document is to ensure we can do type-checking
21
27
// when producing a JsonPatchDocument. However, we cannot send this "typed" over the wire, as that would require
22
28
// including type data in the JsonPatchDocument serialized as JSON (to allow for correct deserialization) - that's
23
29
// not according to RFC 6902, and would thus break cross-platform compatibility.
24
30
[ JsonConverter ( typeof ( TypedJsonPatchDocumentConverter ) ) ]
31
+ #if NET
32
+ public class JsonPatchDocument < TModel > : IJsonPatchDocument , IEndpointParameterMetadataProvider where TModel : class
33
+ #else
25
34
public class JsonPatchDocument< TModel > : IJsonPatchDocument where TModel : class
35
+ #endif
26
36
{
27
37
public List < Operation < TModel > > Operations { get ; private set ; }
28
38
@@ -31,7 +41,7 @@ public class JsonPatchDocument<TModel> : IJsonPatchDocument where TModel : class
31
41
32
42
public JsonPatchDocument ( )
33
43
{
34
- Operations = new List < Operation < TModel > > ( ) ;
44
+ Operations = [ ] ;
35
45
ContractResolver = new DefaultContractResolver ( ) ;
36
46
}
37
47
@@ -656,11 +666,22 @@ IList<Operation> IJsonPatchDocument.GetOperations()
656
666
return allOps ;
657
667
}
658
668
669
+ #if NET
670
+ /// <inheritdoc/>
671
+ static void IEndpointParameterMetadataProvider . PopulateMetadata ( ParameterInfo parameter , EndpointBuilder builder )
672
+ {
673
+ ArgumentNullException . ThrowIfNull ( parameter ) ;
674
+ ArgumentNullException . ThrowIfNull ( builder ) ;
675
+
676
+ builder . Metadata . Add ( new AcceptsMetadata ( [ "application/json-patch+json" ] , typeof ( TModel ) ) ) ;
677
+ }
678
+ #endif
679
+
659
680
// Internal for testing
660
681
internal string GetPath < TProp > ( Expression < Func < TModel , TProp > > expr , string position )
661
682
{
662
683
var segments = GetPathSegments ( expr . Body ) ;
663
- var path = String . Join ( "/" , segments ) ;
684
+ var path = string . Join ( "/" , segments ) ;
664
685
if ( position != null )
665
686
{
666
687
path += "/" + position ;
@@ -712,8 +733,7 @@ private List<string> GetPathSegments(Expression expr)
712
733
713
734
private string GetPropertyNameFromMemberExpression ( MemberExpression memberExpression )
714
735
{
715
- var jsonObjectContract = ContractResolver . ResolveContract ( memberExpression . Expression . Type ) as JsonObjectContract ;
716
- if ( jsonObjectContract != null )
736
+ if ( ContractResolver . ResolveContract ( memberExpression . Expression . Type ) is JsonObjectContract jsonObjectContract )
717
737
{
718
738
return jsonObjectContract . Properties
719
739
. First ( jsonProperty => jsonProperty . UnderlyingName == memberExpression . Member . Name )
0 commit comments