Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/vanilla/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ public static string CheckNull(string valueReference, string executionBlock)
/// <param name="type">The type to validate</param>
/// <param name="scope">A scope provider for generating variable names as necessary</param>
/// <param name="valueReference">A reference to the value being validated</param>
/// <param name="isNullable">Indicates whether the parameter or property is expressed as a nullable type or not</param>
/// <param name="constraints">Constraints</param>
/// <returns>The code to validate the reference of the given type</returns>
public static string ValidateType(this IModelType type, IChild scope, string valueReference,
public static string ValidateType(this IModelType type, IChild scope, string valueReference, bool isNullable,
Dictionary<Constraint, string> constraints)
{
if (scope == null)
Expand All @@ -346,7 +347,7 @@ public static string ValidateType(this IModelType type, IChild scope, string val
if (sequence != null && sequence.ShouldValidateChain())
{
var elementVar = scope.GetUniqueName("element");
var innerValidation = sequence.ElementType.ValidateType(scope, elementVar, null);
var innerValidation = sequence.ElementType.ValidateType(scope, elementVar, false, null);
if (!string.IsNullOrEmpty(innerValidation))
{
sb.AppendLine("foreach (var {0} in {1})", elementVar, valueReference)
Expand All @@ -358,7 +359,7 @@ public static string ValidateType(this IModelType type, IChild scope, string val
else if (dictionary != null && dictionary.ShouldValidateChain())
{
var valueVar = scope.GetUniqueName("valueElement");
var innerValidation = dictionary.ValueType.ValidateType(scope, valueVar, null);
var innerValidation = dictionary.ValueType.ValidateType(scope, valueVar, false, null);
if (!string.IsNullOrEmpty(innerValidation))
{
sb.AppendLine("foreach (var {0} in {1}.Values)", valueVar, valueReference)
Expand All @@ -370,7 +371,7 @@ public static string ValidateType(this IModelType type, IChild scope, string val

if (sb.ToString().Trim().Length > 0)
{
if (type.IsValueType())
if (type.IsValueType() && !isNullable)
{
return sb.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
if(parameter.CanBeValidated && (Model.HttpMethod != HttpMethod.Patch || parameter.Location != ParameterLocation.Body))
{
@:@(parameter.ModelType.ValidateType(Model, parameter.Name, parameter.Constraints))
@:@(parameter.ModelType.ValidateType(Model, parameter.Name, parameter.IsNullable(), parameter.Constraints))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vanilla/Templates/Rest/Common/ModelTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ foreach (PropertyCs property in Model.InstanceProperties.Where(p => p.IsRequired
foreach (var property in Model.InstanceProperties.Where(p => p.Constraints.Any() || !(p.ModelType is PrimaryType)))
{
anythingToValidate = true;
@:@property.ModelType.ValidateType(Model, $"this.{property.Name}", property.Constraints)
@:@property.ModelType.ValidateType(Model, $"this.{property.Name}", property.IsNullable(), property.Constraints)
}
if (!anythingToValidate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
}
if (parameter.CanBeValidated && (Model.HttpMethod != HttpMethod.Patch || parameter.Location != ParameterLocation.Body))
{
@:@(parameter.ModelType.ValidateType(Model, parameter.Name, parameter.Constraints))
@:@(parameter.ModelType.ValidateType(Model, parameter.Name, parameter.IsNullable(), parameter.Constraints))
}
}
@if (Model.HttpMethod == HttpMethod.Get)
Expand Down
40 changes: 40 additions & 0 deletions test/Resource/Bug885/Bug885.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Simple API",
"termsOfService": "http://helloreverb.com/terms/",
"contact": {
"email": "[email protected]"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"host": "test.swagger.io",
"basePath": "/v2",
"schemes": [
"http"
],
"paths": {},
"definitions": {
"DateAfterModification": {
"properties": {
"daysAfterModificationGreaterThan": {
"type": "number",
"multipleOf": 1.0,
"minimum": 0,
"description": "Value indicating the age in days after last modification"
},
"daysAfterLastAccessTimeGreaterThan": {
"type": "number",
"multipleOf": 1.0,
"minimum": 0,
"description": "Value indicating the age in days after last blob access. This property can only be used in conjuction with last access time tracking policy"
}
},
"description": "Object to define the number of days after object last modification Or last access. Properties daysAfterModificationGreaterThan and daysAfterLastAccessTimeGreaterThan are mutually exclusive"
}
}
}
33 changes: 33 additions & 0 deletions test/Resource/Bug885/ISimpleAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>

namespace Bug885
{
using Models;
using Newtonsoft.Json;

/// <summary>
/// </summary>
public partial interface ISimpleAPI : System.IDisposable
{
/// <summary>
/// The base URI of the service.
/// </summary>
System.Uri BaseUri { get; set; }

/// <summary>
/// Gets or sets json serialization settings.
/// </summary>
JsonSerializerSettings SerializationSettings { get; }

/// <summary>
/// Gets or sets json deserialization settings.
/// </summary>
JsonSerializerSettings DeserializationSettings { get; }


}
}
95 changes: 95 additions & 0 deletions test/Resource/Bug885/Models/DateAfterModification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>

namespace Bug885.Models
{
using Microsoft.Rest;
using Newtonsoft.Json;
using System.Linq;

/// <summary>
/// Object to define the number of days after object last modification Or
/// last access. Properties daysAfterModificationGreaterThan and
/// daysAfterLastAccessTimeGreaterThan are mutually exclusive
/// </summary>
public partial class DateAfterModification
{
/// <summary>
/// Initializes a new instance of the DateAfterModification class.
/// </summary>
public DateAfterModification()
{
CustomInit();
}

/// <summary>
/// Initializes a new instance of the DateAfterModification class.
/// </summary>
/// <param name="daysAfterModificationGreaterThan">Value indicating the
/// age in days after last modification</param>
/// <param name="daysAfterLastAccessTimeGreaterThan">Value indicating
/// the age in days after last blob access. This property can only be
/// used in conjuction with last access time tracking policy</param>
public DateAfterModification(double? daysAfterModificationGreaterThan = default(double?), double? daysAfterLastAccessTimeGreaterThan = default(double?))
{
DaysAfterModificationGreaterThan = daysAfterModificationGreaterThan;
DaysAfterLastAccessTimeGreaterThan = daysAfterLastAccessTimeGreaterThan;
CustomInit();
}

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

/// <summary>
/// Gets or sets value indicating the age in days after last
/// modification
/// </summary>
[JsonProperty(PropertyName = "daysAfterModificationGreaterThan")]
public double? DaysAfterModificationGreaterThan { get; set; }

/// <summary>
/// Gets or sets value indicating the age in days after last blob
/// access. This property can only be used in conjuction with last
/// access time tracking policy
/// </summary>
[JsonProperty(PropertyName = "daysAfterLastAccessTimeGreaterThan")]
public double? DaysAfterLastAccessTimeGreaterThan { get; set; }

/// <summary>
/// Validate the object.
/// </summary>
/// <exception cref="ValidationException">
/// Thrown if validation fails
/// </exception>
public virtual void Validate()
{
if (DaysAfterModificationGreaterThan != null)
{
if (DaysAfterModificationGreaterThan < 0)
{
throw new ValidationException(ValidationRules.InclusiveMinimum, "DaysAfterModificationGreaterThan", 0);
}
if (DaysAfterModificationGreaterThan % 1 != 0)
{
throw new ValidationException(ValidationRules.MultipleOf, "DaysAfterModificationGreaterThan", 1);
}
}
if (DaysAfterLastAccessTimeGreaterThan != null)
{
if (DaysAfterLastAccessTimeGreaterThan < 0)
{
throw new ValidationException(ValidationRules.InclusiveMinimum, "DaysAfterLastAccessTimeGreaterThan", 0);
}
if (DaysAfterLastAccessTimeGreaterThan % 1 != 0)
{
throw new ValidationException(ValidationRules.MultipleOf, "DaysAfterLastAccessTimeGreaterThan", 1);
}
}
}
}
}
156 changes: 156 additions & 0 deletions test/Resource/Bug885/SimpleAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// <auto-generated>
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>

namespace Bug885
{
using Microsoft.Rest;
using Microsoft.Rest.Serialization;
using Models;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;

public partial class SimpleAPI : ServiceClient<SimpleAPI>, ISimpleAPI
{
/// <summary>
/// The base URI of the service.
/// </summary>
public System.Uri BaseUri { get; set; }

/// <summary>
/// Gets or sets json serialization settings.
/// </summary>
public JsonSerializerSettings SerializationSettings { get; private set; }

/// <summary>
/// Gets or sets json deserialization settings.
/// </summary>
public JsonSerializerSettings DeserializationSettings { get; private set; }

/// <summary>
/// Initializes a new instance of the SimpleAPI class.
/// </summary>
/// <param name='httpClient'>
/// HttpClient to be used
/// </param>
/// <param name='disposeHttpClient'>
/// True: will dispose the provided httpClient on calling SimpleAPI.Dispose(). False: will not dispose provided httpClient</param>
public SimpleAPI(HttpClient httpClient, bool disposeHttpClient) : base(httpClient, disposeHttpClient)
{
Initialize();
}

/// <summary>
/// Initializes a new instance of the SimpleAPI class.
/// </summary>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
public SimpleAPI(params DelegatingHandler[] handlers) : base(handlers)
{
Initialize();
}

/// <summary>
/// Initializes a new instance of the SimpleAPI class.
/// </summary>
/// <param name='rootHandler'>
/// Optional. The http client handler used to handle http transport.
/// </param>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
public SimpleAPI(HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : base(rootHandler, handlers)
{
Initialize();
}

/// <summary>
/// Initializes a new instance of the SimpleAPI class.
/// </summary>
/// <param name='baseUri'>
/// Optional. The base URI of the service.
/// </param>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
/// <exception cref="System.ArgumentNullException">
/// Thrown when a required parameter is null
/// </exception>
public SimpleAPI(System.Uri baseUri, params DelegatingHandler[] handlers) : this(handlers)
{
if (baseUri == null)
{
throw new System.ArgumentNullException("baseUri");
}
BaseUri = baseUri;
}

/// <summary>
/// Initializes a new instance of the SimpleAPI class.
/// </summary>
/// <param name='baseUri'>
/// Optional. The base URI of the service.
/// </param>
/// <param name='rootHandler'>
/// Optional. The http client handler used to handle http transport.
/// </param>
/// <param name='handlers'>
/// Optional. The delegating handlers to add to the http client pipeline.
/// </param>
/// <exception cref="System.ArgumentNullException">
/// Thrown when a required parameter is null
/// </exception>
public SimpleAPI(System.Uri baseUri, HttpClientHandler rootHandler, params DelegatingHandler[] handlers) : this(rootHandler, handlers)
{
if (baseUri == null)
{
throw new System.ArgumentNullException("baseUri");
}
BaseUri = baseUri;
}

/// <summary>
/// An optional partial-method to perform custom initialization.
///</summary>
partial void CustomInitialize();
/// <summary>
/// Initializes client properties.
/// </summary>
private void Initialize()
{
BaseUri = new System.Uri("http://test.swagger.io/v2");
SerializationSettings = new JsonSerializerSettings
{
Formatting = Newtonsoft.Json.Formatting.Indented,
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter>
{
new Iso8601TimeSpanConverter()
}
};
DeserializationSettings = new JsonSerializerSettings
{
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter>
{
new Iso8601TimeSpanConverter()
}
};
CustomInitialize();
}
}
}
Loading