christianhelle / refitter

A tool for generating Refit interfaces and contracts from OpenAPI specifications
https://refitter.github.io
MIT License
198 stars 43 forks source link

[ISSUE][1.2.1-preview.54] Some impediments using CLI version. Is not enough for my needs? #450

Open smietanka opened 3 months ago

smietanka commented 3 months ago

Hey, I just started working with Refitter a couple of days ago. It's a really cool tool, but it seems like a couple of options are not working as expected (or I am missing something), and I do have a problem with creating a proper configuration and parameters for the CLI command to generate contracts and interfaces.

So my case looks like this, I need to build interfaces and contracts after each build of project. I have an ASP.NET WebAPI with two controllers and some models, for now just as simple as possible. So I have created a "refitter.targets" file which I included inside the .csproj of my API. Inside targets I have couple <Exec command> targets where I am calling dotnet tool restore, dotnet tool update and of course this command dotnet refitter $(OpenApiDocumentation) -o $(RefitterOutputDirectory) -n $(ProjectName).Client --contracts-output $(RefitterContractsOutputDirectory) --contracts-namespace $(ProjectName).Contract --no-accept-headers --use-api-response --cancellation-tokens

As I mentioned I have 2 controllers named: CoolController and WeatherForecastController

  1. This command is generating me only one file for interfaces named RefitInterfaces.cs and Contracts.cs but I would like to see multiple clients with proper names like ICoolClient and IWeatherForecastClient with methods that belong only to these specific controllers. I have used --multiple-interfaces ByTag option and its looks pretty fine, but is there an option to split these interfaces into different files?
  2. About contracts, they are as well only inside one file. I would really like to see multiple files with proper names of classes. Classes are generated fine with all properties, but why it is the single file? I would like to see just files with names of the contract. For example: Error.cs, User.cs, etc.
  3. Inside the Contracts.cs I see a lot of disabled warnings. Is there a way to pass some kind of settings/configuration to the NSwag from Refitter?
  4. Is there a way to pass a summary as well (not only returns) of the method to the generated Refit interface? For now it looks like this image

I really appreciate any help you can provide.

christianhelle commented 3 months ago

@smietanka Thanks for taking the time to report this

  1. This command is generating me only one file for interfaces named RefitInterfaces.cs and Contracts.cs but I would like to see multiple clients with proper names like ICoolClient and IWeatherForecastClient with methods that belong only to these specific controllers. I have used --multiple-interfaces ByTag option and its looks pretty fine, but is there an option to split these interfaces into different files?

From v1.2.1 you can split the output into multiple files, but it's pretty primitive and can only split it into 3 files:

Technically, it's possible to split up the Refit interfaces into multiple files. I designed the system in a way that I should be able to easily.

  1. About contracts, they are as well only inside one file. I would really like to see multiple files with proper names of classes. Classes are generated fine with all properties, but why it is the single file? I would like to see just files with names of the contract. For example: Error.cs, User.cs, etc.

I piggy-back on NSwag for parsing the OpenAPI spec and generating contracts. I get all contracts generated as a single string and I don't intend to analyze and break that single large string down into multiple files. If I can find a way to use NSwag to generate single contracts at a time then I will in the future add support for generating multiple files for the contracts

  1. Inside the Contracts.cs I see a lot of disabled warnings. Is there a way to pass some kind of settings/configuration to the NSwag from Refitter?

I haven't figured out a way to get NSwag to not disable warnings. This is common practice in code generators as they have to support multiple versions of the framework. Refitter (and NSwag) generates code that is compatible with .NET Framework 4.x -> .NET Standard 2.0 -> .NET Core 9.0

It's probably a personal thing, but I treat generated code like pulling in a third-party library and I don't care much about how to structure code that I didn't write. That said, the way I use Refitter is to generate "Client SDK's" for the API's that I build from the CI/CD platform that I use

  1. Is there a way to pass a summary as well (not only returns) of the method to the generated Refit interface? For now it looks like this

Refitter purely looks at the OpenAPI spec and maps the details in paths to xml docs in the following way:

image

I'm generally very open to pull requests so if you have ideas or things you want to add to Refitter that you think you can build yourself then I'll make sure that if you create a pull request we get your changes in 😄

smietanka commented 3 months ago

Thanks for your response :)

Yeah, splitting files for each client would be really useful not only for readability but also for git history, etc. Basically, all kinds of splitting contracts or interfaces into different files would really help with git history :)

  1. I really would see some kind of option to pass NSwag settings into the NSwag process while the Refitter is running it. I believe it could be done somehow so NSwag probably has an option to split DTOs into different files, I will check first if this CLI has this option.
  2. This topic probably is similar to the second point, so passing configuration into NSwag directly from Refitter would take it into account so warnings could be disabled.
  3. Ok it seems that I have missed generating xmlDocumentation while I am using AddSwaggerGen now summaries are correctly passed into swagger.json :)

Thanks I will try to check the code what could be changed here