AutoRest 为方法 return 类型生成可为空的布尔值
AutoRest generating nullable boolean for method return type
我在为 CSharp 生成 RestApiClient 时遇到问题。
在 Visual Studio AutoRest 中发现了问题,所以我下载了 AutoRest CLI 但生成了相同的错误方法 return 类型。
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
这是 Swagger.json 由 Swashbucke 5.6.0 从 WebApi
生成的
{
"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) ...
根据 autorest GitHub 上的这个问题:x-nullable not working for response primitives,autorest 生成器版本 2 存在一个问题,即它们不处理原始类型的 x-nullable。
并且由于现在有了新版本 (3) 的生成器,因此不会修复此问题。你能更新 autorest 并尝试使用新的生成器吗?
更新 2020-05-11:
我做了一个小测试(应该在回答之前完成,对此感到抱歉)并且似乎还没有实现对原始 return 类型的 x-nullable 的支持。所以有了autorest,你可能就倒霉了。
我用 nswag 试过你的 swagger.json:
nswag openapi2csclient /input:swagger.json /classname:UserApiClient /namespace:UserApi /output:UserApiClient.cs
并且它生成了一个具有正确 return 类型的方法:
public System.Threading.Tasks.Task<bool> HasUserAsync(string username)
我在为 CSharp 生成 RestApiClient 时遇到问题。 在 Visual Studio AutoRest 中发现了问题,所以我下载了 AutoRest CLI 但生成了相同的错误方法 return 类型。
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
这是 Swagger.json 由 Swashbucke 5.6.0 从 WebApi
生成的{
"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) ...
根据 autorest GitHub 上的这个问题:x-nullable not working for response primitives,autorest 生成器版本 2 存在一个问题,即它们不处理原始类型的 x-nullable。
并且由于现在有了新版本 (3) 的生成器,因此不会修复此问题。你能更新 autorest 并尝试使用新的生成器吗?
更新 2020-05-11:
我做了一个小测试(应该在回答之前完成,对此感到抱歉)并且似乎还没有实现对原始 return 类型的 x-nullable 的支持。所以有了autorest,你可能就倒霉了。
我用 nswag 试过你的 swagger.json:
nswag openapi2csclient /input:swagger.json /classname:UserApiClient /namespace:UserApi /output:UserApiClient.cs
并且它生成了一个具有正确 return 类型的方法:
public System.Threading.Tasks.Task<bool> HasUserAsync(string username)