Skip to content

Commit a6c29c2

Browse files
shwetashuklashwetashuklams
authored andcommitted
Update modelGeneric.mustache (OpenAPITools#5378)
* Update modelGeneric.mustache If maxlength is specified for a property type enum it there should be .ToString() appended before length check * ran the csharp petstore bar and updated the file Co-authored-by: Shweta Shukla <shshukl@microsoft.com>
1 parent 777b41b commit a6c29c2

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache

+24-2
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,44 @@ this.{{name}} = {{#lambda.camelcase_param}}{{name}}{{/lambda.camelcase_param}};
259259
{{/parent}}
260260
{{#vars}}
261261
{{#hasValidation}}
262+
{{#isEnum}}
263+
{{#maxLength}}
264+
// {{{name}}} ({{{dataType}}}) maxLength
265+
if(this.{{{name}}} != null && this.{{{name}}}.ToString().Length > {{maxLength}})
266+
{
267+
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be less than {{maxLength}}.", new [] { "{{{name}}}" });
268+
}
269+
{{/maxLength}}
270+
{{/isEnum}}
271+
{{^isEnum}}
262272
{{#maxLength}}
263273
// {{{name}}} ({{{dataType}}}) maxLength
264274
if(this.{{{name}}} != null && this.{{{name}}}.Length > {{maxLength}})
265275
{
266276
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be less than {{maxLength}}.", new [] { "{{{name}}}" });
267277
}
268-
269278
{{/maxLength}}
279+
{{/isEnum}}
280+
281+
{{#isEnum}}
282+
{{#minLength}}
283+
// {{{name}}} ({{{dataType}}}) minLength
284+
if(this.{{{name}}} != null && this.{{{name}}}.ToString().Length < {{minLength}})
285+
{
286+
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be greater than {{minLength}}.", new [] { "{{{name}}}" });
287+
}
288+
{{/minLength}}
289+
{{/isEnum}}
290+
{{^isEnum}}
270291
{{#minLength}}
271292
// {{{name}}} ({{{dataType}}}) minLength
272293
if(this.{{{name}}} != null && this.{{{name}}}.Length < {{minLength}})
273294
{
274295
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for {{{name}}}, length must be greater than {{minLength}}.", new [] { "{{{name}}}" });
275296
}
276-
277297
{{/minLength}}
298+
{{/isEnum}}
299+
278300
{{#maximum}}
279301
// {{{name}}} ({{{dataType}}}) maximum
280302
if(this.{{{name}}} > ({{{dataType}}}){{maximum}})

samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Model/FormatTest.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ public override int GetHashCode()
367367
/// <returns>Validation Result</returns>
368368
IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
369369
{
370+
371+
370372
// Integer (int) maximum
371373
if(this.Integer > (int)100)
372374
{
@@ -379,6 +381,8 @@ public override int GetHashCode()
379381
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Integer, must be a value greater than or equal to 10.", new [] { "Integer" });
380382
}
381383

384+
385+
382386
// Int32 (int) maximum
383387
if(this.Int32 > (int)200)
384388
{
@@ -391,6 +395,8 @@ public override int GetHashCode()
391395
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Int32, must be a value greater than or equal to 20.", new [] { "Int32" });
392396
}
393397

398+
399+
394400
// Number (decimal) maximum
395401
if(this.Number > (decimal)543.2)
396402
{
@@ -403,6 +409,8 @@ public override int GetHashCode()
403409
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Number, must be a value greater than or equal to 32.1.", new [] { "Number" });
404410
}
405411

412+
413+
406414
// Float (float) maximum
407415
if(this.Float > (float)987.6)
408416
{
@@ -415,6 +423,8 @@ public override int GetHashCode()
415423
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Float, must be a value greater than or equal to 54.3.", new [] { "Float" });
416424
}
417425

426+
427+
418428
// Double (double) maximum
419429
if(this.Double > (double)123.4)
420430
{
@@ -427,13 +437,17 @@ public override int GetHashCode()
427437
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Double, must be a value greater than or equal to 67.8.", new [] { "Double" });
428438
}
429439

440+
441+
430442
// String (string) pattern
431443
Regex regexString = new Regex(@"[a-z]", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
432444
if (false == regexString.Match(this.String).Success)
433445
{
434446
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for String, must match a pattern of " + regexString, new [] { "String" });
435447
}
436448

449+
450+
437451
// Password (string) maxLength
438452
if(this.Password != null && this.Password.Length > 64)
439453
{
@@ -445,7 +459,7 @@ public override int GetHashCode()
445459
{
446460
yield return new System.ComponentModel.DataAnnotations.ValidationResult("Invalid value for Password, length must be greater than 10.", new [] { "Password" });
447461
}
448-
462+
449463
yield break;
450464
}
451465
}

0 commit comments

Comments
 (0)