如何防止autorest在构造函数中接受null / nullable参数?

时间:2018-08-01 12:06:12

标签: c# swagger autorest

我正在使用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
}

这是招摇中类的定义:

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

这是使用自动休息自动生成的相同内容:

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在构造函数中生成可空参数?

1 个答案:

答案 0 :(得分:0)

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
}