How to filter generated Swagger JSON (yaml)

被刻印的时光 ゝ 提交于 2019-12-02 20:12:00

问题


I have over 5k lines long swagger.json file describing hundreds of paths and objects. I want to generate a TypeScript client (using swagger-codegen) using only a part of the endpoints. I don't want the generated TypeScript application to contain classes or interfaces connected with unused part of the swagger.json

How to filter out only a part of the Swagger documentation, describing the specified group of paths (e.g. all paths starting with /api/*)? Especially I want the filtered JSON to not contain definitions for unused data structures.


回答1:


I think you can do it, using task automation (grunt, gulp, shell, whatever). Basically it could be a 3 steps task:

  1. get the swagger.json (or call the swagger code-gen to get the json, with something like java -jar swagger-codegen-cli-x.x.x.jar generate -i <URL> -l swagger -o GeneratedCodeSwagger )
  2. remove the definitions/paths that you want to exclude and create a modified swagger.json
  3. call the code-gen passing the modified json with java -jar swagger-codegen-cli-x.x.x.jar generate -i GeneratedCodeSwagger\swagger.json -l typescript-angular



回答2:


Finally, we created swagger-json-filter – a command-line tool allowing filtering a Swagger documentation. It can be easily used along other commands in bash:

cat input.json | swagger-json-filter --include-paths="^\/api\/.*" > output.json

The tool is performing a logic needed to filter out undesired definitions (nested also) from the output.



来源:https://stackoverflow.com/questions/39788099/how-to-filter-generated-swagger-json-yaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!