繁体   English   中英

Swagger-Codegen C#:缺少类型为byte []的客户端成员

[英]Swagger-codegen c#: Missing client member of type byte[]

我正在使用java Swagger-Codegen类为展示固件发布服务的Swagger Web Api生成c#客户端。

生成的客户端DataContract类中缺少作为public async Task<IHttpActionResult> PublishPackageAsync(...) ApiController方法的参数所需的PackagePublishRequestInfoAndLel dto类的public Byte[] LelFile属性。

我做错什么了吗? 处理Byte[]属性有什么特别的事情吗? 还是在Swagger-Codegen Github项目中报告这个问题?

请注意, PackagePublishRequestInfo PackagePublishRequestInfoAndLel.PackagePublishRequestInfo成员属性正确显示在客户端中,只有Byte[] PackagePublishRequestInfoAndLel.LelFile成员属性在DataContract类中丢失。

感谢您的任何帮助。

以下是一些代码摘录:

LelController (ApiController)类公开了public async Task<IHttpActionResult> PublishPackageAsync()方法:(有关信息)

/// <summary>
/// The lel ApiController handling lel file publication.
/// </summary>
[System.Web.Http.RoutePrefix("api/Lels")]
[GenerateFactory(typeof(ILelControllerFactory))]
public class LelController : ApiController,
                             ILelController
{
    #region Fields

    private readonly ILelRepository lelRepository; // Repository to access the Datasets in the RED Database

    #endregion

    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="LelController"/> class.
    /// </summary>
    /// <param name="lelRepository">
    /// The lel repository.
    /// </param>
    public LelController(ILelRepository lelRepository)
    {
        this.lelRepository = lelRepository;
    }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// Publishes a new firmware package.
    /// </summary>
    /// <param name="packagePublishRequestInfoAndLel">
    /// The package publish info and the lel file content.
    /// </param>
    /// <returns>
    /// The returned IHttpActionResult
    /// </returns>
    [System.Web.Http.Route("")]
    [SwaggerResponse(HttpStatusCode.Created, "Package succesfully published.")]
    [SwaggerResponse(HttpStatusCode.NotFound, "Unable to publish posted package data. (Detailed error message available in the response body).")]
    [SwaggerResponse(HttpStatusCode.InternalServerError, "Internal server error. Package not published. An HttpException is returned with the original inner exception thrown. (Detailed error message available in the returned exception).", typeof(HttpException))]
    public async Task<IHttpActionResult> PublishPackageAsync(PackagePublishRequestInfoAndLel packagePublishRequestInfoAndLel)
    {
        // Try to publish the package:
        await this.lelRepository.PublishPackageAsync(packagePublishRequestInfoAndLel);
        return this.Content(HttpStatusCode.Created, "Package successfully published.");
    }

    #endregion
}

PackagePublishRequestInfoAndLel dto类,包括byte[] LelFile属性:

public class PackagePublishRequestInfoAndLel : IPackagePublishRequestInfoAndLel
{
    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="PackagePublishRequestInfoAndLel"/> class.
    /// </summary>
    public PackagePublishRequestInfoAndLel()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="PackagePublishRequestInfoAndLel"/> class.
    /// </summary>
    /// <param name="packagePublishRequestInfo">
    /// The package Publish Request Info.
    /// </param>
    /// <param name="lelFile">
    /// The lel file.
    /// </param>
    public PackagePublishRequestInfoAndLel(PackagePublishRequestInfo packagePublishRequestInfo,
                                           byte[] lelFile)
    {
        this.LelFile = lelFile;
        this.PackagePublishRequestInfo = packagePublishRequestInfo;
    }

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets or sets the lel file
    /// </summary>
    [Required(AllowEmptyStrings = false, ErrorMessage = ValidationConstants.LelFileIsRequired)]
    public byte[] LelFile { get; set; } // The LelFile property missing in the Swagger-Codegen generated client DataContract.

    /// <summary>
    /// Gets or sets the package publish request info
    /// </summary>
    public PackagePublishRequestInfo PackagePublishRequestInfo { get; set; }

    #endregion
}

Swagger JSon中的PackagePublishRequestInfoAndLel定义:(正确包含类型为“ Byte []”且格式为“字符串”的LelFile属性)

"PackagePublishRequestInfoAndLel": {
  "required": [
    "LelFile"
  ],
  "type": "object",
  "properties": {
    "LelFile": {
      "format": "string",
      "type": "byte[]"
    },
    "PackagePublishRequestInfo": {
      "$ref": "#/definitions/PackagePublishRequestInfo"
    }
  }
},
"PackagePublishRequestInfo": {
  "required": [
    "UserIdent"
  ],
  "type": "object",
  "properties": {
    "Override": {
      "type": "boolean"
    },
    "BootManagerVersion": {
      "type": "string"
    },
    "BootloaderVersion": {
      "type": "string"
    },
    "EcuIdent": {
      "type": "string"
    },
    "HardwareVersion": {
      "format": "int32",
      "type": "integer"
    },
    "LelFileName": {
      "pattern": "^[0-9]{8}_[0-9]{3}\\.lel$",
      "type": "string"
    },
    "SoftwareVersion": {
      "type": "string"
    },
    "SpfVersion": {
      "type": "string"
    },
    "Status": {
      "type": "string"
    },
    "UserIdent": {
      "format": "int32",
      "type": "integer"
    },
    "DatasetIdent": {
      "type": "string"
    },
    "DatasetRevision": {
      "format": "int32",
      "type": "integer"
    }
  }
},

最后,不完整的Swagger-Codegen生成了客户端DataContract:

namespace IO.Swagger.Model
{
  [DataContract]
  public class PackagePublishRequestInfoAndLel : IEquatable<PackagePublishRequestInfoAndLel>
  {
    [DataMember(EmitDefaultValue = false, Name = "PackagePublishRequestInfo")]
    public PackagePublishRequestInfo PackagePublishRequestInfo { get; set; }

    // Here, missing public Byte[] LelFile DataMember !!!


    public override string ToString()
    {
      StringBuilder stringBuilder = new StringBuilder();
      stringBuilder.Append("class PackagePublishRequestInfoAndLel {\n");
      stringBuilder.Append("  PackagePublishRequestInfo: ").Append((object) this.PackagePublishRequestInfo).Append("\n");
      stringBuilder.Append("}\n");
      return ((object) stringBuilder).ToString();
    }

    public string ToJson()
    {
      return JsonConvert.SerializeObject((object) this, Formatting.Indented);
    }

    public override bool Equals(object obj)
    {
      return this.Equals(obj as PackagePublishRequestInfoAndLel);
    }

    public bool Equals(PackagePublishRequestInfoAndLel other)
    {
      if (other == null)
        return false;
      if (this.PackagePublishRequestInfo == other.PackagePublishRequestInfo)
        return true;
      if (this.PackagePublishRequestInfo != null)
        return this.PackagePublishRequestInfo.Equals(other.PackagePublishRequestInfo);
      else
        return false;
    }

    public override int GetHashCode()
    {
      int num = 41;
      if (this.PackagePublishRequestInfo != null)
        num = num * 57 + this.PackagePublishRequestInfo.GetHashCode();
      return num;
    }
  }
}

附录:

我猜问题可能出在byte []类型的Swashbuckle / Swagger映射中。 所以这是我的SwaggerConfig类代码:( byte []类型的映射可能是错误的...)

public class SwaggerConfig
{
    #region Public Methods and Operators

    /// <summary>
    /// Register the configuration for Swashbuckle (Swagger .Net)
    /// </summary>
    /// <param name="config">
    ///     The global configuration
    /// </param>
    public static void Register(HttpConfiguration config)
    {
        config.EnableSwagger(c =>
                                 {
                                     c.SingleApiVersion("v1", "RedFull.Api")
                                      .Description(@"An API for accessing the Firmware Database services."
                                                   + "(lel file publishing and firmware package retrieval)"
                                                   + "<br /><br />Service events viewer is available on : <a href='/elmah.axd'>ELMAH</a> page")
                                      .TermsOfService("Terms of service: Reserved for internal usage.")
                                      .Contact(cc => cc
                                                         .Name("Firmware Services"
                                                         .Url("https://xxxxx.com/yyyy/Home.aspx")
                                                         .Email("xxx.yyy@gmail.com"))
                                      .License(lc => lc
                                                         .Name("Usage License")
                                                         .Url("http://xxxxx.com/license"));

                                     c.MapType<byte[]>(() => new Schema
                                                                 {
                                                                     type = "byte[]",
                                                                     format = "string"
                                                                 });
...

我也尝试通过这种方式声明映射:

                                 c.MapType<byte[]>(() => new Schema
                                 {
                                     type = "array",
                                     format = "string",
                                     items = new Schema()
                                                 {
                                                     type = "byte",
                                                     format = "int32"
                                                 }
                                 });

但是在启动swagger-codegen生成器时,此代码导致异常:

Exception in thread "main" java.lang.RuntimeException: Could not generate model
'PackagePublishRequestInfoAndLel'
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:21
5)
        at io.swagger.codegen.cmd.Generate.run(Generate.java:188)
        at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)
Caused by: java.lang.NullPointerException
        at io.swagger.codegen.languages.CSharpClientCodegen.getSwaggerType(CShar
pClientCodegen.java:246)
        at io.swagger.codegen.DefaultCodegen.getTypeDeclaration(DefaultCodegen.j
ava:714)
        at io.swagger.codegen.languages.CSharpClientCodegen.getTypeDeclaration(C
SharpClientCodegen.java:239)
        at io.swagger.codegen.languages.CSharpClientCodegen.getTypeDeclaration(C
SharpClientCodegen.java:232)
        at io.swagger.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:10
32)
        at io.swagger.codegen.DefaultCodegen.addVars(DefaultCodegen.java:1868)
        at io.swagger.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:845)
        at io.swagger.codegen.DefaultGenerator.processModels(DefaultGenerator.ja
va:695)
        at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:18
9)
        ... 2 more

Swagger-Codegen的最新版本支持binary格式(在C#API客户端中映射为byte []),它将包含在即将发布的稳定版本2.1.6中。

请暂时拉最新的master,以获得C#API客户端中byte []的支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM