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&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"
}
}
}
},
I am trying to generate some client code from a swagger.json
file and the methods created by NSwag do not have any return types as you can see below:
Can anyone tell me what I might be doing wrong? This is my first time using NSwag to consume/generate client code for a 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&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();
}
}
Here is a piece from the swagger.json file. (Note, I have no control over how this file is created)
"/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"
}
}
}
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为修饰 api 操作方法的
ProducesResponseType
属性错过了指定返回对象的类型,即如下所示。您需要做的是指定 api 操作方法返回的对象类型,就像
一旦您完成了该操作,swagger.json 应该包含相关信息,并且 NSwag 应该能够生成预期的代码。
例如,您将在 swagger.json 的 responses 属性中看到以下附加位
This is because the
ProducesResponseType
attribute decorating your api action method has missed to specify the type of object being returned, i.e. something like the following.What you need to do is specify the type of object being returned by the api action method, like
Once you have that in place, the swagger.json should contain the relevant info and NSwag should be able to generate the expected code.
For e.g. you would see the following additional bit in responses property in your swagger.json
您的 API 方法 ApiFarmlandListAsync 应返回任务而不是任务类型。将 T 设置为您想要的返回类型。
Your API method ApiFarmlandListAsync should return Task instead of Task type. Set T to your desired return type.