致命:所有操作均需要OperationId.请为路径的“获取"操作添加它 [英] FATAL: OperationId is required for all operations. Please add it for 'get' operation of path
问题描述
我正在使用AutoRest从swagger.json生成api的客户端
I am using AutoRest to generate the client side of an api from swagger.json
输出为
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.
我注意到swagger.json没有提及operationId
I notice that the swagger.json does not have any mention of operationId
但是我确实在api中提到了
Yet I do have it mentioned in the api
[AllowAnonymous]
[HttpGet("Test")]
[SwaggerOperation(OperationId = "Test")]
public IActionResult Test()
{
[更新]
在尝试升级解决方案以使用netcore3.1和AutoRest 3时,我在Swagger属性地狱中呆了2天
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)
{
}
}
我正在使用
autorest --v3 --input-file=https://mywebsite/myapi/v1/swagger.json --output-folder=generated --csharp --namespace=myconnector
输出为
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.
在api代码中,我具有TargetFramework netcoreapp3.1,Swashbuckle.AspNetCore 5.2.0,FluentValidation 8.6.2
In the api code I have TargetFramework netcoreapp3.1, Swashbuckle.AspNetCore 5.2.0, FluentValidation 8.6.2
当我的api在.net core 2.1中时,一切正常 但是我想调用.netstandard2库,所以我将api升级到了netcore3.1
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
文档似乎不完整.没有提及Autorest.我是否应该尝试使用其他代码生成器?
The docs seem incomplete. No mention of Autorest. Should I be trying a different code generator I wonder.
[更新]
.netcore2.1分支中的json样本
Sample json from .netcore2.1 branch
.netcore3.1分支中的json样本
Sample json from .netcore3.1 branch
但是,这种比较可能不公平,因为我可能已经更改了netcore3.1分支中的内容.
However the comparison may not be fair because I may have changed things in the netcore3.1 branch.
我已经为此相关问题建立了示例存储库,并将为此设置了一个netcore2.1分支.
I have set up a sample repository for this related question and will set up a netcore2.1 branch for that.
推荐答案
我认为默认情况下,最新的Swagger(5.2.1)不会为操作生成OperationId
,因为它是
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是用于标识操作的可选唯一字符串.如果提供这些ID,则这些ID在API中描述的所有操作中必须唯一.
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.
但是,AutoRest似乎使用它来识别每种方法.我发现了 Github问题/问题,人们通过配置AutoRest来使用标签来解决此问题ID标识方法.
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使用operationId来确定给定API的类名称/方法名称.
AutoRest uses operationId to determine the class name/method name for a given API.
如果您不想使用标签,或者您的标签不够独特,可以通过以下方式要求Swagger将操作ID添加到生成的JSON中:
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);
这篇关于致命:所有操作均需要OperationId.请为路径的“获取"操作添加它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!