是否有更多在模式的多个级别使用 readOnly 来阐明语义的示例?

Are there more examples of readOnly being used at several levels of a schema to clarify the semantics?

我一直在我的模式中使用 readOnly 关键字,我才意识到我只是在编造我自己的语义。现在我正在清理我的一堆设计并尝试验证我是否按预期使用了这个注释。 validation spec 是我提出这个问题的依据,但我想了解更多示例使用场景。

我举三个例子。在第一个示例中,我的意思是说整个资源都是只读的。任何级别都不能突变。

{
    "type": "object",
    "readOnly": true,
    "properties": {
        "name": {
            "type": "string",
        },
        "members": {
            "type": "object",
            "properties": {
                "member1": { "type" : "string" },
                "member2": { "type" : "string" }
            }
        }
    }
}

我不认为这有太大争议。但最初,我自己的心智模型是顶层的 readOnly 意味着您不能用新资源替换此资源。服务器会阻止这种情况。但是内部成员仍然是可变的。所以我在 name 子模式和每个成员子模式中添加了 readOnly。我认为删除所有这些是正确的。 (我的心智模型可能大致基于我如何解释 JavaScript 中的 const 变量。如果我的 const var 是一个对象,我不能更改变量的值,但我可以改变它的成员甚至添加成员到它。)

在第二个示例中,我将 readOnly 完全排除在模式之外。因此,认为这意味着资源中的任何内容都是可变的并没有太大争议。

{
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
        },
        "members": {
            "type": "object",
            "properties": {
                "member1": { "type" : "string" },
                "member2": { "type" : "string" }
            }
        }
    }
}

第三个例子,我想混搭

{
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "readOnly": true
        },
        "members": {
            "type": "object",
            "properties": {
                "member1": { "type" : "string", "readOnly": true },
                "member2": { "type" : "string", "readOnly": true },
                "member3": { "type" : "string"},
                "member4": { "type" : "string"}
            }
        }
    }
}

在此示例中,名称、member1 和 member2 是不可变的。 member3和member4可以修改

那么问题来了,我对readOnly的理解有什么不对吗?

您链接的规范为 readOnly...

定义了以下内容

If "readOnly" has a value of boolean true, it indicates that the value of the instance is managed exclusively by the owning authority, and attempts by an application to modify the value of this property are expected to be ignored or rejected by that owning authority.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-02#section-9.4

如果采用value的JSON定义的含义,就是键右边的位后面跟冒号。因此,我会将其视为值的任何部分。

OpenAPI 规范仅真正定义 readOnly 适用于个别属性。