NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

Exclude API paths in CSharp Client generation

Open SaebAmini opened this issue 5 years ago • 14 comments

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.

SaebAmini avatar Dec 16 '19 23:12 SaebAmini

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 avatar Dec 17 '19 14:12 jeremyVignelles

@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.

RicoSuter avatar Dec 17 '19 14:12 RicoSuter

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.

jeremyVignelles avatar Dec 17 '19 14:12 jeremyVignelles

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.

RicoSuter avatar Dec 17 '19 15:12 RicoSuter

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.

SaebAmini avatar Dec 17 '19 19:12 SaebAmini

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.

RicoSuter avatar Dec 17 '19 20:12 RicoSuter

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?

unchase avatar Feb 26 '20 12:02 unchase

@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.

SampleSpec.txt

francisminu avatar Jul 29 '20 14:07 francisminu

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?

dis081278 avatar Sep 04 '20 05:09 dis081278

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.

RicoSuter avatar Sep 29 '20 08:09 RicoSuter

Thanks. That's what I was looking for!

dis081278 avatar Oct 01 '20 06:10 dis081278

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

tiagopsantos avatar Sep 17 '22 22:09 tiagopsantos

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.

Sleyy avatar May 29 '23 13:05 Sleyy