忽略 JsonPatch 特定 属性

Ignore JsonPatch specific property

我目前正在想办法阻止我的 API 用户使用 change/update 特定的 属性。从我从网上收集到的信息来看,它根本不受支持,至少微软的实现是这样 Microsoft.AspNetCore.JsonPatch。除此之外,我还找到了 IObjectAdapter 接口,它允许我在 ApplyTo 方法中添加一些自定义逻辑。然而,这似乎是一种非常丑陋的方法。

显然我也可以使用 GraphQL,但是对于我真正需要它的那几次来说,这有点过分了。还有其他选择吗?

在 GitHub 上进行了一些挖掘之后,我找到了 Labradoratory 称为 JsonPath.Patchable 的 gem,它完全解决了我遇到的问题。虽然它确实打破了 DDD 模式,这并没有真正让我高兴,但它几乎是我们能达到的最好的。

更新

我刚刚创建了一个 nuget 包,它通过提供允许您传入允许更改的特定属性的扩展方法来解决这个问题。 示例:

public class DummyModel
{
    public int Id { get; set; }
    public string Value { get; set; }
}

var patchDocument = new JsonPatchDocument();
patchDocument.Replace("/Value", "newValue");

patchDocument.ApplyToWithRestrictions(dummy, "Value"); // Allows the Patch to only modify the Value property. This argument takes an array.

此包的源代码可用 here

您是否通过指定 class 序列化来尝试限制, 例如

public class UpdatableDummyModel
{
    public string Value { get; set; }
}

您的输入是

JsonPatchDocument<UpdatableDummyModel>