如何使用jq从OpenAPI 3.0或Swagger 2.0 json中删除路径方法(基于x-* 属性)?

sg3maiej  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(113)

我是一个新的jq,这一个似乎不可能写。所以需要一些帮助,从jq大师在那里:
我有一个Open API规范文件,以版本3.0为例:
paths元素列出了endpoints,每个元素都有方法(getpost等)。
我需要一个jq过滤器来只选择那些用"x-visibility": "public"标记的方法,而忽略其他的一切。

到目前为止,我已经尝试过以下方法:

.paths |= map(select(.[]."x-visibility" == "public" | not))

显然,这行不通。

如有任何帮助/提示,我们将不胜感激!

下面是OpenAPI 3.0规范文件(去掉了额外的内容):

{
  "openapi": "3.0.1",
  "info": {
    "title": "API service",
    "description": "Exposing useful methods",
    "x-descriptionForPublicDocs": "This is a test description for public docs specifically",
    "contact": {
      "name": "Message me",
      "url": "https://message.me"
    },
    "version": "1.0"
  },
  "servers": [
    {
      "url": "http://localhost:9090",
      "description": "Generated server url"
    }
  ],
  "tags": [
    {
      "name": "AccountGroups"
    }
  ],
  "x-tags": [
    {
      "name": "TestResourceLists"
    }
  ],
  "paths": {
    "/crane/v1/resource_list/{id}": {
      "get": {
        "tags": [
          "ResourceLists"
        ],
        "x-tags": "TestResourceLists",
        "x-visibility": "public",
        "summary": "Get Resource List by ID",
        "description": "Returns the resource list that has the specified ID.",
        "operationId": "getById",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "ResourceList Id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json; charset=UTF-8": {
                "schema": {
                  "$ref": "#/components/schemas/ResourceListModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "4XX": {
            "description": "Client error",
            "content": {
              "application/json; charset=UTF-8": {
                "schema": {
                  "$ref": "#/components/schemas/SpringErrorResponse"
                }
              }
            }
          }
        }
      },
      "put": {
        "tags": [
          "ResourceLists"
        ],
        "x-visibility": "internal",
        "summary": "Update Resource List",
        "description": "Updates the resource list that has the specified ID.",
        "operationId": "update",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "ResourceList Id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "Model for Resource List",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ResourceListModel"
              },
              "examples": {
                "TAG": {
                  "description": "Tag ResourceList",
                  "value": {
                    "members": [
                      {
                        "string": "string"
                      }
                    ],
                    "name": "string",
                    "resourceListType": "TAG",
                    "description": "string"
                  }
                },
                "RESOURCE_GROUP": {
                  "description": "RESOURCE_GROUP ResourceList",
                  "value": {
                    "description": "string",
                    "members": [
                      "string",
                      "string"
                    ],
                    "name": "string",
                    "resourceListType": "RESOURCE_GROUP"
                  }
                },
                "COMPUTE_ACCESS_GROUP": {
                  "description": "COMPUTE_ACCESS_GROUP ResourceList",
                  "value": {
                    "description": "string",
                    "members": [
                      {
                        "appIDs": [
                          "*"
                        ],
                        "clusters": [
                          "*"
                        ],
                        "codeRepos": [
                          "*"
                        ],
                        "containers": [
                          "*"
                        ],
                        "functions": [
                          "*"
                        ],
                        "hosts": [
                          "*"
                        ],
                        "images": [
                          "*"
                        ],
                        "labels": [
                          "*"
                        ],
                        "namespaces": [
                          "*"
                        ]
                      }
                    ],
                    "name": "string",
                    "resourceListType": "COMPUTE_ACCESS_GROUP"
                  }
                }
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json; charset=UTF-8": {
                "schema": {
                  "$ref": "#/components/schemas/ResourceListModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "4XX": {
            "description": "Client error",
            "content": {
              "application/json; charset=UTF-8": {
                "schema": {
                  "$ref": "#/components/schemas/SpringErrorResponse"
                }
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "ResourceLists"
        ],
        "x-visibility": "internal",
        "summary": "Delete Resource List",
        "description": "Deletes the resource list that has the specified ID.",
        "operationId": "delete",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "ResourceList Id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "Successful operation"
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "4XX": {
            "description": "Client error",
            "content": {
              "*/*": {
                "schema": {
                  "$ref": "#/components/schemas/SpringErrorResponse"
                }
              }
            }
          }
        }
      }
    },
    "/crane/v1/account_group/{id}": {
      "get": {
        "tags": [
          "AccountGroups"
        ],
        "x-visibility": "public",
        "summary": "Get resource",
        "description": "Returns something",
        "operationId": "getById_1",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Account Group ID",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "includeAccountInfo",
            "in": "query",
            "description": "Include Cloud Account details",
            "required": false,
            "schema": {
              "type": "boolean"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AccountGroupModel"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "4XX": {
            "description": "Client error",
            "content": {
              "application/json; charset=UTF-8": {
                "schema": {
                  "$ref": "#/components/schemas/SpringErrorResponse"
                }
              }
            }
          }
        },
      },
      "put": {
        "tags": [
          "AccountGroups"
        ],
        "x-visibility": "internal",
        "summary": "Update resource",
        "description": "Update information",
        "operationId": "update_1",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "AccountGroup Id",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json; charset=UTF-8": {
              "schema": {
                "$ref": "#/components/schemas/AccountGroupModel"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful operation"
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "4XX": {
            "description": "Client error",
            "content": {
              "application/json; charset=UTF-8": {
                "schema": {
                  "$ref": "#/components/schemas/SpringErrorResponse"
                }
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "AccountGroups"
        ],
        "x-visibility": "internal",
        "summary": "Delete resource",
        "description": "Deletes the resource",
        "operationId": "delete_1",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "Delete resource",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful operation"
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "403": {
            "description": "Forbidden",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "*/*": {
                "schema": {
                  "type": "object"
                }
              }
            }
          },
          "4XX": {
            "description": "Client error",
            "content": {
              "*/*": {
                "schema": {
                  "$ref": "#/components/schemas/SpringErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  }
}
tkclm6bt

tkclm6bt1#

.paths |= map_values(
   if type == "object"
   then with_entries(select(
        if .value|objects|has("x-visibility") 
        then .value|.["x-visibility"] == "public" 
        else true end) )
   else . end)
643ylb08

643ylb082#

使用map_values将函数应用于对象的所有值。您有两个嵌套对象,因此需要调用map_values两次:

.paths |= map_values(map_values(select(."x-visibility" == "public")))

要去除空路径,请仅选择那些不是空数组的值:

.paths |= map_values(
    map_values(select(."x-visibility" == "public"))
    | select(. != {})
)

或者甚至可能是以下之一:
第一次

相关问题