1

我正在尝试使用jq. 我想“识别”模式

"status": {
  "allOf": [
    {
      "$ref": "#/components/schemas/Status"
    },
    {
      "description": "Status"
    }
  ]
}

并将其更改为

"status": {
  "$ref": "#/components/schemas/Status"
}

我认为这并不难,但是当我尝试在一个更大的例子上做这件事时,我遇到了麻烦。让我们举个例子:

  {
    "components": {
      "schemas": {
        "ObjectStatusType": {
          "required": [
            "status",
            "type",
            "agreement"
          ],
          "type": "object",
          "properties": {
            "agreement": {
              "description": "An agreement",
              "maxLength": 5,
              "type": "string"
            },
            "status": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Status"
                },
                {
                  "description": "Status"
                }
              ]
            },
            "type": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Type"
                },
                {
                  "description": "Type"
                }
              ]
            }
          }
        },
        "BallType": {
          "required": [
            "colour",
            "type",
            "physics"
          ],
          "type": "object",
          "properties": {
            "colour": {
              "description": "Ball colour",
              "maxLength": 5,
              "type": "string"
            },
            "type": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Type"
                },
                {
                  "description": "Type"
                }
              ]
            },
            "physics": {
              "allOf": [
                {
                  "$ref": "#/components/schemas/Physics"
                },
                {
                  "description": "Physics"
                }
              ]
            }
          }
        }
      }
    }
  }

我想变成

  {
    "components": {
      "schemas": {
        "ObjectStatusType": {
          "required": [
            "status",
            "type",
            "agreement"
          ],
          "type": "object",
          "properties": {
            "agreement": {
              "description": "An agreement",
              "maxLength": 5,
              "type": "string"
            },
            "status": {
              "$ref": "#/components/schemas/Status"
            },
            "type": {
              "$ref": "#/components/schemas/Type"
            }
          }
        },
        "BallType": {
          "required": [
            "colour",
            "type",
            "physics"
          ],
          "type": "object",
          "properties": {
            "colour": {
              "description": "Ball colour",
              "maxLength": 5,
              "type": "string"
            },
            "type": {
              "$ref": "#/components/schemas/Type"
            },
            "physics": {
              "$ref": "#/components/schemas/Physics"
            }
          }
        }
      }
    }
  }

我已经尝试过表达(.components | .schemas | .[] | .properties // empty | .[] | select(.allOf // empty | .[] // empty | .description) | select(.allOf // empty | .[] // empty | ."$ref")) = { "$ref" : "what to put here" } ,这给了我

{
  "components": {
    "schemas": {
      "ObjectStatusType": {
        "required": [
          "status",
          "type",
          "agreement"
        ],
        "type": "object",
        "properties": {
          "agreement": {
            "description": "An agreement",
            "maxLength": 5,
            "type": "string"
          },
          "status": {
            "$ref": "what to put here"
          },
          "type": {
            "$ref": "what to put here"
          }
        }
      },
      "BallType": {
        "required": [
          "colour",
          "type",
          "physics"
        ],
        "type": "object",
        "properties": {
          "colour": {
            "description": "Ball colour",
            "maxLength": 5,
            "type": "string"
          },
          "type": {
            "$ref": "what to put here"
          },
          "physics": {
            "$ref": "what to put here"
          }
        }
      }
    }
  }
}

我需要一些对“子文档”的引用,而不是what to put here. 我试过变量,但它们只能是全局定义.的,右侧指的是整个文档,而不仅仅是被替换的部分。

我们将 YAML 用于 OpenAPI 规范,因此如果有一个同样适用的解决方案yq会更好。

4

1 回答 1

1

给定显示的输入,以下 jq 程序会产生预期的输出,并且似乎符合一般要求,并且应该很容易根据您的更详细要求进行修改:

walk(if type=="object" and (.allOf | (type == "array") and (length == 2))
     then {"$ref": .allOf[0]["$ref"]}
     else . end)

FYPI,“then”子句可以缩写为:

.allOf[0] | {"$ref"}
于 2021-03-19T08:09:01.273 回答