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

How to prevent autorest from accepting null / nullable parameters in the constructors?

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

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

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

/// <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中class的定义:

"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 坐标的服务。它要求使用此服务的客户检查这些 属性 中的每一个是否为 null,如果构造函数首先不接受这些 属性 可以自动避免。

问题

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

autorest没有考虑Required attribute. However, it does take into account the Required属性的JsonPropertyclass:

/// <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
}