0

I have a static openapi.yaml file which looks something like this:

Existing Yaml:

openapi: 3.0.0
info:
  title: API
  description: >-
    Awesome API
  contact:
    name: bh4r4th
    email: nenethopu@gmail.com
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 2.0.0
servers:
  - url: http://localhost:8080
    description: Dev Server
  - url: http://www.example.com
    description: Prod Server

Now, I wanted to pass servers dynamically using an environment variable.

Expected Yaml:

openapi: 3.0.0
info:
  title: API
  description: >-
    Awesome API
  contact:
    name: bh4r4th
    email: nenethopu@gmail.com
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 2.0.0
servers: ${SWAGGER_API_URLS}

Why I wanted to do this?

  1. I am building my application as docker image and using same image for 100 different clients.
  2. So, each client has their own set of Dev & Prod servers.

I tried:

  1. Setting @OpenAPIDefinition on application
  2. Adding Beans like OpenApiCustomizer, OpenApi, GlobalOpenApiCustomizer None worked.

Before I go hacky and edit actual file on docker build. I am looking for any simple solution out of the box saves tonnes of extra configuration.

Dependencies used:

* springdoc-openapi-ui: 1.6.11

    * swagger-ui: 4.14.0
    
* spring-boot: 2.6.6

* maven: 3.8.1
bh4r4th
  • 3,760
  • 1
  • 21
  • 25
  • 1
    You cannot use the classes mentioned to enrich a static file, the classes are used runtime, when generating the openapi with a @RestController or similar as base. – Jan Nielsen Nov 30 '22 at 21:52
  • Yes Jan Nielsen, I couldn't supply urls with above mentioned classes. I solved this with a simple fix, which served my purpose. i.e., by removing `servers` from swagger completely. Which pointed to `/`by default. So, it points to application root path in my case. Suppose, if my application is running on http:/localhost:8000 then server points to http:localhost:8000/ – bh4r4th Dec 06 '22 at 04:39

0 Answers0