Saif
Saif

Reputation: 3452

How to extract a few APIs from one OpenAPI doc to another?

My application generates a JSON/YAML for all the API endpoints. However, I have an API gateway where I only allow a subset of these. For this, I currently pick the APIs manually from the first doc along with its models and create a new doc that can be supplied to the API Gateway.

This manual process takes a lot of time and is too complex when it comes to nested model schemas.

Is there a way I can automate this process so that a path I choose can be extracted out with its models to a new Open API doc.

Upvotes: 0

Views: 1785

Answers (1)

Saif
Saif

Reputation: 3452

You can use openapi-filter. https://github.com/Mermade/openapi-filter

Mark the APIs with x-internal: true as shown below:

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

Run the following command (where source.yaml is your input file):

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

Explanation:

--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

Compatibility:

Works with OpenAPI/Swagger 2.0 and 3.0.x and AsyncAPI definitions.

Credits to @Helen

Upvotes: 2

Related Questions