FATAL: OperationId is required for all operations. Please add it for 'get' operation of path

余生长醉 提交于 2020-04-18 03:47:31

问题


I am using AutoRest to generate the client side of an api from swagger.json

The output is

AutoRest code generation utility [cli version: 3.0.6187; node: v10.16.3, max-memory: 8192 gb]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
NOTE: AutoRest core version selected from configuration: ~2.0.4413.
   Loading AutoRest core      'C:\Users\kirst\.autorest\@microsoft.azure_autorest-core@2.0.4417\node_modules\@microsoft.azure\autorest-core\dist' (2.0.4417)
   Loading AutoRest extension '@microsoft.azure/autorest.csharp' (~2.3.79->2.3.82)
   Loading AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.55->2.3.55)
FATAL: OperationId is required for all operations. Please add it for 'get' operation of '/api/Test' path.
FATAL: AutoRest.Core.Logging.CodeGenerationException: OperationId is required for all operations. Please add it for 'get' operation of '/api/Test' path.
   at AutoRest.Modeler.SwaggerModeler.Build(ServiceDefinition serviceDefinition) in /opt/vsts/work/1/s/src/SwaggerModeler.cs:line 96
   at AutoRest.Modeler.Program.<ProcessInternal>d__2.MoveNext() in /opt/vsts/work/1/s/src/Program.cs:line 60
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NewPlugin.<Process>d__15.MoveNext()
FATAL: csharp/imodeler1 - FAILED
FATAL: Error: Plugin imodeler1 reported failure.
Process() cancelled due to exception : Plugin imodeler1 reported failure.
  Error: Plugin imodeler1 reported failure.

I notice that the swagger.json does not have any mention of operationId

Yet I do have it mentioned in the api

    [AllowAnonymous]
    [HttpGet("Test")]
    [SwaggerOperation(OperationId = "Test")]

    public IActionResult Test()
    {

[Update]

I have spent 2 days in Swagger Attribute Hell as I try and upgrade my solution to use netcore3.1 and AutoRest 3

It would help me to know what attributes I need to put on the values controller to be able to generate the client code.

[Route("api/[controller]")]
[Produces("application/json")]
public class ValuesController : Controller
{
    public ValuesController()
    {
    }

    [HttpGet()]
    [Produces("application/json")]
    public IEnumerable<string> Get()
    {
        return new string[] {"value1", "value2"};
    }

    [Produces("application/json")]
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    [HttpPost()]
    public void Post([FromBody] string value)
    {
    }

    // PUT api/values/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody] string value)
    {
    }

    // DELETE api/values/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
}

I am using

autorest --v3 --input-file=https://mywebsite/myapi/v1/swagger.json --output-folder=generated --csharp --namespace=myconnector

The output is

AutoRest code generation utility [cli version: 3.0.6187; node: v12.16.1, max-memory: 8192 gb]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
   Loading AutoRest core      'C:\Users\kirst\.autorest\@autorest_core@3.0.6262\node_modules\@autorest\core\dist' (3.0.6262)
   Loading AutoRest extension '@microsoft.azure/autorest.csharp' (~2.3.79->2.3.84)
   Loading AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.55->2.3.55)
FATAL: OperationId is required for all operations. Please add it for 'get' operation of '/api/Values' path.
FATAL: AutoRest.Core.Logging.CodeGenerationException: OperationId is required for all operations. Please add it for 'get' operation of '/api/Values' path.
   at AutoRest.Modeler.SwaggerModeler.Build(ServiceDefinition serviceDefinition) in /opt/vsts/work/1/s/src/SwaggerModeler.cs:line 96
   at AutoRest.Modeler.Program.<ProcessInternal>d__2.MoveNext() in /opt/vsts/work/1/s/src/Program.cs:line 60
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NewPlugin.<Process>d__15.MoveNext()
  Error: Plugin imodeler1 reported failure.

In the api code I have TargetFramework netcoreapp3.1, Swashbuckle.AspNetCore 5.2.0, FluentValidation 8.6.2

I had everything working when my api was in .net core 2.1 However I wanted to call a .netstandard2 library so I upgraded my api to netcore3.1

The docs seem incomplete. No mention of Autorest. Should I be trying a different code generator I wonder.

[Update]

Sample json from .netcore2.1 branch

Sample json from .netcore3.1 branch

However the comparison may not be fair because I may have changed things in the netcore3.1 branch.

I have set up a sample repository for this related question and will set up a netcore2.1 branch for that.


回答1:


I think latest Swagger (5.2.1) by default doesn't generate OperationId for operations because it is an optional identifier as per their docs.

operationId is an optional unique string used to identify an operation. If provided, these IDs must be unique among all operations described in your API.

However, AutoRest seems to use that to identify each method. I found a Github question / issue where people addressed this by configuring AutoRest to use tags instead of operation ID to identify method.

AutoRest uses operationId to determine the class name/method name for a given API.

If you do not want to use tags, or your tags are not unique enough, you could ask Swagger to add operation ID to the generated JSON, by:

options.SwaggerDoc(...);

// Tell Swagger where to find operation ID.
// In this example, use the controller action name.
options.CustomOperationIds(
    d => (d.ActionDescriptor as ControllerActionDescriptor)?.ActionName);


来源:https://stackoverflow.com/questions/60819169/fatal-operationid-is-required-for-all-operations-please-add-it-for-get-opera

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