Forum Discussion

tanz's avatar
tanz
New Contributor
10 months ago

Server Variables - can it be refrenced?

i am using openapi:3.0.0
i have five servers define in swagger
inside it i am using variables specific to servers.
something like this


servers:
 - url: https://server1/api/{version}
  description:  server
  variables:
    version:
    description: versioning of api
    enum:
      - v1
    default: v1
- url: https://server2/api/{version}
  description: server
  variables:
   version:
   description: versioning of api
   enum:
    - v1
  default: v1


Can we declare variables once and can we reference in multiple servers?

here i am breaking "DRY" by declaring same variables again and again.

  • tanz No that's not supported by the OpenAPI Specification. You can always request changes at https://github.com/OAI/OpenAPI-Specification/issues

    If you have a common pattern in your servers, then perhaps you can approach it slightly differently:

    servers:
      - url: 'https://{env}.domain.com/api/{version}'
        variables:
          env:
            description: Environment - staging or production
            default: staging
            enum:
              - staging
              - production
          version:
            description: API version
            default: v1
            enum:
              - v1
              - v2