Skip to content

Commit f7ba49d

Browse files
committed
Address PR comments
1 parent 8f0e849 commit f7ba49d

File tree

8 files changed

+270
-152
lines changed

8 files changed

+270
-152
lines changed

test/OpenApiKiotaEndToEndTests/ModelStateValidation/ModelStateValidationTests.cs

+76-68
Large diffs are not rendered by default.

test/OpenApiKiotaEndToEndTests/ModelStateValidation/UtcDateTimeJsonConverter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
1111
return dateTimeOffset.UtcDateTime;
1212
}
1313

14-
public override void Write(Utf8JsonWriter writer, DateTime val, JsonSerializerOptions options)
14+
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
1515
{
16-
writer.WriteStringValue(val.ToUniversalTime().ToString("O"));
16+
writer.WriteStringValue(value.ToUniversalTime().ToString("O"));
1717
}
1818
}

test/OpenApiNSwagEndToEndTests/ModelStateValidation/ModelStateValidationTests.cs

+154-32
Large diffs are not rendered by default.

test/OpenApiNSwagEndToEndTests/ModelStateValidation/UtcDateTimeJsonConverter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
1111
return dateTimeOffset.UtcDateTime;
1212
}
1313

14-
public override void Write(Utf8JsonWriter writer, DateTime val, JsonSerializerOptions options)
14+
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
1515
{
16-
writer.WriteStringValue(val.ToUniversalTime().ToString("O"));
16+
writer.WriteStringValue(value.ToUniversalTime().ToString("O"));
1717
}
1818
}

test/OpenApiTests/ModelStateValidation/GeneratedSwagger/net8.0/swagger.g.json

+6
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@
431431
"nullable": true
432432
},
433433
"password": {
434+
"maxLength": 100,
435+
"minLength": 5,
434436
"type": "string",
435437
"nullable": true
436438
},
@@ -534,6 +536,8 @@
534536
"nullable": true
535537
},
536538
"password": {
539+
"maxLength": 100,
540+
"minLength": 5,
537541
"type": "string",
538542
"nullable": true
539543
},
@@ -634,6 +638,8 @@
634638
"nullable": true
635639
},
636640
"password": {
641+
"maxLength": 100,
642+
"minLength": 5,
637643
"type": "string",
638644
"nullable": true
639645
},

test/OpenApiTests/ModelStateValidation/ModelStateValidationFakers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class ModelStateValidationFakers
1818
.RuleFor(socialMediaAccount => socialMediaAccount.UserName, faker => faker.Random.String2(3, 18))
1919
.RuleFor(socialMediaAccount => socialMediaAccount.CreditCard, faker => faker.Finance.CreditCardNumber())
2020
.RuleFor(socialMediaAccount => socialMediaAccount.Email, faker => faker.Person.Email)
21-
.RuleFor(socialMediaAccount => socialMediaAccount.Password, faker => Convert.ToBase64String(faker.Random.Bytes(20)))
21+
.RuleFor(socialMediaAccount => socialMediaAccount.Password, faker => Convert.ToBase64String(faker.Random.Bytes(faker.Random.Number(4, 75))))
2222
.RuleFor(socialMediaAccount => socialMediaAccount.Phone, faker => faker.Person.Phone)
2323
.RuleFor(socialMediaAccount => socialMediaAccount.Age, faker => faker.Random.Double(0.1, 122.9))
2424
.RuleFor(socialMediaAccount => socialMediaAccount.ProfilePicture, faker => new Uri(faker.Image.LoremFlickrUrl()))

test/OpenApiTests/ModelStateValidation/ModelStateValidationTests.cs

+23-47
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenApiTests.ModelStateValidation;
88
public sealed class ModelStateValidationTests : IClassFixture<OpenApiTestContext<OpenApiStartup<ModelStateValidationDbContext>, ModelStateValidationDbContext>>
99
{
1010
// ReSharper disable once UseCollectionExpression (https://youtrack.jetbrains.com/issue/RSRP-497450)
11-
public static readonly TheoryData<string> ModelNames = new()
11+
public static readonly TheoryData<string> SchemaNames = new()
1212
{
1313
"socialMediaAccountAttributesInPostRequest",
1414
"socialMediaAccountAttributesInPatchRequest",
@@ -34,7 +34,7 @@ public ModelStateValidationTests(OpenApiTestContext<OpenApiStartup<ModelStateVal
3434
}
3535

3636
[Theory]
37-
[MemberData(nameof(ModelNames))]
37+
[MemberData(nameof(SchemaNames))]
3838
public async Task Guid_type_produces_expected_schema(string modelName)
3939
{
4040
// Act
@@ -49,7 +49,7 @@ public async Task Guid_type_produces_expected_schema(string modelName)
4949
}
5050

5151
[Theory]
52-
[MemberData(nameof(ModelNames))]
52+
[MemberData(nameof(SchemaNames))]
5353
public async Task Length_annotation_on_resource_string_property_produces_expected_schema(string modelName)
5454
{
5555
// Act
@@ -67,7 +67,7 @@ public async Task Length_annotation_on_resource_string_property_produces_expecte
6767
}
6868

6969
[Theory]
70-
[MemberData(nameof(ModelNames))]
70+
[MemberData(nameof(SchemaNames))]
7171
public async Task Required_annotation_with_AllowEmptyStrings_on_resource_property_produces_expected_schema(string modelName)
7272
{
7373
// Act
@@ -82,7 +82,7 @@ public async Task Required_annotation_with_AllowEmptyStrings_on_resource_propert
8282
}
8383

8484
[Theory]
85-
[MemberData(nameof(ModelNames))]
85+
[MemberData(nameof(SchemaNames))]
8686
public async Task StringLength_annotation_on_resource_property_produces_expected_schema(string modelName)
8787
{
8888
// Act
@@ -98,7 +98,7 @@ public async Task StringLength_annotation_on_resource_property_produces_expected
9898
}
9999

100100
[Theory]
101-
[MemberData(nameof(ModelNames))]
101+
[MemberData(nameof(SchemaNames))]
102102
public async Task RegularExpression_annotation_on_resource_property_produces_expected_schema(string modelName)
103103
{
104104
// Act
@@ -113,7 +113,7 @@ public async Task RegularExpression_annotation_on_resource_property_produces_exp
113113
}
114114

115115
[Theory]
116-
[MemberData(nameof(ModelNames))]
116+
[MemberData(nameof(SchemaNames))]
117117
public async Task CreditCard_annotation_on_resource_property_produces_expected_schema(string modelName)
118118
{
119119
// Act
@@ -128,7 +128,7 @@ public async Task CreditCard_annotation_on_resource_property_produces_expected_s
128128
}
129129

130130
[Theory]
131-
[MemberData(nameof(ModelNames))]
131+
[MemberData(nameof(SchemaNames))]
132132
public async Task Email_annotation_on_resource_property_produces_expected_schema(string modelName)
133133
{
134134
// Act
@@ -143,21 +143,25 @@ public async Task Email_annotation_on_resource_property_produces_expected_schema
143143
}
144144

145145
[Theory]
146-
[MemberData(nameof(ModelNames))]
147-
public async Task Base64String_annotation_on_resource_property_produces_expected_schema(string modelName)
146+
[MemberData(nameof(SchemaNames))]
147+
public async Task Min_max_length_annotation_on_resource_list_property_produces_expected_schema(string modelName)
148148
{
149149
// Act
150150
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
151151

152152
// Assert
153153
document.Should().ContainPath($"components.schemas.{modelName}.properties.password").With(passwordElement =>
154154
{
155+
#if !NET6_0
156+
passwordElement.Should().HaveProperty("maxLength", 100);
157+
passwordElement.Should().HaveProperty("minLength", 5);
158+
#endif
155159
passwordElement.Should().HaveProperty("type", "string");
156160
});
157161
}
158162

159163
[Theory]
160-
[MemberData(nameof(ModelNames))]
164+
[MemberData(nameof(SchemaNames))]
161165
public async Task Phone_annotation_on_resource_property_produces_expected_schema(string modelName)
162166
{
163167
// Act
@@ -172,7 +176,7 @@ public async Task Phone_annotation_on_resource_property_produces_expected_schema
172176
}
173177

174178
[Theory]
175-
[MemberData(nameof(ModelNames))]
179+
[MemberData(nameof(SchemaNames))]
176180
public async Task Range_annotation_on_resource_property_produces_expected_schema(string modelName)
177181
{
178182
// Act
@@ -191,7 +195,7 @@ public async Task Range_annotation_on_resource_property_produces_expected_schema
191195
}
192196

193197
[Theory]
194-
[MemberData(nameof(ModelNames))]
198+
[MemberData(nameof(SchemaNames))]
195199
public async Task Url_annotation_on_resource_property_produces_expected_schema(string modelName)
196200
{
197201
// Act
@@ -206,7 +210,7 @@ public async Task Url_annotation_on_resource_property_produces_expected_schema(s
206210
}
207211

208212
[Theory]
209-
[MemberData(nameof(ModelNames))]
213+
[MemberData(nameof(SchemaNames))]
210214
public async Task Uri_type_produces_expected_schema(string modelName)
211215
{
212216
// Act
@@ -221,7 +225,7 @@ public async Task Uri_type_produces_expected_schema(string modelName)
221225
}
222226

223227
[Theory]
224-
[MemberData(nameof(ModelNames))]
228+
[MemberData(nameof(SchemaNames))]
225229
public async Task Length_annotation_on_resource_list_property_produces_expected_schema(string modelName)
226230
{
227231
// Act
@@ -244,35 +248,7 @@ public async Task Length_annotation_on_resource_list_property_produces_expected_
244248
}
245249

246250
[Theory]
247-
[MemberData(nameof(ModelNames))]
248-
public async Task AllowedValues_annotation_on_resource_list_property_produces_expected_schema(string modelName)
249-
{
250-
// Act
251-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
252-
253-
// Assert
254-
document.Should().ContainPath($"components.schemas.{modelName}.properties.countryCode").With(countryCodeElement =>
255-
{
256-
countryCodeElement.Should().HaveProperty("type", "string");
257-
});
258-
}
259-
260-
[Theory]
261-
[MemberData(nameof(ModelNames))]
262-
public async Task DeniedValues_annotation_on_resource_property_produces_expected_schema(string modelName)
263-
{
264-
// Act
265-
JsonElement document = await _testContext.GetSwaggerDocumentAsync();
266-
267-
// Assert
268-
document.Should().ContainPath($"components.schemas.{modelName}.properties.planet").With(planetElement =>
269-
{
270-
planetElement.Should().HaveProperty("type", "string");
271-
});
272-
}
273-
274-
[Theory]
275-
[MemberData(nameof(ModelNames))]
251+
[MemberData(nameof(SchemaNames))]
276252
public async Task TimeSpan_range_annotation_on_resource_property_produces_expected_schema(string modelName)
277253
{
278254
// Act
@@ -287,7 +263,7 @@ public async Task TimeSpan_range_annotation_on_resource_property_produces_expect
287263
}
288264

289265
[Theory]
290-
[MemberData(nameof(ModelNames))]
266+
[MemberData(nameof(SchemaNames))]
291267
public async Task DateTime_type_produces_expected_schema(string modelName)
292268
{
293269
// Act
@@ -302,7 +278,7 @@ public async Task DateTime_type_produces_expected_schema(string modelName)
302278
}
303279

304280
[Theory]
305-
[MemberData(nameof(ModelNames))]
281+
[MemberData(nameof(SchemaNames))]
306282
public async Task DateOnly_type_produces_expected_schema(string modelName)
307283
{
308284
// Act
@@ -317,7 +293,7 @@ public async Task DateOnly_type_produces_expected_schema(string modelName)
317293
}
318294

319295
[Theory]
320-
[MemberData(nameof(ModelNames))]
296+
[MemberData(nameof(SchemaNames))]
321297
public async Task TimeOnly_type_produces_expected_schema(string modelName)
322298
{
323299
// Act

test/OpenApiTests/ModelStateValidation/SocialMediaAccount.cs

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public sealed class SocialMediaAccount : Identifiable<Guid>
3939
[Attr]
4040
#if !NET6_0
4141
[Base64String]
42+
[MinLength(5)]
43+
[MaxLength(100)]
4244
#endif
4345
public string? Password { get; set; }
4446

@@ -47,7 +49,11 @@ public sealed class SocialMediaAccount : Identifiable<Guid>
4749
public string? Phone { get; set; }
4850

4951
[Attr]
52+
#if NET6_0
5053
[Range(0.1, 122.9)]
54+
#else
55+
[Range(0.1, 122.9, MinimumIsExclusive = true, MaximumIsExclusive = true)]
56+
#endif
5157
public double? Age { get; set; }
5258

5359
[Attr]

0 commit comments

Comments
 (0)