Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single client for multiple api versions

I am using openapi-generator to generate api client from swagger json using command similar to below

openapi-generator generate -i http://localhost:5001/swagger/v1/swagger.json -g csharp-netcore -c config.json

Is it possible to generate single client using open api generator for multiple version of Api.

http://localhost:5001/swagger/v1/swagger.json
http://localhost:5001/swagger/v2/swagger.json

I looked at the documentation but couldn't find any reference for the same. Any help on the same or other alternatives. I am creating my api using .net core framework - doesn't really matter for generating clients but as an information if it helps in any way.

like image 536
Vikash Mishra Avatar asked Nov 02 '25 02:11

Vikash Mishra


1 Answers

Well yes and no. You basically have two API definitions in two different files. Moreover, these files contain API definitions that are similar but not exactly the same: some parameters are new, some old entities have new members (or, worse, lost some members). Some methods are probably not there. You are potentially running into compatibility issues here, so same codebase for two different versions is probably not even possible. Depending on the APIs and your requiments, you can though automate a few things:

  • You could write a batch file that generates code from both APIs and copies generated data into your project, e.g. into V1 and V2 folders, from where you access the generated code.
  • Depending on your API you already probably have some idea on how the generated code should look like. You can access mustache template files in the Swagger Codegen project and modify them. So the generated code will suit exactly your needs. This makes sense for a big project with long timespan where this job will pay off.

Edit: Also why would you need a single client for several versions? I can imagine a single server that supports multiple versions, but client? Wouldn't it be better off by just using the latest version?

like image 188
Maxim Zabolotskikh Avatar answered Nov 04 '25 18:11

Maxim Zabolotskikh