warmuuh/milkman

OpenAPI Import only takes first listed Server Object in configuration

Closed this issue · 3 comments

While it isn't difficult to edit the OpenAPI yaml file and simply move the Server Object I want to the top of the array, it would nice if Milkman could let me decide which Server Object I'd like to be the base for the Imported Collection.

Other Potential solutions

Another way would be to integrate into environments by just replacing the generated url with an {{ENVURL}} variable tag and then for every member of the array you populate a new environment (called something like ImportedServerN) with the Server Object's url as a {ENVURL} variable.
(This method would then maybe even allow directly importing the Server Object Variables Field into the Milkman system by copying the variable'd url and then populating the environment with the named variables)

Spot in code

I think this is the line where the base URL is determined from the import.

String host = spec.getServers().stream().findAny().map(s -> s.getUrl()).orElse("http://localhost:8080");

I like the idea of setting up env-variables for the server-urls, but problem would be that milkman doesnt know which env to pick for which server-url. so importing everything into the current env leads to env-vars like "my-service.production-server.url", "my-service.staging-sever.url", which is ok-ish but not perfect.
i will think about this and might add it soon

I thought of creating multiple environments instead of one.

I assumed that people would set up different base servers for different contexts as in api.host.com , staging-api.host.com or dev-api.host.com.

So they would define different base servers that can serve all their paths making this approach in MilkMan easy:

  • Replace all base urls in path definitions with {{BASEURL}}
  • Create N Environments in the workspace that define {{BASEURL}}.

e.g the Serv1 environment defines BASEURL = staging-api.host.com
Serv2 environment defines BASEURL = staging-api.host.com and the
Serv3 environment defines BASEURL = dev-api.host.com
You would access the same paths on different servers by switching environments.

But...

The problem then becomes dealing with the fact that OpenAPI doesn't just define a list of base servers, but allows you to define a totally different set for a specific path!
You can define a path like /create to be serviceable by the base list but also by a totally different set of servers!

This should be fine to omit in milkman (which is, after all, a lightweight client) as it doesn't seem like most api definitions using OpenAPI include an alternate set to a specific path definition regularly, but it is something to keep in mind.

Server Templating

After giving it more thought including the variable definitions for servers might be more trouble than it is worth:

Consider:

servers:
  - url: https://{region}.api.global.com
    variables:
      region:
        default: westus
        enum:
          - westus
          - eastus2
          - westcentralus
          - westeurope
          - southeastasia

Is an easy example:

  • Make the baseurl for every path https://{{IMPORT-region}}.api.global.com
  • Define 5 environments in the workspace that set {{IMPORT-region}} to the every value in the enum

e.g. Serv1 defines IMPORT-region = westus
Serv2 defines IMPORT-region = eastus2
Serv3 defines IMPORT-region = westcentralus
etc...

but:

servers:
  - url: {protacol}//{region}.api.global.com:{port}/v2
    variables:
      protocol:
        enum:
          - http
          - https
        default: https
      region:
        default: westus
        enum:
          - westus
          - eastus2
          - westcentralus
          - westeurope
          - southeastasia
        port:
        enum:
          - '443'
          - '8443'
        default: '443'

How should MilkMan parse this?
Should it create 20 environments for every permutation?
Probably not.

Note: 'default' is a REQUIRED parameter so an easy way to parse this is just to interpret the BASEURL with only the defaults selected

i chose the most simple solution for now: import all and generate server-names from the description if given. hope thats good enough for now