OpenAPITools / OpenAPITools/openapi-generator

[BUG] Server Variables not working with Multiple Servers

Open
#6,338 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

If you specify multiple servers with server variables in your yaml file, their URLs are not properly made available at template render time. Instead, it shows their URLs with the non-replaced variable names.

openapi-generator version

Am using master @ commit 77f2a25c3d

OpenAPI declaration file content or url
servers:
  - url: http://127.0.0.1.nip.io:{listenPort}/{commandLineServerArg}
    description: Debug Localhost
    variables:
      listenPort:
        default: '5000'
        description: Port on which server listens
  - url: https://{username}.gigantic-server.com:{port}/{basePath}
    description: The production API server
    variables:
      username:
        # note! no enum here means it is an open value
        default: demo
        description: this value is assigned by the service provider, in this example `gigantic-server.com`
      port:
        enum:
          - '8443'
          - '443'
        default: '8443'
      basePath:
        # open meaning there is the opportunity to use special base paths as assigned by the provider, default is `v2`
        default: v5/
Command line used for generation

./run-in-docker.sh generate -i /gen/spec.yaml -g python -o /gen/python/ --server-variables listenPort=3456,commandLineServerArg=woof

Steps to reproduce

Run command above on supplied yaml. Will see in output file configuration.py @ about line 338 that servers do not have template variables replaced.

Related issues/PRs

#6337

Suggest a fix

One potential fix is in the pull request noted above. PHP templates deal with this by rendering all of the server information into their configuration template/file, ignoring serverVariables specified on command line to openapi-generator (--serverVariables).

Best is probably to have all templates individually handle. Command line serverVariables should be added to the bundle that is supplied to templates, and individual language templates should fill in such variables at generator run-time.

For variables specified in the yaml/json file, they can remain un-substituted at generator run-time, and instead filled in with logic in the rendered templates.

Wdyt?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the supplied OpenAPI declaration and generation command, then inspect the generated Python configuration.py around line 338. Compare the behavior with related issue/PR #6337 and the PHP template handling described here. Done means server URLs are rendered with the appropriate variable values rather than unresolved variable names.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, python
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.