RicoSuter / NSwag

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

Setting Operation Ids in AddOpenApiDocument configuration - DOTNET 8 #4799

Open andreeapurta opened 4 months ago

andreeapurta commented 4 months ago

Hello all! Prevously I was using for a project Swashbuckle.AspNetCore for generating my swagger.json file. Now I started using NSwag for this. Is there any way I can achieve the following configuration of OperationId in NSwag? (without MANUALLY adding any annotation on the controllers' methods)

Swashbuckle.AspNetCore AddSwaggerGen configuration:


                    options.CustomOperationIds(setup =>
                    {
                        string operationId = setup.ActionDescriptor.AttributeRouteInfo.Name;
                        if (string.IsNullOrEmpty(operationId))
                        {
                            operationId = setup.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor ?
                                controllerActionDescriptor.ActionName : operationId;
                        }

                        return operationId;
                    });
Thanks in advance! 
andreeapurta commented 4 months ago

You can achieve this by:

public class MyOperationsIdTest : IOperationProcessor
{
    public bool Process(OperationProcessorContext context)
    {
        SetOperationId(context);
        return true;
    }

    private static void SetOperationId(OperationProcessorContext context)
    {
        //insert your rules here
        context.OperationDescription.Operation.OperationId = context.MethodInfo.Name;
    }

    private static string GetOperationIdPrefix(string operationId)
    {
        return new string(operationId.TakeWhile(char.IsLetter).ToArray());
    }
  services.AddOpenApiDocument(
      options =>
      {
          options.OperationProcessors.Add(new MyOperationsIdTest()); 
           ..........
andreeapurta commented 4 months ago

however some are duplicated...like GetById1,GetById2..