Skip to content

Commit 600afa4

Browse files
authored
Pattern validation for model properties, fixes #61
Pattern validation for model properties, fixes #61
2 parents 30931ee + 414906d commit 600afa4

File tree

7 files changed

+97
-6
lines changed

7 files changed

+97
-6
lines changed

src/Atc.Rest.ApiGenerator/Extensions/PropertyDeclarationSyntaxExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ OpenApiDataTypeConstants.Integer when schema.HasFormatType() && schema.IsFormatT
196196
return propertyDeclaration;
197197
}
198198

199+
public static PropertyDeclarationSyntax AddValidationAttributeForPatternIfRequired(
200+
this PropertyDeclarationSyntax propertyDeclaration,
201+
OpenApiSchema schema)
202+
{
203+
if (schema == null)
204+
{
205+
throw new ArgumentNullException(nameof(schema));
206+
}
207+
208+
if (schema.Type == OpenApiDataTypeConstants.String && schema.Pattern is not null)
209+
{
210+
propertyDeclaration = propertyDeclaration.AddValidationAttribute(new RegularExpressionAttribute(schema.Pattern));
211+
}
212+
213+
return propertyDeclaration;
214+
}
215+
199216
private static PropertyDeclarationSyntax RangeAttributeInt(
200217
PropertyDeclarationSyntax propertyDeclaration,
201218
OpenApiSchema schema)

src/Atc.Rest.ApiGenerator/SyntaxFactories/SyntaxPropertyDeclarationFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public static PropertyDeclarationSyntax CreateAuto(
114114

115115
propertyDeclaration = propertyDeclaration.AddValidationAttributeForMinMaxIfRequired(schema.Value);
116116

117+
propertyDeclaration = propertyDeclaration.AddValidationAttributeForPatternIfRequired(schema.Value);
118+
117119
return propertyDeclaration;
118120
}
119121

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/SyntaxGeneratorContractModelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions api
2121
{
2222
// Verify spec file supported for unit test
2323
Assert.Single(apiProject.Document.Components.Schemas);
24-
var schema = apiProject.Document.Components.Schemas.First().Value;
24+
var schema = apiProject.Document.Components.Schemas.First();
2525

2626
// Construct SUT
27-
return new SyntaxGeneratorContractModel(apiProject, string.Empty, schema, FocusOnSegment);
27+
return new SyntaxGeneratorContractModel(apiProject, schema.Key, schema.Value, FocusOnSegment);
2828
}
2929

3030
[Theory(DisplayName = "Api Contract Model")]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.0
2+
components:
3+
schemas:
4+
TestComponent:
5+
title: TestComponent
6+
description: 'Test component description'
7+
type: object
8+
properties:
9+
noConstraint:
10+
type: string
11+
minLengthConstraint:
12+
type: string
13+
minLength: 42
14+
maxLengthConstraint:
15+
type: string
16+
maxLength: 42
17+
bothLengthsConstraint:
18+
type: string
19+
minLength: 3
20+
maxLength: 42
21+
patternConstraint:
22+
type: string
23+
pattern: '^[A-Z]$'
24+
allConstraint:
25+
type: string
26+
minLength: 3
27+
maxLength: 42
28+
pattern: '^[A-Z]$'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.CodeDom.Compiler;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
//------------------------------------------------------------------------------
5+
// This code was auto-generated by ApiGenerator x.x.x.x.
6+
//
7+
// Changes to this file may cause incorrect behavior and will be lost if
8+
// the code is regenerated.
9+
//------------------------------------------------------------------------------
10+
namespace TestProject.AtcTest.Contracts.Test
11+
{
12+
/// <summary>
13+
/// Test component description.
14+
/// </summary>
15+
[GeneratedCode("ApiGenerator", "x.x.x.x")]
16+
public class TestComponent
17+
{
18+
public string NoConstraint { get; set; }
19+
20+
[MinLength(42)]
21+
public string MinLengthConstraint { get; set; }
22+
23+
[StringLength(42)]
24+
public string MaxLengthConstraint { get; set; }
25+
26+
[MinLength(3)]
27+
[MaxLength(42)]
28+
public string BothLengthsConstraint { get; set; }
29+
30+
[RegularExpression("^[A-Z]$")]
31+
public string PatternConstraint { get; set; }
32+
33+
[MinLength(3)]
34+
[MaxLength(42)]
35+
[RegularExpression("^[A-Z]$")]
36+
public string AllConstraint { get; set; }
37+
38+
/// <summary>
39+
/// Converts to string.
40+
/// </summary>
41+
public override string ToString()
42+
{
43+
return $"{nameof(NoConstraint)}: {NoConstraint}, {nameof(MinLengthConstraint)}: {MinLengthConstraint}, {nameof(MaxLengthConstraint)}: {MaxLengthConstraint}, {nameof(BothLengthsConstraint)}: {BothLengthsConstraint}, {nameof(PatternConstraint)}: {PatternConstraint}, {nameof(AllConstraint)}: {AllConstraint}";
44+
}
45+
}
46+
}

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/ContractModel/requiredArrayComponent.yaml.verified.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ namespace TestProject.AtcTest.Contracts.Test
1414
/// CreateItemRequest.
1515
/// </summary>
1616
[GeneratedCode("ApiGenerator", "x.x.x.x")]
17-
public class
18-
17+
public class RequiredArrayComponent
1918
{
2019
[Required]
2120
public List<string> MyItems { get; set; }

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/XUnitTestData/ContractModel/stringComponent.yaml.verified.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ namespace TestProject.AtcTest.Contracts.Test
1414
/// A string component.
1515
/// </summary>
1616
[GeneratedCode("ApiGenerator", "x.x.x.x")]
17-
public class
18-
17+
public class StringComponent
1918
{
2019
/// <summary>
2120
/// normal string.

0 commit comments

Comments
 (0)