Skip to content

Commit

Permalink
utilize LengthAttribute of .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
zijianhuang committed Jun 27, 2024
1 parent 48078fd commit 9e2d974
Show file tree
Hide file tree
Showing 305 changed files with 7,994 additions and 15,908 deletions.
30 changes: 22 additions & 8 deletions Fonlow.OpenApiClientGen.ClientTypes/ComponentsToTypesBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void CreateCodeDomForComponents(OpenApiComponents components)
var groupedComponentsSchemas = ComponentsSchemas
.GroupBy(d => NameFunc.GetNamespaceOfClassName(d.Key))
.OrderBy(k => k.Key); // always sort namespaces if there are multiple ones
//var namespacesOfTypes = groupedComponentsSchemas.Select(d => d.Key).ToArray();
//var namespacesOfTypes = groupedComponentsSchemas.Select(d => d.Key).ToArray();
foreach (var groupedTypes in groupedComponentsSchemas)
{
var classNamespaceText = groupedTypes.Key;
Expand Down Expand Up @@ -589,15 +589,22 @@ public CodeTypeReference PropertySchemaToCodeTypeReference(OpenApiSchema propert

protected virtual void AddValidationAttributes(OpenApiSchema fieldSchema, CodeMemberField memberField)
{
if (fieldSchema.MinLength.HasValue)
if (fieldSchema.MinLength.HasValue && fieldSchema.MaxLength.HasValue)
{
CodeSnippetExpression minLen = new(fieldSchema.MinLength.Value.ToString());
CodeSnippetExpression maxLen = new(fieldSchema.MaxLength.Value.ToString());
CodeAttributeArgument[] attributeParams = new CodeAttributeArgument[] { new CodeAttributeArgument(minLen), new CodeAttributeArgument(maxLen) };
CodeAttributeDeclaration cad = new("System.ComponentModel.DataAnnotations.Length", attributeParams); //.NET 8 feature
memberField.CustomAttributes.Add(cad);
}
else if (fieldSchema.MinLength.HasValue)
{
CodeSnippetExpression len = new(fieldSchema.MinLength.Value.ToString());
CodeAttributeArgument[] attributeParams = new CodeAttributeArgument[] { new CodeAttributeArgument(len) };
CodeAttributeDeclaration cad = new("System.ComponentModel.DataAnnotations.MinLength", attributeParams);
memberField.CustomAttributes.Add(cad);
}

if (fieldSchema.MaxLength.HasValue)
else if (fieldSchema.MaxLength.HasValue)
{
CodeSnippetExpression len = new(fieldSchema.MaxLength.Value.ToString());
CodeAttributeArgument[] attributeParams = new CodeAttributeArgument[] { new CodeAttributeArgument(len) };
Expand Down Expand Up @@ -642,15 +649,22 @@ protected virtual void AddValidationAttributes(OpenApiSchema fieldSchema, CodeMe
memberField.CustomAttributes.Add(cad);
}

if (fieldSchema.MinItems.HasValue)
if (fieldSchema.MinItems.HasValue && fieldSchema.MaxItems.HasValue)
{
CodeSnippetExpression minLen = new(fieldSchema.MinItems.Value.ToString());
CodeSnippetExpression maxLen = new(fieldSchema.MaxItems.Value.ToString());
CodeAttributeArgument[] attributeParams = new CodeAttributeArgument[] { new CodeAttributeArgument(minLen), new CodeAttributeArgument(maxLen) };
CodeAttributeDeclaration cad = new("System.ComponentModel.DataAnnotations.Length", attributeParams); //.NET 8 feature
memberField.CustomAttributes.Add(cad);
}
else if (fieldSchema.MinItems.HasValue)
{
CodeSnippetExpression len = new(fieldSchema.MinItems.Value.ToString());
CodeAttributeArgument[] attributeParams = new CodeAttributeArgument[] { new CodeAttributeArgument(len) };
CodeAttributeDeclaration cad = new("System.ComponentModel.DataAnnotations.MinLength", attributeParams);
memberField.CustomAttributes.Add(cad);
}

if (fieldSchema.MaxItems.HasValue)
else if (fieldSchema.MaxItems.HasValue)
{
CodeSnippetExpression len = new(fieldSchema.MaxItems.Value.ToString());
CodeAttributeArgument[] attributeParams = new CodeAttributeArgument[] { new CodeAttributeArgument(len) };
Expand Down Expand Up @@ -696,7 +710,7 @@ static KeyValuePair<string, OpenApiSchema>[] ExtractRequestBodiesOfApplicationJs
{
if (d.Value.Content.TryGetValue("application/json", out OpenApiMediaType mediaTypeObject))
{
if (mediaTypeObject.Schema.Reference == null && (mediaTypeObject.Schema.Properties==null || mediaTypeObject.Schema.Properties.Count == 0))
if (mediaTypeObject.Schema.Reference == null && (mediaTypeObject.Schema.Properties == null || mediaTypeObject.Schema.Properties.Count == 0))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,15 @@ static string GetCodeMemberFieldTextForAngularFormGroup(CodeMemberField codeMemb
case "System.ComponentModel.DataAnnotations.Required":
validatorList.Add("Validators.required");
break;
case "System.ComponentModel.DataAnnotations.MaxLength":
AddMaxLengthValidations(ca, validatorList);
case "System.ComponentModel.DataAnnotations.Length":
AddLengthValidations(ca, validatorList);
break;
case "System.ComponentModel.DataAnnotations.MinLength":
AddMinLengthValidations(ca, validatorList);
break;
case "System.ComponentModel.DataAnnotations.MaxLength":
AddMaxLengthValidations(ca, validatorList);
break;
case "System.ComponentModel.DataAnnotations.Range": // for minimum and maximum
AddNumberRangeValidations(ca, validatorList);
break;
Expand Down Expand Up @@ -310,6 +313,24 @@ static void AddNumberRangeValidations(CodeAttributeDeclaration ca, List<string>
}
}

static void AddLengthValidations(CodeAttributeDeclaration ca, List<string> validatorList)
{
Debug.Assert(ca.Arguments.Count == 2);
var arg0 = ca.Arguments[0];
var arg0VExpression = arg0.Value as CodeSnippetExpression;
var arg1 = ca.Arguments[1];
var arg1VExpression = arg1.Value as CodeSnippetExpression;
if (!arg0VExpression.Value.Contains("MinValue"))
{
validatorList.Add($"Validators.minLength({arg0VExpression.Value})");
}

if (!arg1VExpression.Value.Contains("MaxValue"))
{
validatorList.Add($"Validators.maxLength({arg1VExpression.Value})");
}
}

static void AddMinLengthValidations(CodeAttributeDeclaration ca, List<string> validatorList)
{
Debug.Assert(ca.Arguments.Count == 1);
Expand Down
51 changes: 17 additions & 34 deletions Tests/CsOpenApi3TestsResults/amadeus_com_2_2_0_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,7 @@ public class FlightOffer
/// Maximum items: 250
/// </summary>
[System.Runtime.Serialization.DataMember(Name="itineraries")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(250)]
[System.ComponentModel.DataAnnotations.Length(1, 250)]
public FlightOfferItineraries[] FlightOfferItineraries { get; set; }

/// <summary>
Expand Down Expand Up @@ -491,8 +490,7 @@ public class FlightOffer
/// Maximum items: 18
/// </summary>
[System.Runtime.Serialization.DataMember(Name="travelerPricings")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(18)]
[System.ComponentModel.DataAnnotations.Length(1, 18)]
public FlightOfferTravelerPricings[] FlightOfferTravelerPricings { get; set; }

/// <summary>
Expand All @@ -509,8 +507,7 @@ public class FlightOffer
/// Maximum items: 9
/// </summary>
[System.Runtime.Serialization.DataMember(Name="validatingAirlineCodes")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(9)]
[System.ComponentModel.DataAnnotations.Length(1, 9)]
public string[] ValidatingAirlineCodes { get; set; }
}

Expand All @@ -530,8 +527,7 @@ public class FlightOfferItineraries
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember(Name="segments")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(9)]
[System.ComponentModel.DataAnnotations.Length(1, 9)]
public Segment[] Segments { get; set; }
}

Expand Down Expand Up @@ -597,8 +593,7 @@ public class FlightOfferTravelerPricings
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember(Name="fareDetailsBySegment")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(18)]
[System.ComponentModel.DataAnnotations.Length(1, 18)]
public FlightOfferTravelerPricingsFareDetailsBySegment[] FlightOfferTravelerPricingsFareDetailsBySegment { get; set; }

/// <summary>
Expand Down Expand Up @@ -851,8 +846,7 @@ public class FlightSegment
/// Max length: 2
/// </summary>
[System.Runtime.Serialization.DataMember(Name="carrierCode")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(2)]
[System.ComponentModel.DataAnnotations.Length(1, 2)]
public string CarrierCode { get; set; }

/// <summary>
Expand All @@ -873,8 +867,7 @@ public class FlightSegment
/// Max length: 4
/// </summary>
[System.Runtime.Serialization.DataMember(Name="number")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(4)]
[System.ComponentModel.DataAnnotations.Length(1, 4)]
public string Number { get; set; }

/// <summary>
Expand Down Expand Up @@ -903,8 +896,7 @@ public class OperatingFlight
/// Max length: 2
/// </summary>
[System.Runtime.Serialization.DataMember(Name="carrierCode")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(2)]
[System.ComponentModel.DataAnnotations.Length(1, 2)]
public string CarrierCode { get; set; }
}

Expand Down Expand Up @@ -946,8 +938,7 @@ public class GetFlightOffersQuery
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember(Name="originDestinations")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(6)]
[System.ComponentModel.DataAnnotations.Length(1, 6)]
public OriginDestination[] OriginDestinations { get; set; }

[System.Runtime.Serialization.DataMember(Name="searchCriteria")]
Expand All @@ -970,8 +961,7 @@ public class GetFlightOffersQuery
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember(Name="travelers")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(18)]
[System.ComponentModel.DataAnnotations.Length(1, 18)]
public Traveler[] Travelers { get; set; }
}

Expand All @@ -985,8 +975,7 @@ public class OriginDestination
/// Maximum items: 2
/// </summary>
[System.Runtime.Serialization.DataMember(Name="alternativeDestinationsCodes")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(2)]
[System.ComponentModel.DataAnnotations.Length(1, 2)]
public string[] AlternativeDestinationsCodes { get; set; }

/// <summary>
Expand All @@ -995,8 +984,7 @@ public class OriginDestination
/// Maximum items: 2
/// </summary>
[System.Runtime.Serialization.DataMember(Name="alternativeOriginsCodes")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(2)]
[System.ComponentModel.DataAnnotations.Length(1, 2)]
public string[] AlternativeOriginsCodes { get; set; }

[System.Runtime.Serialization.DataMember(Name="arrivalDateTimeRange")]
Expand Down Expand Up @@ -1026,8 +1014,7 @@ public class OriginDestination
/// Maximum items: 3
/// </summary>
[System.Runtime.Serialization.DataMember(Name="excludedConnectionPoints")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(3)]
[System.ComponentModel.DataAnnotations.Length(1, 3)]
public string[] ExcludedConnectionPoints { get; set; }

[System.Runtime.Serialization.DataMember(Name="id")]
Expand All @@ -1039,8 +1026,7 @@ public class OriginDestination
/// Maximum items: 2
/// </summary>
[System.Runtime.Serialization.DataMember(Name="includedConnectionPoints")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(2)]
[System.ComponentModel.DataAnnotations.Length(1, 2)]
public string[] IncludedConnectionPoints { get; set; }

/// <summary>
Expand Down Expand Up @@ -1143,8 +1129,7 @@ public class SearchCriteriaFlightFilters
/// Maximum items: 6
/// </summary>
[System.Runtime.Serialization.DataMember(Name="cabinRestrictions")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(6)]
[System.ComponentModel.DataAnnotations.Length(1, 6)]
public SearchCriteriaFlightFiltersCabinRestrictions[] SearchCriteriaFlightFiltersCabinRestrictions { get; set; }

/// <summary>
Expand Down Expand Up @@ -1227,8 +1212,7 @@ public class SearchCriteriaFlightFiltersCarrierRestrictions
/// Maximum items: 99
/// </summary>
[System.Runtime.Serialization.DataMember(Name="excludedCarrierCodes")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(99)]
[System.ComponentModel.DataAnnotations.Length(1, 99)]
public string[] ExcludedCarrierCodes { get; set; }

/// <summary>
Expand All @@ -1237,8 +1221,7 @@ public class SearchCriteriaFlightFiltersCarrierRestrictions
/// Maximum items: 99
/// </summary>
[System.Runtime.Serialization.DataMember(Name="includedCarrierCodes")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(99)]
[System.ComponentModel.DataAnnotations.Length(1, 99)]
public string[] IncludedCarrierCodes { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3487,8 +3487,7 @@ public class ApplyArchiveRulePutBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember(Name="ruleName")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(255)]
[System.ComponentModel.DataAnnotations.Length(1, 255)]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute(@"[A-Za-z][A-Za-z0-9_.-]*")]
public string RuleName { get; set; }

Expand Down Expand Up @@ -3539,8 +3538,7 @@ public class CreateAnalyzerPutBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember(Name="analyzerName")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(255)]
[System.ComponentModel.DataAnnotations.Length(1, 255)]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute(@"[A-Za-z][A-Za-z0-9_.-]*")]
public string AnalyzerName { get; set; }

Expand Down Expand Up @@ -3583,8 +3581,7 @@ public class CreateArchiveRulePutBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember(Name="ruleName")]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(255)]
[System.ComponentModel.DataAnnotations.Length(1, 255)]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute(@"[A-Za-z][A-Za-z0-9_.-]*")]
public string RuleName { get; set; }

Expand Down
24 changes: 8 additions & 16 deletions Tests/CsOpenApi3TestsResults/amazonaws_com_account_2021_02_01_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,7 @@ public class DisableRegionPostBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(50)]
[System.ComponentModel.DataAnnotations.Length(1, 50)]
public string RegionName { get; set; }
}

Expand All @@ -719,8 +718,7 @@ public class EnableRegionPostBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(50)]
[System.ComponentModel.DataAnnotations.Length(1, 50)]
public string RegionName { get; set; }
}

Expand Down Expand Up @@ -775,8 +773,7 @@ public class GetRegionOptStatusPostBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(50)]
[System.ComponentModel.DataAnnotations.Length(1, 50)]
public string RegionName { get; set; }
}

Expand Down Expand Up @@ -806,8 +803,7 @@ public class ListRegionsPostBody
/// Max length: 1000
/// </summary>
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(0)]
[System.ComponentModel.DataAnnotations.MaxLength(1000)]
[System.ComponentModel.DataAnnotations.Length(0, 1000)]
public string NextToken { get; set; }

/// <summary>
Expand Down Expand Up @@ -845,8 +841,7 @@ public class PutAlternateContactPostBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(254)]
[System.ComponentModel.DataAnnotations.Length(1, 254)]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute(@"^[\s]*[\w+=.#|!&-]+@[\w.-]+\.[\w]+[\s]*$")]
public string EmailAddress { get; set; }

Expand All @@ -858,8 +853,7 @@ public class PutAlternateContactPostBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(64)]
[System.ComponentModel.DataAnnotations.Length(1, 64)]
public string Name { get; set; }

/// <summary>
Expand All @@ -871,8 +865,7 @@ public class PutAlternateContactPostBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(25)]
[System.ComponentModel.DataAnnotations.Length(1, 25)]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute(@"^[\s0-9()+-]+$")]
public string PhoneNumber { get; set; }

Expand All @@ -884,8 +877,7 @@ public class PutAlternateContactPostBody
/// </summary>
[System.ComponentModel.DataAnnotations.Required()]
[System.Runtime.Serialization.DataMember()]
[System.ComponentModel.DataAnnotations.MinLength(1)]
[System.ComponentModel.DataAnnotations.MaxLength(50)]
[System.ComponentModel.DataAnnotations.Length(1, 50)]
public string Title { get; set; }
}

Expand Down
Loading

0 comments on commit 9e2d974

Please sign in to comment.