OpenAPITools / OpenAPITools/openapi-generator

[BUG][TYPESCRIPT] ObservableAPI methods don't take care of `_options.middleware` property.

Open
#14,549 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

ObservableAPI generated methods don't take care of _options.middleware property.

openapi-generator version

v6.2.1

OpenAPI declaration file content or url

I'm able to reproduce the bug using the official petstore.json file available at: https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.json

Generation Details

I generated the code by running:

openapi-generator-cli generate -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.json -g typescript -o .
Steps to reproduce

I installed all dependencies and created a bug.ts file to test and show the issue.

npm install
npm install -D ts-node

I uploaded all sources to StackBitz, this is the link https://stackblitz.com/edit/node-4amp2b?file=README.md,bug.ts

You just need to run:

npx ts-node bug.ts

Inside the bug.ts file, I'm defining two middleware;

The first one is at the configuration level.

const configuration = createConfiguration({
  middleware: [
    {
      pre: (context) => {
        // set a Query Param from here (it works!)
        context.setQueryParam('fromConfiguration', 'got-it');

        console.log('###### URL:', context.getUrl());
        console.log('');

        return of(context);
      },
      post: (context) => of(context),
    },
  ],
});

The second one is at the getPetById level.

petApi
  .getPetById(1, {
    authMethods: configuration.authMethods,
    baseServer: configuration.baseServer,
    httpApi: configuration.httpApi,

    // middleware from here are not applied
    middleware: [
      {
        pre: (context) => {
          // set a Query Param from here (it doesn't work)
          context.setQueryParam('fromGetPet', 'so-sad');
          return of(context);
        },
        post: (context) => of(context),
      },
    ],
  })

My expectation is to see a console.log with:

URL: http://petstore.swagger.io/v2/pet/1?fromConfiguration=1&fromGetPet=1

but instead, I see:

URL: http://petstore.swagger.io/v2/pet/1?fromConfiguration=1

Suggest a fix

When methods are auto-generated, they are not taking care of the property _options.middleware but they rely only on this.configuration.middleware.

https://github.com/OpenAPITools/openapi-generator/blob/ac5134acf3db00171f8ee4689d52c404284a9128/modules/openapi-generator/src/main/resources/typescript/types/ObservableAPI.mustache#L67-L71

I think that we should concat these two arrays.

I'll open a PR as soon as possible with the proposed solution.

What do you think? Is there a different way to apply query parameters (usually for filtering data) when the OpenApi Schema doesn't provide an - in: query?

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 at modules/openapi-generator/src/main/resources/typescript/types/ObservableAPI.mustache around lines 67-71 and generate the client from the linked petstore.json using the reported command. Verify the generated getPetById call applies both configuration.middleware and _options.middleware, producing a URL containing both query parameters; reproduce with the linked bug.ts example.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.