Search code examples
angulartypescriptopenapi-generatoropenapi-generator-cli

Is it a way to generate endpoint api basePath as variable with OpenAPI Generator?


I'm using OpenAPI Generator cli to generate endpoint api service from template.yml

In my template i define server url in string and i would like to pass as a variable because i have dev env & prod env with differents url.

openapi: 3.0.0
servers:
  - url: 'http://localhost:8080/MyApiCustom'

Can i pass options in cli command openapi generate which it can be use in template, or use env variable in template ?

Thanks for help !


Solution

  • Yes, this is entirely possible. OpenApi 3.x supports server templating. For example:

    servers:
      - url: http://{env}:{port}/{path}
        variables:
          env:
            default: prod
            description: The current environment
          port:
            enum:
              - '8080'
              - '8098'
            default: '8080'
          path:
            default: MyApiCustom
    

    You can then define these variables in your cli using the following cli parameter:

    --server-variables=env=dev,port=8098,path=devApi

    This feature was added in this commit from 2019.

    Note: you must have a default specified in your OAS file.