-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathJsonPatchDocument.cs
125 lines (115 loc) · 4.59 KB
/
JsonPatchDocument.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks;
using WebApiClientCore.Exceptions;
namespace WebApiClientCore.Parameters
{
/// <summary>
/// 表示将自身作为JsonPatch请求内容
/// </summary>
[DebuggerTypeProxy(typeof(DebugView))]
[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext.")]
public class JsonPatchDocument : IApiParameter, IChunkedable
{
private static readonly MediaTypeHeaderValue mediaTypeHeaderValue = new("application/json-patch+json");
private readonly List<object> operations = [];
/// <summary>
/// 获取或设置是否允许 chunked 传输
/// 默认为 true
/// </summary>
public bool AllowChunked { get; set; } = true;
/// <summary>
/// Add操作
/// </summary>
/// <param name="path">json路径</param>
/// <param name="value">值</param>
/// <exception cref="ArgumentNullException"></exception>
public void Add(string path, object? value)
{
if (string.IsNullOrEmpty(path) == true)
{
throw new ArgumentNullException(nameof(path));
}
this.operations.Add(new { op = "add", path, value });
}
/// <summary>
/// Remove操作
/// </summary>
/// <param name="path">json路径</param>
/// <exception cref="ArgumentNullException"></exception>
public void Remove(string path)
{
if (string.IsNullOrEmpty(path) == true)
{
throw new ArgumentNullException(nameof(path));
}
this.operations.Add(new { op = "remove", path });
}
/// <summary>
/// Replace操作
/// </summary>
/// <param name="path">json路径</param>
/// <param name="value">替换后的值</param>
/// <exception cref="ArgumentNullException"></exception>
public void Replace(string path, object? value)
{
if (string.IsNullOrEmpty(path) == true)
{
throw new ArgumentNullException(nameof(path));
}
this.operations.Add(new { op = "replace", path, value });
}
/// <summary>
/// 执行请求前
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
[UnconditionalSuppressMessage("Trimming", "IL3050:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public Task OnRequestAsync(ApiParameterContext context)
{
if (context.HttpContext.RequestMessage.Method != HttpMethod.Patch)
{
throw new ApiInvalidConfigException(Resx.required_PatchMethod);
}
var options = context.HttpContext.HttpApiOptions.JsonSerializeOptions;
if (this.AllowChunked)
{
context.HttpContext.RequestMessage.Content = JsonContent.Create(this.operations, this.operations.GetType(), mediaTypeHeaderValue, options);
}
else
{
context.HttpContext.RequestMessage.Content = new HttpContents.JsonPatchContent(this.operations, options);
}
return Task.CompletedTask;
}
/// <summary>
/// 调试视图
/// </summary>
private class DebugView
{
/// <summary>
/// 调试的目标
/// </summary>
private readonly JsonPatchDocument target;
/// <summary>
/// 查看的内容
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public List<object> Operations => this.target.operations;
/// <summary>
/// 调试视图
/// </summary>
/// <param name="target">查看的对象</param>
public DebugView(JsonPatchDocument target)
{
this.target = target;
}
}
}
}