1

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

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

这是来自的原始 C# 类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
}

这是招摇中的类的定义:

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

4

1 回答 1

0

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

/// <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
}
于 2018-08-03T16:22:18.177 回答