Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parameterize the API base path in OpenAPI (Swagger)?

I have an URL like this:

/id/{idnumber}/status

In this URL, /id/{idnumber} is the API base path, and /status is the resource.


I know that OpenAPI (Swagger) allows parameters in paths, like so:

paths:
  /id/{number}/status:

but this is not what I need, because /id/{idnumber} is the base path and not part of the resoruce path.

Is there any way to have a parameter in the base path?

host: my.api.com
basePath: /id/{idnumber}   # <---

paths:
  /status:
like image 764
Rad4 Avatar asked Feb 22 '16 22:02

Rad4


2 Answers

Parameterized base paths are now supported in OpenAPI 3.0, using server variables:

openapi: 3.0.0
...

servers:
  - url: https://my.api.com/id/{number}
    variables:
      number:
        default: '-1'

Note that server variables MUST have a default value - it will be used if the client does not supply a value.

For details, see:

  • Server Object and Server Variable Object sections of the the OpenAPI 3.0 Specification
  • API Server and Base URL on swagger.io
like image 140
Helen Avatar answered Sep 26 '22 18:09

Helen


I don't think basePath allows variables.

For your case, you don't need to use basePath. You can simply put /id/{idnumber} in the URL path. For example:

    "/pet/{petId}": {
like image 26
William Cheng Avatar answered Sep 25 '22 18:09

William Cheng