66using System . Globalization ;
77using System . Linq ;
88using System . Linq . Expressions ;
9+ using System . Reflection ;
910using Microsoft . AspNetCore . JsonPatch . Adapters ;
1011using Microsoft . AspNetCore . JsonPatch . Converters ;
1112using Microsoft . AspNetCore . JsonPatch . Exceptions ;
1516using Newtonsoft . Json ;
1617using Newtonsoft . Json . Serialization ;
1718
19+ #if NET
20+ using Microsoft . AspNetCore . Builder ;
21+ using Microsoft . AspNetCore . Http . Metadata ;
22+ #endif
23+
1824namespace Microsoft . AspNetCore . JsonPatch ;
1925
2026// Implementation details: the purpose of this type of patch document is to ensure we can do type-checking
2127// when producing a JsonPatchDocument. However, we cannot send this "typed" over the wire, as that would require
2228// including type data in the JsonPatchDocument serialized as JSON (to allow for correct deserialization) - that's
2329// not according to RFC 6902, and would thus break cross-platform compatibility.
2430[ JsonConverter ( typeof ( TypedJsonPatchDocumentConverter ) ) ]
31+ #if NET
32+ public class JsonPatchDocument < TModel > : IJsonPatchDocument , IEndpointParameterMetadataProvider where TModel : class
33+ #else
2534public class JsonPatchDocument< TModel > : IJsonPatchDocument where TModel : class
35+ #endif
2636{
2737 public List < Operation < TModel > > Operations { get ; private set ; }
2838
@@ -31,7 +41,7 @@ public class JsonPatchDocument<TModel> : IJsonPatchDocument where TModel : class
3141
3242 public JsonPatchDocument ( )
3343 {
34- Operations = new List < Operation < TModel > > ( ) ;
44+ Operations = [ ] ;
3545 ContractResolver = new DefaultContractResolver ( ) ;
3646 }
3747
@@ -656,11 +666,22 @@ IList<Operation> IJsonPatchDocument.GetOperations()
656666 return allOps ;
657667 }
658668
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+
659680 // Internal for testing
660681 internal string GetPath < TProp > ( Expression < Func < TModel , TProp > > expr , string position )
661682 {
662683 var segments = GetPathSegments ( expr . Body ) ;
663- var path = String . Join ( "/" , segments ) ;
684+ var path = string . Join ( "/" , segments ) ;
664685 if ( position != null )
665686 {
666687 path += "/" + position ;
@@ -712,8 +733,7 @@ private List<string> GetPathSegments(Expression expr)
712733
713734 private string GetPropertyNameFromMemberExpression ( MemberExpression memberExpression )
714735 {
715- var jsonObjectContract = ContractResolver . ResolveContract ( memberExpression . Expression . Type ) as JsonObjectContract ;
716- if ( jsonObjectContract != null )
736+ if ( ContractResolver . ResolveContract ( memberExpression . Expression . Type ) is JsonObjectContract jsonObjectContract )
717737 {
718738 return jsonObjectContract . Properties
719739 . First ( jsonProperty => jsonProperty . UnderlyingName == memberExpression . Member . Name )
0 commit comments