Skip to content

[typescript] Use static middleware when calltime options exist but specify no middleware #20985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

bodograumann
Copy link
Contributor

This PR is a continuation of #20978 with more improvements following #20430.
I'll rebase once the other PR is merged. Currently this branch contains all changes from both PRs.

  • Keep static middleware when options contain no middleware field
    This is the main change in behaviour as discussed before in the other PR.
    When the user calls a method with options that do not set a middleware, the existing static middleware should be used, to follow the spirit of merging the top-level fields of the options.
    On the other hand the user can give an empty array to skip any middlewares for the call.
    I added tests for both cases.
  • Extract middleware merge logic into function
    I included more refactoring in this PR.
  • Simplify extracted functions
  • Fix: Pass static config when no options are given for inversify
    I also noticed an error in the inversify variant. When no options are given, the static config should be used. Previously even empty options where used here.
    Unfortunately it seems the inversify tests are broken and I guess inversify 6 is not compatible with typescript 4. Anyway I could not get them to work again and so I did not add a test here.
  • Fix: Allow overriding http api with options
    Another bug, that I found, is that httpApi was always taken from the static configuration and could not be overridden using options. I fixed that and added a test.
  • Regenerate samples
    I regenerated the samples after almost every change, to make them clearer.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

CC @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04) @joscha (2024/10)

@bodograumann bodograumann force-pushed the typescript-keep-static-middleware branch 2 times, most recently from 9c3a423 to d88422b Compare April 1, 2025 10:39
@bodograumann bodograumann force-pushed the typescript-keep-static-middleware branch from d88422b to f102f02 Compare April 1, 2025 11:16
@bodograumann
Copy link
Contributor Author

bodograumann commented Apr 1, 2025

I rebased all remaining changes here onto master, @joscha .
Is that small enough, or should I split it up?
There are multiple things happening here:

  • Format change
  • Refactor to improve code
  • Slight behavioural change
  • Two bug fixes

@bodograumann bodograumann marked this pull request as ready for review April 1, 2025 11:52
return staticMiddleware;
}
// default to replace behavior
switch(strategy ?? "replace") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this become a signature change, i.e.: ..., strategy: "append" | "prepend" | "replace" = "append" then the default is also visible for callers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Changed :-)

}
return conf;
throw new Error(`Unrecognized middleware merge strategy '${strategy}'`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you move this out because of the explicit return rule? Otherwise I think it was semantically easier to understand inside the switch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It felt cleaner to me, because it made it obvious, that it is a last-resort for the function (actually in typescript it is unreachable anyway, because the switch completely covers all possible values of strategy)
Putting it into default makes sense as well because it is concerned with invalid values for strategy.
I can put it back into default: if you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer it for future readers/changes, to make sure noone puts code in between. It should immediately exit on an unknown value. If you're happy to move it back in, that would be appreciated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I moved it back inside.

@joscha
Copy link
Contributor

joscha commented Apr 1, 2025

@macjohnny this one is ready, too

@macjohnny macjohnny merged commit 8e329fe into OpenAPITools:master Apr 1, 2025
18 checks passed
@wing328 wing328 added this to the 7.13.0 milestone Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants