Skip to content

[REQ] Support parameter objects for apis and clients. #6844

Open
@yug1991

Description

@yug1991

Is your feature request related to a problem? Please describe.

There are 2 major issues blocking us from adopting openapi-generator.

  1. For languages that lack named parameters support, the generated SDKs are prune to errors during API upgrades.

    Consider a case where we decided to make some changes to our pre-existing API. Previously:

    class DefaultApi {
      myAPI (paramBoo, paramFoo, opts) { }
    }

    now becomes

    class DefaultApi {
      myAPI (paramBar, paramFoo, opts) { }
    }

    The code works "fine", as parameter paramBoo and paramBar have the same semantics, thus pass validations. But they actually have different meanings and may cause subtle errors in the backend. Users of the SDK will most likely fail to catch one or two calls like this and only realize problems down the line.

  2. Generated SDK clients don't support session parameters.
    For example, my backend may require a region field from all my client's requests, which stays constant for each user / session. The current SDKs (cpp and js at least) require these fields to be passed into each operation.

    class DefaultApi {
       myAPI (region, paramBoo, opts) { }
       myAPI2 (region, paramFoo, opts) { }
       myAPI3 (region, paramZoo, opts) { }
    }

    This is tedious for devs and also creates opportunities for human error.

Describe the solution you'd like

  1. I would like an option to put all api parameters into the opts field. For example, I generate my SDK with

    docker run --rm -v ${PWD}:/local 
        openapitools/openapi-generator-cli generate \
        -i /local/spec.json \
        -g javascript \
        --additional-properties useParameterObjects=true \
        -o /local/js-sdk

    The Api.js in the generated SDK will have something like:

    class DefaultApi {
       myAPI (opts) { }
    }

    and I call the api with

    myClient.myApi({ paramBar: 'xyz', paramFoo: 'abc' })
    // myClient.myApi({ paramBoo: 'xyz', paramFoo: 'abc' }) // Throws
  2. I would like a parameter added to SDK clients to support default parameters for all requests, so I could use the SDK like below:

    const myClient = new DefaultApi(new APIClient(), { region: 'NA' })
    myClient.myAPI({ paramFoo: 123 })

    or for one-offs do

    myClient.myAPI({ paramFoo: 123, region: 'EU' })

But if we add support for 1 above, support for 2 can be implemented by callers too. The solution would just be less nice.

Describe alternatives you've considered

Obviously, I can achieve these with customizations / providing my own templates, but these features seem to be useful in general.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions