如何使用 Swagger API (JSON) 到 C# 代码生成器(如 SwaggerHub 或 NSwagStudio)为更大的 API(如 Clio)生成 C# 客户端代码?(https://app.clio.com/api_v4.json)。
这些工具似乎适用于较小的 API,但是当您将大型模式放入其中时,它们会输出无法编译且似乎存在多个问题的代码。
/// <summary>Return the data for all triggers</summary>
/// <param name="x_API_VERSION">The [API minor version](#section/Minor-Versions). Default: latest version.</param>
/// <param name="x_BULK">An indicator if [bulk actions](#section/Bulk-Actions) should be performed.
/// When performing a bulk action, the id path parameter is not required.</param>
/// <param name="fields">The fields to be returned. See response samples for what fields are available. For more information see the [fields section](#section/Fields).</param>
/// <param name="is_requirements_required">Filter JurisdictionsToTrigger records to those which require addition requirements to be checked (usually specifying trigger time).</param>
/// <param name="is_served">Filter JurisdictionsToTrigger records to those which require a service type to be selected.</param>
/// <param name="jurisdiction_id">The unique identifier for the Jurisdiction.</param>
/// <param name="limit">A limit on the number of JurisdictionsToTrigger records to be returned. Limit can range between 1 and 200. Default: `200`.</param>
/// <param name="page_token">A token specifying which page to return.</param>
/// <param name="query">Wildcard search for `description` matching a given string.</param>
/// <returns>Ok</returns>
/// <exception cref="ClioAPIException">A server side error occurred.</exception>
public System.Threading.Tasks.Task<JurisdictionsToTriggerList> CourtRulesJurisdictionsId}Triggers.jsonAsync(string x_API_VERSION, bool? x_BULK, string fields, bool? is_requirements_required, bool? is_served, int jurisdiction_id, int? limit, string page_token, string query)
{
return CourtRulesJurisdictionsId}Triggers.jsonAsync(x_API_VERSION, x_BULK, fields, is_requirements_required, is_served, jurisdiction_id, limit, page_token, query, System.Threading.CancellationToken.None);
}
例如,在上面的例程中,它在名称CourtRulesJurisdictions}Triggers中添加了一个“}”
我已经为这个特定的 API 尝试了 SwaggerHub 和 NSwagStudio,但都没有工作。NSwagStudio 存在上述问题,而 SwaggerHub 生成的代码存在此问题。在获取数据的客户端 API 调用结束时,对JsonConvert.DeserializeObject的调用失败。数据在响应中。我在调试器中看到的内容,并且类型设置为正确的模型,但模型中没有放置数据。
try
{
return JsonConvert.DeserializeObject(response.Content, type, serializerSettings);
}
catch (Exception e)
{
throw new ApiException(500, e.Message);
}
我将代码简化为这个,它甚至不使用任何东西,只使用生成的模型并且它失败了。
Dim x = "{""data"":{""name"":""Fred G. Jones"",""last_name"":""Jones"",""id"":345171548}}"
u = Newtonsoft.Json.JsonConvert.DeserializeObject(x, GetType(Clio_API.Model.UserShow))
这是生成的模型UserShow
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.ComponentModel.DataAnnotations;
using SwaggerDateConverter = Clio_API.Client.SwaggerDateConverter;
namespace Clio_API.Model
{
/// <summary>
/// UserShow
/// </summary>
[DataContract]
public partial class UserShow : IEquatable<UserShow>, IValidatableObject
{
/// <summary>
/// Initializes a new instance of the <see cref="UserShow" /> class.
/// </summary>
[JsonConstructorAttribute]
protected UserShow() { }
/// <summary>
/// Initializes a new instance of the <see cref="UserShow" /> class.
/// </summary>
/// <param name="Data">User Object Response (required).</param>
public UserShow(User Data = default(User))
{
// to ensure "Data" is required (not null)
if (Data == null)
{
throw new InvalidDataException("Data is a required property for UserShow and cannot be null");
}
else
{
this.Data = Data;
}
}
/// <summary>
/// User Object Response
/// </summary>
/// <value>User Object Response</value>
[DataMember(Name="data", EmitDefaultValue=false)]
public User Data { get; set; }
/// <summary>
/// Returns the string presentation of the object
/// </summary>
/// <returns>String presentation of the object</returns>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("class UserShow {\n");
sb.Append(" Data: ").Append(Data).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
/// <returns>JSON string presentation of the object</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
/// <summary>
/// Returns true if objects are equal
/// </summary>
/// <param name="input">Object to be compared</param>
/// <returns>Boolean</returns>
public override bool Equals(object input)
{
return this.Equals(input as UserShow);
}
/// <summary>
/// Returns true if UserShow instances are equal
/// </summary>
/// <param name="input">Instance of UserShow to be compared</param>
/// <returns>Boolean</returns>
public bool Equals(UserShow input)
{
if (input == null)
return false;
return
(
this.Data == input.Data ||
(this.Data != null &&
this.Data.Equals(input.Data))
);
}
/// <summary>
/// Gets the hash code
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hashCode = 41;
if (this.Data != null)
hashCode = hashCode * 59 + this.Data.GetHashCode();
return hashCode;
}
}
/// <summary>
/// To validate all properties of the instance
/// </summary>
/// <param name="validationContext">Validation context</param>
/// <returns>Validation Result</returns>
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
yield break;
}
}
}