-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathOpenApiSchemaExtensionsTests.cs
More file actions
134 lines (113 loc) · 5.69 KB
/
Copy pathOpenApiSchemaExtensionsTests.cs
File metadata and controls
134 lines (113 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Microsoft.OpenApi.Models;
namespace Swashbuckle.AspNetCore.SwaggerGen.Test;
public static class OpenApiSchemaExtensionsTests
{
public static TheoryData<string, bool, RangeAttribute, string, string> TestCases()
{
bool[] isExclusive = [false, true];
string[] invariantOrEnglishCultures =
[
string.Empty,
"en",
"en-AU",
"en-GB",
"en-US",
];
string[] commaForDecimalCultures =
[
"de-DE",
"fr-FR",
"sv-SE",
];
Type[] fractionNumberTypes =
[
typeof(float),
typeof(double),
typeof(decimal),
];
var testCases = new TheoryData<string, bool, RangeAttribute, string, string>();
foreach (var culture in invariantOrEnglishCultures)
{
foreach (var exclusive in isExclusive)
{
testCases.Add(culture, exclusive, new(1, 1234) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1d, 1234d) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1.23, 4.56) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1.23", "4.56");
foreach (var type in fractionNumberTypes)
{
testCases.Add(culture, exclusive, new(type, "1.23", "4.56") { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1.23", "4.56");
testCases.Add(culture, exclusive, new(type, "1.23", "4.56") { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive, ParseLimitsInInvariantCulture = true }, "1.23", "4.56");
}
}
}
foreach (var culture in commaForDecimalCultures)
{
foreach (var exclusive in isExclusive)
{
testCases.Add(culture, exclusive, new(1, 1234) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1, 1234) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1d, 1234d) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1", "1234");
testCases.Add(culture, exclusive, new(1.23, 4.56) { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1.23", "4.56");
foreach (var type in fractionNumberTypes)
{
testCases.Add(culture, exclusive, new(type, "1,23", "4,56") { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "1.23", "4.56");
testCases.Add(culture, exclusive, new(type, "1.23", "4.56") { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive, ParseLimitsInInvariantCulture = true }, "1.23", "4.56");
}
}
}
// Numbers using numeric format, such as with thousands separators
testCases.Add("en-GB", false, new(typeof(float), "-12,445.7", "12,445.7"), "-12445.7", "12445.7");
testCases.Add("fr-FR", false, new(typeof(float), "-12 445,7", "12 445,7"), "-12445.7", "12445.7");
testCases.Add("sv-SE", false, new(typeof(float), "-12 445,7", "12 445,7"), "-12445.7", "12445.7");
// Decimal value that would lose precision if parsed as a float or double
foreach (var exclusive in isExclusive)
{
testCases.Add("en-US", exclusive, new(typeof(decimal), "12345678901234567890.123456789", "12345678901234567890.123456789") { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive }, "12345678901234567890.123456789", "12345678901234567890.123456789");
testCases.Add("en-US", exclusive, new(typeof(decimal), "12345678901234567890.123456789", "12345678901234567890.123456789") { MaximumIsExclusive = exclusive, MinimumIsExclusive = exclusive, ParseLimitsInInvariantCulture = true }, "12345678901234567890.123456789", "12345678901234567890.123456789");
}
return testCases;
}
[Theory]
[MemberData(nameof(TestCases))]
public static void ApplyValidationAttributes_Handles_RangeAttribute_Correctly(
string cultureName,
bool isExclusive,
RangeAttribute rangeAttribute,
string expectedMinimum,
string expectedMaximum)
{
// Arrange
var minimum = decimal.Parse(expectedMinimum, CultureInfo.InvariantCulture);
var maximum = decimal.Parse(expectedMaximum, CultureInfo.InvariantCulture);
var schema = new OpenApiSchema();
// Act
using (CultureSwitcher.UseCulture(cultureName))
{
schema.ApplyValidationAttributes([rangeAttribute]);
}
// Assert
Assert.Equal(isExclusive ? true : null, schema.ExclusiveMinimum);
Assert.Equal(isExclusive ? true : null, schema.ExclusiveMaximum);
Assert.Equal(minimum, schema.Minimum);
Assert.Equal(maximum, schema.Maximum);
}
private sealed class CultureSwitcher : IDisposable
{
private readonly CultureInfo _previous;
private CultureSwitcher(string name)
{
_previous = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(name);
}
public static CultureSwitcher UseCulture(string name) => new(name);
public void Dispose()
{
if (_previous is not null)
{
CultureInfo.CurrentCulture = _previous;
}
}
}
}