cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Support selection of Server Variables #315

Open juliangrube1988 opened 9 months ago

juliangrube1988 commented 9 months ago

OpenApi 3.1 allows a Server Variable Object like:

{
  "servers": [
    {
      "url": "https://{username}.gigantic-server.com:{port}/{basePath}",
      "description": "The production API server",
      "variables": {
        "username": {
          "default": "demo",
          "description": "this value is assigned by the service provider, in this example `gigantic-server.com`"
        },
        "port": {
          "enum": [
            "8443",
            "443"
          ],
          "default": "8443"
        },
        "basePath": {
          "default": "v2"
        }
      }
    }
  ]
}

ng-openapi-gen only uses the provided default values:

private readRootUrl() {
    if (!this.openApi.servers || this.openApi.servers.length === 0) {
      return '';
    }
    const server = this.openApi.servers[0];
    let rootUrl = server.url;
    if (rootUrl == null || rootUrl.length === 0) {
      return '';
    }
    const vars = server.variables || {};
    for (const key of Object.keys(vars)) {
      const value = String(vars[key].default);
      rootUrl = rootUrl.replace(`{${key}}`, value);
    }
    return rootUrl;
  }

It would be great if users can provide values for the variables