Create one code client / flatten controllers with NSwag and AutoREST

巧了我就是萌 提交于 2021-02-08 10:55:49

问题


I'm trying to create a code wrapper for an api with NSwag and Autorest.

Previously I was using Swashbuckle to generate the swagger file. It generated the swagger file with operationIds in the format actionMethod. This resulted in Autorest generating a code client that was 1-deep. All of the actions were on the top-level class.

For various reasons, I needed to change swagger generation to NSwag. This generates operationIds in the format controller_actionMethod. This results in AutoRest creating a composite class that exposes separate classes with actions for each controller.

How can either

  • Change how NSwag generates the operationIds
  • Change how Autorest maps operationIds

Note: I know I can manually change the swagger.json, but I'd like to keep a consistent automated process for generating the code client.


回答1:


There doesn't appear to be any readily available settings, but you can hook into the generation process of NSwag

https://github.com/RicoSuter/NSwag/wiki/Document-Processors-and-Operation-Processors#operation-processors

The operation processor

class FlattenOperationsProcessor: IOperationProcessor
{
    public async Task<bool> ProcessAsync(OperationProcessorContext context)
    {
        context.OperationDescription.Operation.OperationId = $"{context.MethodInfo.Name}";
        return true;
    }
}

Then add it in Startup.cs

document.OperationProcessors.Add(new FlattenOperationsProcessor());


来源:https://stackoverflow.com/questions/56191033/create-one-code-client-flatten-controllers-with-nswag-and-autorest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!