【问题标题】:AutoRest generating nullable boolean for method return typeAutoRest 为方法返回类型生成可为空的布尔值
【发布时间】:2020-07-28 01:29:45
【问题描述】:

我在为 CSharp 生成 RestApiClient 时遇到问题。 在 Visual Studio AutoRest 中发现了问题,因此我下载了 AutoRest CLI,但生成了相同的错误方法返回类型。

AutoRest code generation utility [cli version: 3.0.6187; node: v12.14.1, max-memory: 8192 gb]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest


Showing All Installed Extensions

 Type       Extension Name                           Version 
 core       @autorest/core                           3.0.6274     
 core       @microsoft.azure/autorest-core           2.0.4417     
 extension  @microsoft.azure/autorest.csharp         2.3.84      
 extension  @microsoft.azure/autorest.modeler        2.3.55       

这是 Swashbucke 5.6.0 从 WebApi 生成的 Swagger.json

{
    "swagger": "2.0",
    "info": {
        "version": "v1",
        "title": "Server"
    },
    "host": "localhost:5992",
    "schemes": [
        "http"
    ],
    "paths": {
        "/api/User/HasUser": {
            "post": {
                "tags": [
                    "User"
                ],
                "operationId": "User_HasUser",
                "consumes": [],
                "produces": [
                    "application/json",
                    "text/json",
                    "application/xml",
                    "text/xml"
                ],
                "parameters": [
                    {
                        "name": "username",
                        "in": "query",
                        "required": true,
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                            "schema": {
                              "x-nullable": false,
                              "type": "boolean"
                            }
                    }
                }
            }
        }
    },
    "definitions": {}
}

带参数的命令

c:\> autorest --input-file=swagger.json --output-folder=autorest --csharp --clear-output-folder

AutoRest 的输出

namespace Api
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Rest;

    public static partial class UserExtensions
    {
            public static bool? HasUser(this IUser operations, string username) ...    
    }
}

我期待“bool”而不是“bool”?

public static bool HasUser(this IUser operations, string username) ...    

【问题讨论】:

标签: c# visual-studio-2019 autorest


【解决方案1】:

根据 autorest GitHub 上的这个问题:x-nullable not working for response primitives,autorest 生成器版本 2 存在一个问题,即它们不能处理原始类型的 x-nullable。

而且由于现在有一个新版本 (3) 的生成器,这将不会被修复。您能否更新 autorest 并尝试使用新的生成器?

2020 年 5 月 11 日更新:

我做了一个小测试(应该在回答之前完成,对此感到抱歉)并且似乎还没有实现对原始返回类型的 x-nullable 的支持。因此,使用 autorest,您可能会不走运。

我用 nswag 试过你的 swagger.json:

nswag openapi2csclient /input:swagger.json /classname:UserApiClient /namespace:UserApi /output:UserApiClient.cs

它会生成一个返回类型正确的方法:

public System.Threading.Tasks.Task<bool> HasUserAsync(string username)

【讨论】:

  • 你能说得更具体点吗?我尝试使用 v3 开关 autorest --input-file=swagger.json --output-folder=autorest --csharp --clear-output-folder --v3 进行最新的 autorest,但它不起作用。
  • 天哪,我还有-50分只是为了问问。你的答案是正确的。也许我想念你的回应。您的回答是正确的,并被标记为好答案并被赞成。很抱歉错过了赏金。
  • @Mertuarez,没问题。赏金和积分很好,但帮助他人才是最重要的:-)。不过,autorest 落后太糟糕了。我们以前经常使用 autorest,但由于 autorest 在功能上一直落后,因此不再使用它(并移至 nswag)。
猜你喜欢
  • 2015-09-10
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 2012-11-03
  • 2013-02-16
  • 2018-01-24
相关资源
最近更新 更多