Skip to content

Commit 90fd171

Browse files
committed
Add test for length attribute on collection
1 parent 6c78d76 commit 90fd171

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

test/OpenApiNSwagEndToEndTests/ModelStateValidation/ModelStateValidationTests.cs

+34-1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,39 @@ public async Task Cannot_use_invalid_url()
285285
errorObject.Source.Pointer.Should().Be("/data/attributes/backgroundPicture");
286286
}
287287

288+
[Theory]
289+
[InlineData(0)]
290+
[InlineData(11)]
291+
public async Task Cannot_exceed_collection_length_constraint(int length)
292+
{
293+
// Arrange
294+
using HttpClient httpClient = _testContext.Factory.CreateDefaultClient(_logHttpMessageHandler);
295+
ModelStateValidationClient apiClient = new(httpClient);
296+
297+
// Act
298+
Func<Task<SocialMediaAccountPrimaryResponseDocument>> action = () => apiClient.PostSocialMediaAccountAsync(new SocialMediaAccountPostRequestDocument
299+
{
300+
Data = new SocialMediaAccountDataInPostRequest
301+
{
302+
Attributes = new SocialMediaAccountAttributesInPostRequest
303+
{
304+
LastName = "",
305+
Tags = Enumerable.Repeat("", length).ToArray(),
306+
}
307+
}
308+
});
309+
310+
// Assert
311+
ErrorResponseDocument document = (await action.Should().ThrowExactlyAsync<ApiException<ErrorResponseDocument>>()).Which.Result;
312+
document.Errors.ShouldHaveCount(1);
313+
314+
ErrorObject errorObject = document.Errors.First();
315+
errorObject.Title.Should().Be("Input validation failed.");
316+
errorObject.Detail.Should().Be("The field Tags must be a string or collection type with a minimum length of '1' and maximum length of '10'.");
317+
errorObject.Source.ShouldNotBeNull();
318+
errorObject.Source.Pointer.Should().Be("/data/attributes/tags");
319+
}
320+
288321
[Fact]
289322
public async Task Cannot_use_non_allowed_value()
290323
{
@@ -300,7 +333,7 @@ public async Task Cannot_use_non_allowed_value()
300333
Attributes = new SocialMediaAccountAttributesInPostRequest
301334
{
302335
LastName = "",
303-
CountryCode = "US"
336+
CountryCode = "XX"
304337
}
305338
}
306339
});

test/OpenApiTests/ModelStateValidation/ModelStateValidationFakers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public sealed class ModelStateValidationFakers
2727
.RuleFor(socialMediaAccount => socialMediaAccount.Age, faker => faker.Random.Double(0.1, 122.9))
2828
.RuleFor(socialMediaAccount => socialMediaAccount.ProfilePicture, faker => new Uri(faker.Image.LoremFlickrUrl()))
2929
.RuleFor(socialMediaAccount => socialMediaAccount.BackgroundPicture, faker => faker.Image.LoremFlickrUrl())
30-
.RuleFor(socialMediaAccount => socialMediaAccount.Tags, faker => faker.Make(faker.Random.Number(0, 10), () => faker.Random.String2(2, 10)))
30+
.RuleFor(socialMediaAccount => socialMediaAccount.Tags, faker => faker.Make(faker.Random.Number(1, 10), () => faker.Random.String2(2, 10)))
3131
.RuleFor(socialMediaAccount => socialMediaAccount.CountryCode, faker => faker.Random.ListItem(["NL", "FR"]))
3232
.RuleFor(socialMediaAccount => socialMediaAccount.Planet, faker => faker.Random.String2(2, 8))
3333
.RuleFor(socialMediaAccount => socialMediaAccount.NextRevalidation, faker => TimeSpan.FromMinutes(faker.Random.Number(1, 5)))

test/OpenApiTests/ModelStateValidation/SocialMediaAccount.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public sealed class SocialMediaAccount : Identifiable<Guid>
6363

6464
[Attr]
6565
#if NET8_0_OR_GREATER
66-
[Length(0, 10)]
66+
[Length(1, 10)]
6767
#endif
6868
public List<string>? Tags { get; set; }
6969

0 commit comments

Comments
 (0)