【问题标题】:How to prevent autorest from accepting null / nullable parameters in the constructors?如何防止 autorest 在构造函数中接受 null / nullable 参数?
【发布时间】:2019-01-09 00:02:27
【问题描述】:

我正在使用 autorest 从 swagger.json 文件生成 C# 客户端。

但是,我注意到自动生成的类具有接受可为空值的构造函数。

这是swagger.json 来自的原始 C# 类:

/// <summary>
/// Defines the GPS coordinates model.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public sealed class GpsCoordinatesModel
{
    #region Properties

    /// <summary>
    /// Gets the latitude.
    /// </summary>
    [JsonProperty("latitude")]
    public double Latitude { get; }

    /// <summary>
    /// Gets the longitude.
    /// </summary>
    [JsonProperty("longitude")]
    public double Longitude { get; }

    #endregion

    #region Constructors

    /// <param name="latitude"></param>
    /// <param name="longitude"></param>
    /// <exception cref="ArgumentOutOfRangeException">
    /// Thrown if:
    /// - The Latitude is not within the interval [-90, 90].
    /// - The Longitude is not within the interval [-180, 180].
    /// </exception>
    [JsonConstructor]
    public GpsCoordinatesModel(double latitude, double longitude)
    {
        if (latitude < -90d || latitude > 90d)
            throw new ArgumentOutOfRangeException(nameof(latitude), latitude, $"The {nameof(latitude)} must be between -90 and 90.");

        if (longitude < -180d || longitude > 180d)
            throw new ArgumentOutOfRangeException(nameof(longitude), longitude, $"The {nameof(latitude)} must be between -180 and 180.");

        Latitude = latitude;
        Longitude = longitude;
    }

    #endregion
}

这是swagger中类的定义:

"GpsCoordinatesModel": {
    "type":"object",
    "properties": {
        "latitude": {  
            "format":"double",
            "type":"number",
            "readOnly":true
        },
        "longitude": {
            "format":"double",
            "type":"number",
            "readOnly":true
        }
    }       
}

这是使用 autorest 自动生成的这些:

public partial class GpsCoordinatesModel
{
    /// <summary>
    /// Initializes a new instance of the GpsCoordinatesModel class.
    /// </summary>
    public GpsCoordinatesModel()
    {
        CustomInit();
    }

    /// <summary>
    /// Initializes a new instance of the GpsCoordinatesModel class.
    /// </summary>
    public GpsCoordinatesModel(double? latitude = default(double?), double? longitude = default(double?))
    {
        Latitude = latitude;
        Longitude = longitude;
        CustomInit();
    }

    /// <summary>
    /// An initialization method that performs custom operations like setting defaults
    /// </summary>
    partial void CustomInit();

    /// <summary>
    /// </summary>
    [JsonProperty(PropertyName = "latitude")]
    public double? Latitude { get; private set; }

    /// <summary>
    /// </summary>
    [JsonProperty(PropertyName = "longitude")]
    public double? Longitude { get; private set; }

}

这不好,因为我不希望这些属性中的任何一个为空。

我有一项提供 GPS 坐标的服务。它要求使用此服务的客户端检查这些属性中的每一个是否为空,如果构造函数从不接受这些属性,则可以自动避免。

问题

有没有办法防止autorest在构造函数中生成可为空的参数?

【问题讨论】:

  • 如果将属性标记为 Required 会怎样?根据此 (github.com/Azure/autorest/issues/375)
  • @RyanWilson Nope :( 我尝试将 Required 属性放在 Properties 上,然后放在每个构造函数的参数上。没有任何效果。
  • @Krystof 这很奇怪。你用的是什么版本的autorest
  • @RyanWilson 一个相当新的 :) AutoRest 代码生成实用程序 [版本:2.0.4283;节点:v8.9.1]

标签: c# swagger autorest


【解决方案1】:

autorest 不考虑Required attribute. 但是,它确实考虑了JsonProperty 类的Required 属性:

/// <summary>
/// Defines the GPS coordinates model.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public sealed class GpsCoordinatesModel
{
    #region Properties

    /// <summary>
    /// Gets the latitude.
    /// </summary>
    [JsonProperty(PropertyName = "latitude", Required = Required.Always)]
    public double Latitude { get; }

    /// <summary>
    /// Gets the longitude.
    /// </summary>
    [JsonProperty(PropertyName = "longitude", Required = Required.Always)]
    public double Longitude { get; }

    #endregion

    #region Constructors

    /// <param name="latitude"></param>
    /// <param name="longitude"></param>
    /// <exception cref="ArgumentOutOfRangeException">
    /// Thrown if:
    /// - The Latitude is not within the interval [-90, 90].
    /// - The Longitude is not within the interval [-180, 180].
    /// </exception>
    [JsonConstructor]
    public GpsCoordinatesModel(double latitude, double longitude)
    {
        if (latitude < -90d || latitude > 90d)
            throw new ArgumentOutOfRangeException(nameof(latitude), latitude, $"The {nameof(latitude)} must be between -90 and 90.");

        if (longitude < -180d || longitude > 180d)
            throw new ArgumentOutOfRangeException(nameof(longitude), longitude, $"The {nameof(latitude)} must be between -180 and 180.");

        Latitude = latitude;
        Longitude = longitude;
    }

    #endregion
}

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多