NSwag Studio C#生成的客户端代码没有分配任何返回类型



我正在尝试从swagger.json文件生成一些客户端代码,NSwag创建的方法没有任何返回类型,如下所示:

有人能告诉我我可能做错了什么吗?这是我第一次使用NSwag为swagger API消费/生成客户端代码。

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <param name="wallet">The Wallet Address, that the operation should be performed on.</param>
/// <param name="page">Show this page number.</param>
/// <param name="limit">Show this many results per page.</param>
/// <param name="sortAsc">Sort by this field, ascending.</param>
/// <param name="sortDesc">Sort by this field, descending.</param>
/// <param name="filter">Filter by these field=value combinations. Performs an exact string comparison. e.g.
/// <br/>- AND - `?filter=name=Bob&amp;filter=age=30`
/// <br/>- OR - `?filter=id=[1,abc,true]`
/// <br/>- NESTED - `?filter=params.owner=1`</param>
/// <returns>Successful</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task ApiFarmlandListAsync(string wallet = null, double? page = null, double? limit = null, string sortAsc = null, string sortDesc = null, System.Collections.Generic.IEnumerable<string> filter = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/farmland/list?");
if (wallet != null)
{
urlBuilder_.Append(System.Uri.EscapeDataString("wallet") + "=").Append(System.Uri.EscapeDataString(ConvertToString(wallet, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
}
if (page != null)
{
urlBuilder_.Append(System.Uri.EscapeDataString("page") + "=").Append(System.Uri.EscapeDataString(ConvertToString(page, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
}
if (limit != null)
{
urlBuilder_.Append(System.Uri.EscapeDataString("limit") + "=").Append(System.Uri.EscapeDataString(ConvertToString(limit, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
}
if (sortAsc != null)
{
urlBuilder_.Append(System.Uri.EscapeDataString("sortAsc") + "=").Append(System.Uri.EscapeDataString(ConvertToString(sortAsc, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
}
if (sortDesc != null)
{
urlBuilder_.Append(System.Uri.EscapeDataString("sortDesc") + "=").Append(System.Uri.EscapeDataString(ConvertToString(sortDesc, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
}
if (filter != null)
{
foreach (var item_ in filter) { urlBuilder_.Append(System.Uri.EscapeDataString("filter") + "=").Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }
}
urlBuilder_.Length--;
var client_ = new System.Net.Http.HttpClient();
var disposeClient_ = true;
try
{
using (var request_ = new System.Net.Http.HttpRequestMessage())
{
request_.Method = new System.Net.Http.HttpMethod("GET");
PrepareRequest(client_, request_, urlBuilder_);
var url_ = urlBuilder_.ToString();
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
PrepareRequest(client_, request_, url_);
var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var disposeResponse_ = true;
try
{
var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}
ProcessResponse(client_, response_);
var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
return;
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
}
}
finally
{
if (disposeResponse_)
response_.Dispose();
}
}
}
finally
{
if (disposeClient_)
client_.Dispose();
}
}

这是swagger.json文件中的一段。(注意,我无法控制这个文件是如何创建的(

"/api/farmland/list": {
"get": {
"produces": [
"application/json"
],
"parameters": [
{
"$ref": "#/components/parameters/WalletParameter"
},
{
"$ref": "#/components/parameters/PageParameter"
},
{
"$ref": "#/components/parameters/LimitParameter"
},
{
"$ref": "#/components/parameters/SortAscParameter"
},
{
"$ref": "#/components/parameters/SortDescParameter"
},
{
"$ref": "#/components/parameters/FilterParameter"
}
],
"tags": [
"Farmland"
],
"responses": {
"200": {
"description": "Successful"
}
}
}
},

这是因为装饰api操作方法的ProducesResponseType属性遗漏了指定返回对象类型的属性,例如以下内容。

[ProducesResponseType(StatusCodes.Status200OK)]

您需要做的是指定api操作方法返回的对象类型,如

[ProducesResponseType(typeof(YourExpectedResponseType[]), StatusCodes.Status200OK)]

一旦你有了这些,swagger.json应该包含相关的信息,NSwag应该能够生成预期的代码。

例如,您将在swagger.json 中的响应属性中看到以下附加位

"responses": {
"200": {
"description": "Successful",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/<YourExpectedResponseType>"
}
}
}
}
}
}

您的API方法ApiFarmlandListAsync应返回Task而不是Task类型。将T设置为您想要的返回类型。

相关内容

  • 没有找到相关文章

最新更新