我正在研究asp.net核心webAPi和EF核心,并希望实现"更新"操作(部分编辑实体).我搜索了正确的方法来处理它,并看到我应该使用jsonPatch.问题是我通过我的API暴露了DTO,如果我使用jsonPatch:
public AccountDto Patch(int id, [FromBody]JsonPatchDocument<AccountDto> patch)
Run Code Online (Sandbox Code Playgroud)
然后我需要在DTO上应用补丁,我不能在模型实体上应用它,而不创建新的实体.
我也读过Odata.Delta,但它仍然无法在asp.net核心上运行,而且 - 我认为它没有内置的解决方案来处理dto(我发现这个例子可以帮助当Odata for core能得到的)
那么,现在 - 我应该使用POST并在查询中发送带有更改属性列表的DTO(正如我在这里看到的那样),或者 - 有更优雅的解决方案吗?
谢谢!
我有一个像这样的 json 对象:
{
"content" : [
{
"id" : 54
"foo" : "bar"
},
{
"id" : 43
"foo" : "bar"
},
{
"id" : 76
"foo" : "bar"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果我想向内容数组添加多个项目(顺序无关紧要),我可以使用带有单行/操作的 json 补丁添加到它吗?
{ "op": "add", "path": "/content/-", "value": [
{
"id" : 34
"foo" : "bar"
},
{
"id" : 23
"foo" : "bar"
},
{
"id" : 87
"foo" : "bar"
}
]
}
Run Code Online (Sandbox Code Playgroud)
或者我是否必须为每个要添加的对象添加一行?
编辑:要清楚我想追加,而不是替换内容。
有没有人找到一种使用数据注释来防止特定属性在 json 补丁文档中更新的好方法。
模型:
public class Entity
{
[DoNotAllowPatchUpdate]
public string Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public string Action { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
逻辑:
var patchDoc = new JsonPatchDocument<Entity>();
patchDoc.Replace(o => o.Name, "Foo");
//Prevent this from being applied
patchDoc.Replace(o => o.Id, "213");
patchDoc.ApplyTo(Entity);
Run Code Online (Sandbox Code Playgroud)
逻辑代码只是一个示例,说明补丁文档可能来自客户端,只是在 C# 中生成以进行快速测试
在我的春季启动服务中,我使用https://github.com/java-json-tools/json-patch来处理PATCH请求.
一切似乎是确定除的方式来避免修改状物品ID的,CREATION_TIME等不可改变的领域我都发现了类似的问题在Github https://github.com/java-json-tools/json-patch/issues/21为我找不到合适的例子.
这个博客似乎提供了一些有趣的解决方案,用于在node.js中使用解决方案来验证JSON补丁请求.很高兴知道JAVA中是否有类似的东西.
参考http://tools.ietf.org/html/rfc6902#appendix-A.14:
A.14. ~ 转义排序
示例目标 JSON 文档:
Run Code Online (Sandbox Code Playgroud){ "/": 9, "~1": 10 }一个 JSON 补丁文档:
Run Code Online (Sandbox Code Playgroud)[ {"op": "test", "path": "/~01", "value": 10} ]生成的 JSON 文档:
Run Code Online (Sandbox Code Playgroud){ "/": 9, "~1": 10 }
我正在编写这个 RFC 的一个实现,我一直坚持这个。这是要达到什么目的,它应该如何工作?
假设第一部分的答案是“允许引用包含 /s 的 json 键名”,你会怎么做?
嗨,我正在关注这个文档https://github.com/kubernetes/kubernetes/blob/master/docs/devel/api-conventions.md#strategic-merge-patch forstrategic-merge-patch 使用部分更新 JSON 对象补丁 REST API。该文档说它可以添加或删除对象,但我已经尝试过,每当我向现有 JSON 添加新对象时,它只是替换它而不是添加新对象。我正在尝试修改 OpenShift 3.2 中的 pod 定义。任何人都可以帮助我它是如何工作的,可能是例子。我还需要使用删除操作,我可以在其中按名称删除值。
我正在尝试以编程方式在两个工作项之间添加父子关系。我正在使用 Microsoft Team Foundation 和 Visual Studio Services 库来导出和导入 TFS 2015 和 VSTS 积压对象。
https://docs.microsoft.com/en-us/vsts/integrate/concepts/dotnet-client-libraries
https://www.visualstudio.com/en-us/docs/integrate/api/wit/samples#migrating-work-items
我已经通过获取到我的服务器的 VssConnection 并获取 WorkItemTrackingHttpClient 来执行 Wiql 查询并创建工作项。我还有一个查询来识别目标工作项的父项。
我无法弄清楚的是如何添加子工作项与其父项之间的链接。我不知道添加父项的正确 JsonPatchDocument 项目路径,或者现有 WorkItem 上的正确属性或方法以使用父链接更新它。有没有人有关于使用这些库向工作项添加父关系的文档链接或特定信息?
以下是上下文的一些代码摘录:
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
// ...
var sourceConnection = new VssConnection(new Uri(_sourceTsUrl), new VssClientCredentials());
var targetConnection = new VssConnection(new Uri(_targetTsUrl), new VssClientCredentials());
var sourceClient = sourceConnection.GetClient<WorkItemTrackingHttpClient>();
var targetClient = targetConnection.GetClient<WorkItemTrackingHttpClient>();
// ...
var …Run Code Online (Sandbox Code Playgroud) 我有 WebAPI (.NET Core) 并用于FluentValidator验证模型,包括更新。我使用 PATCH 动词并有以下方法:
public IActionResult Update(int id, [FromBody] JsonPatchDocument<TollUpdateAPI> jsonPatchDocument)
{
Run Code Online (Sandbox Code Playgroud)
另外,我有一validator堂课:
public class TollUpdateFluentValidator : AbstractValidator<TollUpdateAPI>
{
public TollUpdateFluentValidator ()
{
RuleFor(d => d.Date)
.NotNull().WithMessage("Date is required");
RuleFor(d => d.DriverId)
.GreaterThan(0).WithMessage("Invalid DriverId");
RuleFor(d => d.Amount)
.NotNull().WithMessage("Amount is required");
RuleFor(d => d.Amount)
.GreaterThanOrEqualTo(0).WithMessage("Invalid Amount");
}
}
Run Code Online (Sandbox Code Playgroud)
并将其映射到类validator中Startup:
services.AddTransient<IValidator<TollUpdateAPI>, TollUpdateFluentValidator>();
Run Code Online (Sandbox Code Playgroud)
但它不起作用。如何编写FluentValidator对我的任务有效的内容?
这个可以换吗
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: elastic-operator
labels:
argocd.application.type: "system"
spec:
ignoreDifferences:
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/0/clientConfig/caBundle
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/1/clientConfig/caBundle
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/2/clientConfig/caBundle
Run Code Online (Sandbox Code Playgroud)
对于使用“通配符”的东西?以下是我正在寻找的“非工作”示例:
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: elastic-operator
labels:
argocd.application.type: "system"
spec:
ignoreDifferences:
- group: admissionregistration.k8s.io
kind: ValidatingWebhookConfiguration
jsonPointers:
- /webhooks/[*]/clientConfig/caBundle
Run Code Online (Sandbox Code Playgroud)
我无法在他们的文档中找到任何说明这是可能的或相反的内容。iehttps://argoproj.github.io/argo-cd/user-guide/diffing/ 似乎正在使用“json-patch”,但阅读了一些 RFC,我也找不到有关通配符的任何信息。
谢谢!
json-patch ×10
json ×4
rest ×4
c# ×3
patch ×2
.net-core ×1
argocd ×1
arrays ×1
asp.net-core ×1
azure-devops ×1
http ×1
jsonpath ×1
jsonpointer ×1
kubelet ×1
kubernetes ×1
rfc6902 ×1
spring-rest ×1
tfs ×1