MvdD
MvdD

Reputation: 23436

Naming AutoRest generated method names using Swagger

I was playing around with the AutoRest and Swagger projects. My Web API controller has a method called GetAllAsync. When I generate a client for the service, the client has an interface IResourcesOperations, where Resources is the name of the controller.

The interface has a method called GetAllAsyncWithHttpMessagesAsync. Then there's a static class called ResourcesOperationsExtensions that defines a method called GetAllAsync and one called GetAllAsyncAsync. The first one actually runs the second one of a new thread from the thread pool (Task.Factory.StartNew). Anyone know the reason for that?

I found that I can decorate my controller action method with the attribute

    [SwaggerOperation("GetResources")]

This will generate a method on the client class called GetResourcesWithHttpMessagesAsync and remove all methods for this web API action from the interface and extension method class.

Now my question are, why are these three methods generated by default?

And is there a way to generate a client with a method named GetResources (i.e. get rid of that WithHttpMessagesAsync suffix) or even GetAllAsync?

Upvotes: 4

Views: 2844

Answers (2)

Martin Rhodes
Martin Rhodes

Reputation: 1118

AutoRest (at least the more recent versions) generates classes with the suffix Extensions. These classes contain extension methods on the proxy interface which allow you to call the methods with shortened method names like the ones you are after.

Simply add a

@using TheNameSpace.OfYour.Client.Extensions

to any class in which you need access to these shortened method names.

Upvotes: 2

olydis
olydis

Reputation: 3310

AutoRest basically always generates an <operation-name>Async (extension) method for people who want to use async/await and a synchronous version <operation-name> that blocks.

You did not say what your method actually returns or is supposed to do, but I assume you want one less Async in your generated method names.

In that case you have to convince Swashbuckle to strip the Async suffix when it generates the Swagger, i.e. override the generated method name to GetAll. AutoRest does not try to be smart when it sees a Swagger method definition with name GetAllAsync.

Upvotes: 1

Related Questions