NSwag
NSwag copied to clipboard
Exclude API paths in CSharp Client generation
I know there's already support for excluding controllers when generating the OpenAPI document, but this is for the other end when generating clients.
Is it possible to configure NSwag to generate clients only for specific segments/paths from a given OpenAPI document? I'm specifically using the CSharp Client generator here.
The use case is for when you're dealing with huge APIs, but you only really need to consume small portions of it.
I had a similar issue where I have a "Common" project with its API, and my project that adds its own API. When using swagger on the web, I want to be able to see both, but when generating the project's client classes, I only want to generate that portion of the API.
I found this workaround:
In my AddOpenApiDocument() :
if (Assembly.GetEntryAssembly() != Assembly.GetExecutingAssembly())
{// This is true when the API is being generated with NSwag
settings.ApiGroupNames = new[] {"Project"};
}
I hope that it helps.
@jeremyVignelles that's on the OpenAPI generator side, but the question here is whether there is a filter setting for the incoming OpenAPI spec in the client code generator, and the answer is: Not yet.
So the solution for now is to preprocess/transform the spec (e.g. with a simple cmd line app which uses the nswag packages) before passing it to the generator.
Yes, maybe that wasn't clear, but what I meant is that I'm using NSwagStudio to generate the client code from the .net assembly. In that process, the entry assembly is NSwag, which in turn runs the code assembly.
That way, I'm able to generate C#/TS code from a subset of the API.
I agree, if you control the whole toolchain/pipeline you can generate a spec with only the operations you need. But if you get the spec from an external source, then you dont have the first step and you need to apply a filter on the document.
Thanks for the suggestion @jeremyVignelles. As @RicoSuter is saying, this is for an external API that I can't modify.
@RicoSuter do you think this is something I could pick up and do a PR for? If you could point me to the correct places I'll have a crack at it.
The hardest part here is that for example choose only half of the operations and we only need to render the required DTO classes. That means we need to transitively scan the schemas and only pick the needed ones depending on their usage.
In fact, you only need to preload the DTO classes from the specification in NSwagStudio, deduce all the classes, and then select only the necessary ones. Then generate client code. Is it possible to implement this?
And then you can think about how to add this to the NSwag.Commands - this is the hardest part. Right?
@RicoSuter @SaebAmini @unchase I am generating the spec using the tool StopLightStudio.We currently pass this spec to NSwag dll to get the Controllers and the Models generated. But, going forward we would like to have just the Models generated and not the controller. Is the ignoreApi attribute used for this purpose? If so, where do I have to specify this attribute?
Please find attached a sample spec.
Hi, I'm using NSwag Studio to generate Clients via processing of the Web API by reflection. There are actions [such as HttpGet, Route("")] in my controllers for which I wish to avoid generating client methods. Does a process exist yet where I can specify which actions to skip using the Web API by reflection method?
Use an [OpenApiIgnore] attribute or preprocess the spec before providing it to the client generator. With the ASP.NET Core spec generator you can also register custom operation processors to exclude operations or define other ApiGroupNames as @jeremyVignelles described.
Thanks. That's what I was looking for!
This is really tricky, and a common problem Huge json APIs, but only a small amount needed
An ignoreApi or an onlyApi would be helpful
Other alternative is to edit manually the json API... and remove the unnecessary
Are there any updates on the topic? We also have a huge api being generated which I would like to filter by tag name for example and ignore the rest.