33
44using System ;
55using System . Collections . Generic ;
6+ using System . Reflection ;
67using System . Text . Json ;
78using System . Text . Json . Serialization ;
9+ using Microsoft . AspNetCore . Builder ;
10+ using Microsoft . AspNetCore . Http . Metadata ;
811using Microsoft . AspNetCore . JsonPatch . SystemTextJson . Adapters ;
912using Microsoft . AspNetCore . JsonPatch . SystemTextJson . Converters ;
1013using Microsoft . AspNetCore . JsonPatch . SystemTextJson . Exceptions ;
@@ -18,7 +21,7 @@ namespace Microsoft.AspNetCore.JsonPatch.SystemTextJson;
1821// documents for cases where there's no class/DTO to work on. Typical use case: backend not built in
1922// .NET or architecture doesn't contain a shared DTO layer.
2023[ JsonConverter ( typeof ( JsonPatchDocumentConverter ) ) ]
21- public class JsonPatchDocument : IJsonPatchDocument
24+ public class JsonPatchDocument : IJsonPatchDocument , IEndpointParameterMetadataProvider
2225{
2326 public List < Operation > Operations { get ; private set ; }
2427
@@ -27,7 +30,7 @@ public class JsonPatchDocument : IJsonPatchDocument
2730
2831 public JsonPatchDocument ( )
2932 {
30- Operations = new List < Operation > ( ) ;
33+ Operations = [ ] ;
3134 SerializerOptions = JsonSerializerOptions . Default ;
3235 }
3336
@@ -205,17 +208,27 @@ IList<Operation> IJsonPatchDocument.GetOperations()
205208 {
206209 foreach ( var op in Operations )
207210 {
208- var untypedOp = new Operation ( ) ;
209-
210- untypedOp . op = op . op ;
211- untypedOp . value = op . value ;
212- untypedOp . path = op . path ;
213- untypedOp . from = op . from ;
211+ var untypedOp = new Operation
212+ {
213+ op = op . op ,
214+ value = op . value ,
215+ path = op . path ,
216+ from = op . from
217+ } ;
214218
215219 allOps . Add ( untypedOp ) ;
216220 }
217221 }
218222
219223 return allOps ;
220224 }
225+
226+ /// <inheritdoc/>
227+ static void IEndpointParameterMetadataProvider . PopulateMetadata ( ParameterInfo parameter , EndpointBuilder builder )
228+ {
229+ ArgumentNullException . ThrowIfNull ( parameter ) ;
230+ ArgumentNullException . ThrowIfNull ( builder ) ;
231+
232+ builder . Metadata . Add ( new AcceptsMetadata ( [ "application/json-patch+json" ] , parameter . ParameterType ) ) ;
233+ }
221234}
0 commit comments