如何防止autotest在构造函数中接受null/可为null的参数



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

然而,我注意到自动生成的类具有接受可为null值的构造函数。

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

问题

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

autorest不考虑Required属性。但是,它确实考虑了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
}

最新更新