Is it possible to prefix all paths in an OpenAPI 2.0 spec?

Viewed 37

I have Swagger specification for an API that looks something like this:

swagger: '2.0'
info:
  title: my-api
  description: My API
  version: v1
schemes:
  - https
produces:
  - application/json
paths:
  v1/entry/check:
    post:
      ...
  v1/entry/enter:
    post:
      ...
  v1/exit/leave:
    post:
      ...
...

As you can see, every single path in paths is prefixed with v1/ as the spec describes version v1 of the API (as you can also see in the info.version value).

I am wondering, is there a way to avoid repeating this prefix, i.e. implicitly prefix all paths with v1/? Mainly, I want to do this to avoid any accidental misuse of the spec, i.e. entirely prevent adding another path without the prefix.

1 Answers

You can specify the prefix as basePath.

basePath: /v1
paths:
  /entry/check:
    ...
  /entry/enter
    ...
Related