如何从一个OpenAPI文档提取一些API到另一个



我的应用程序为所有API端点生成一个JSON/YAML。然而,我有一个API网关,我只允许其中的一个子集。为此,我目前从第一个文档中手动选择API及其模型,并创建一个可以提供给API网关的新文档。

当涉及到嵌套模型模式时,这个手动过程需要花费大量时间并且过于复杂。

有没有一种方法可以使这个过程自动化,这样我选择的路径可以与它的模型一起提取到一个新的Open API文档中。

您可以使用openapi-filter。https://github.com/Mermade/openapi-filter

x-internal: true标记API,如下所示:

openapi: 3.0.0
info:
title: API
version: 1.0.0
paths:
/:
get:
x-internal: true
...

运行以下命令(其中source.yaml是您的输入文件(:

npx openapi-filter --inverse --valid --strip -- source.yaml target.yaml

解释

--inverse: to include the APIs marked (and not exclude them, which is the default filter).
--valid: to copy all the other associated objects (and not just the paths)
--strip: to remove the flags added manually

兼容性

适用于OpenAPI/Swagger 2.0和3.0.x以及AsyncAPI定义。

感谢@Helen

最新更新