@apidevtools/swagger-cli

中文文档
4.0.4发布于 2020/07/19MITCommonJS近 30 天下载 133.9万

Swagger 2.0 and OpenAPI 3.0 command-line tool

此软件包已被标记为弃用

This package has been abandoned. Please switch to using the actively maintained @redocly/cli

原始使用文档
文档目录9 节
  1. Features
  2. Related Projects
  3. Installation
  4. Usage
  5. Validate an API
  6. Combine Multiple Files
  7. Contributing
  8. License
  9. Big Thanks To

Swagger/OpenAPI CLI

Cross-Platform Compatibility Build Status

Dependencies Coverage Status

npm License Buy us a tree

Features

  • Validate Swagger/OpenAPI files in JSON or YAML format
  • Supports multi-file API definitions via $ref pointers
  • Bundle multiple Swagger/OpenAPI files into one combined file

Installation

Install using npm:

npm install -g @apidevtools/swagger-cli

Usage

swagger-cli <command> [options] <file>

Commands:
    validate                Validates an API definition in Swagger 2.0 or OpenAPI 3.0 format

    bundle                  Bundles a multi-file API definition into a single file

Options:
    -h, --help              Show help for any command
    -v, --version           Output the CLI version number
    -d, --debug [filter]    Show debug output, optionally filtered (e.g. "*", "swagger:*", etc.)

Validate an API

The swagger-cli validate command will validate your Swagger/OpenAPI definition against the Swagger 2.0 schema or OpenAPI 3.0 Schema. It also performs additional validations against the specification, which will catch some things that aren't covered by the schema, such as duplicate parameters, invalid MIME types, etc.

The command will exit with a non-zero code if the API is invalid.

swagger-cli validate [options] <file>

Options:
    --no-schema             Do NOT validate against the Swagger/OpenAPI JSON schema

    --no-spec               Do NOT validate against the Swagger/OpenAPI specification

Git pre-commit hook

There is a useful Python tool called pre-commit that can be used to execute a wide suite of pre-commit checks. The swagger-cli validate command can be integrated as part of a git pre-commit hook by adding the following configuration to the repos entry of an existing .pre-commit-config.yaml file.

-   repo: https://github.com/APIDevTools/swagger-cli
    rev: v2.2.1
    hooks:
    - id: swagger-validation
      args: ["validate", "<path to root swagger>"]

The intention is to point to single root swagger that references multiple swagger definitions. The above hook will execute the swagger-cli validation against the root swagger anytime that a file matching the pattern .*swagger.*\.(json|yaml|yml) is modified. Any failures in this validation will prevent the git commit from being processed.

Combine Multiple Files

The Swagger and OpenAPI specs allows you to split your API definition across multiple files using $ref pointers to reference each file. You can use the swagger-cli bundle command to combine all of those referenced files into a single file, which is useful for distribution or interoperation with other tools.

By default, the swagger-cli bundle command tries to keep the output file size as small as possible, by only embedding each referenced file once. If the same file is referenced multiple times, then any subsequent references are simply modified to point to the single inlined copy of the file. If you want to produce a bundled file without any $ref pointers, then add the --dereference option. This will result in a larger file size, since multiple references to the same file will result in that file being embedded multiple times.

If you don't specify the --outfile option, then the bundled API will be written to stdout, which means you can pipe it to other commands.

The result of this method by default is written as JSON. It can be changed to YAML with the --type option, by passing the yaml value.

swagger-cli bundle [options] <file>

Options:
    -o, --outfile <file>        The output file

    -r, --dereference           Fully dereference all $ref pointers

    -f, --format <spaces>       Formats the output using the given number of spaces
                                (the default is 2 spaces)

    -t, --type <filetype>       Defines the output file type. The valid values are: json, yaml
                                (the default is JSON)

    -w, --wrap <column>         Set the line length for YAML strings
                                (the default is no wrapping)

Contributing

I welcome any contributions, enhancements, and bug-fixes. Open an issue on GitHub and submit a pull request.

Building/Testing

To build/test the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/APIDevTools/swagger-cli.git

  2. Install dependencies
    npm install

  3. Run the tests
    npm test

License

Swagger CLI is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

Travis CI SauceLabs Coveralls

版本与发布记录

@apidevtools/swagger-cli 共发布 6 个版本,首次发布于 2020/03/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.4latest2020/07/196 年前
4.0.32020/05/256 年前
4.0.22020/03/166 年前
4.0.12020/03/156 年前
4.0.02020/03/146 年前
3.0.12020/03/146 年前

关于 @apidevtools/swagger-cli 的常见问题

@apidevtools/swagger-cli 是什么?

Swagger 2.0 and OpenAPI 3.0 command-line tool注意:该包已被作者标记为弃用——This package has been abandoned. Please switch to using the actively maintained @redocly/cli

如何安装 @apidevtools/swagger-cli?

在项目目录运行 npm install @apidevtools/swagger-cli,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 swagger-cli,可以用 npx swagger-cli 直接运行。

@apidevtools/swagger-cli 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/apidevtools__swagger-cli 是否存在。

@apidevtools/swagger-cli 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@apidevtools/swagger-cli 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apidevtools/swagger-cli 可以商用吗?

@apidevtools/swagger-cli 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apidevtools/swagger-cli 的下载量有多少?

根据 npm 官方下载统计,@apidevtools/swagger-cli 在 2026/10/06 – 2026/11/04 这 30 天内被下载 1,338,928 次,日均约 44,631 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apidevtools/swagger-cli 还在维护吗?

最新版本 4.0.4 发布于 2020/07/19(6 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/14。作者已将其标记为弃用,新项目建议寻找替代方案。