OpenAPITools / OpenAPITools/openapi-generator

[REQ] Support parameter objects for apis and clients.

Open
#6,844 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: JavaScript/Node.js Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

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

Start by tracing how the generator's Java implementation and the JavaScript/C++ client generation paths handle API method parameters and additional properties such as useParameterObjects. Compare the generated Api.js examples in the issue with current output; done means parameter objects are supported as requested and the generated clients handle the documented defaults and overrides consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, java, javascript, openapi
Domain
api, developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.