RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.68k stars 1.24k forks source link

Exclude API paths in CSharp Client generation #2584

Open SaebAmini opened 4 years ago

SaebAmini commented 4 years ago

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.

jeremyVignelles commented 4 years ago

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.

RicoSuter commented 4 years ago

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

jeremyVignelles commented 4 years ago

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.

RicoSuter commented 4 years ago

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.

SaebAmini commented 4 years ago

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.

RicoSuter commented 4 years ago

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.

unchase commented 4 years ago

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?

francisminu commented 4 years ago

@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

dis081278 commented 4 years ago

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?

RicoSuter commented 3 years ago

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.

dis081278 commented 3 years ago

Thanks. That's what I was looking for!

tiagopsantos commented 1 year ago

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

Sleyy commented 1 year ago

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.

dsolteszopyn commented 1 month ago

"documentGenerator": { "fromDocument": { "json": "", "url": "", "output": null, "newLineBehavior": "Auto", "operationFilter": { "includeOperations": ["operationId1", "operationId2"], "excludeOtherOperations": true } } }

need to have some type of operationfilter that supports either includeOperations or excludeOperations