Skip to content

Commit a8fceaa

Browse files
Add dedicated localization tests
Move the fixture for OpenAPI localization to its own fixture to not pollute the sample app.
1 parent ad5130e commit a8fceaa

File tree

18 files changed

+4455
-28
lines changed

18 files changed

+4455
-28
lines changed

src/OpenApi/sample/Program.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Globalization;
54
using System.Text.Json.Serialization;
65
using Sample.Transformers;
76

@@ -86,32 +85,6 @@
8685

8786
var app = builder.Build();
8887

89-
// Run requests with a culture that uses commas to format decimals to
90-
// verify the invariant culture is used to generate the OpenAPI document.
91-
app.Use((next) =>
92-
{
93-
return async context =>
94-
{
95-
var originalCulture = CultureInfo.CurrentCulture;
96-
var originalUICulture = CultureInfo.CurrentUICulture;
97-
98-
var newCulture = new CultureInfo("fr-FR");
99-
100-
try
101-
{
102-
CultureInfo.CurrentCulture = newCulture;
103-
CultureInfo.CurrentUICulture = newCulture;
104-
105-
await next(context);
106-
}
107-
finally
108-
{
109-
CultureInfo.CurrentCulture = originalCulture;
110-
CultureInfo.CurrentUICulture = originalUICulture;
111-
}
112-
};
113-
});
114-
11588
app.MapOpenApi();
11689
app.MapOpenApi("/openapi/{documentName}.yaml");
11790
if (app.Environment.IsDevelopment())
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
// Runs requests with a culture that uses commas to format decimals to
9+
// verify the invariant culture is used to generate the OpenAPI document.
10+
11+
public sealed class LocalizedSampleAppFixture : SampleAppFixture
12+
{
13+
protected override void ConfigureWebHost(IWebHostBuilder builder)
14+
{
15+
base.ConfigureWebHost(builder);
16+
17+
builder.ConfigureServices(services =>
18+
{
19+
services.AddTransient<IStartupFilter, AddLocalizationMiddlewareFilter>();
20+
services.AddRequestLocalization((options) =>
21+
{
22+
options.DefaultRequestCulture = new("fr-FR");
23+
options.SupportedCultures = [new("fr-FR")];
24+
options.SupportedUICultures = [new("fr-FR")];
25+
});
26+
});
27+
}
28+
29+
private sealed class AddLocalizationMiddlewareFilter : IStartupFilter
30+
{
31+
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
32+
{
33+
return (app) =>
34+
{
35+
app.UseRequestLocalization();
36+
next(app);
37+
};
38+
}
39+
}
40+
}

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/OpenApiDocumentIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.OpenApi;
66

77
[UsesVerify]
8-
public sealed class OpenApiDocumentIntegrationTests(SampleAppFixture fixture) : IClassFixture<SampleAppFixture>
8+
public class OpenApiDocumentIntegrationTests(SampleAppFixture fixture) : IClassFixture<SampleAppFixture>
99
{
1010
public static TheoryData<string, OpenApiSpecVersion> OpenApiDocuments()
1111
{
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
[UsesVerify]
5+
public sealed class OpenApiDocumentLocalizationTests(LocalizedSampleAppFixture fixture)
6+
: OpenApiDocumentIntegrationTests(fixture), IClassFixture<LocalizedSampleAppFixture>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Sample | controllers-openapi3_0",
5+
"version": "1.0.0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost/"
10+
}
11+
],
12+
"paths": {
13+
"/getbyidandname/{id}/{name}": {
14+
"get": {
15+
"tags": [
16+
"Test"
17+
],
18+
"parameters": [
19+
{
20+
"name": "Id",
21+
"in": "path",
22+
"required": true,
23+
"schema": {
24+
"type": "integer",
25+
"format": "int32"
26+
}
27+
},
28+
{
29+
"name": "Name",
30+
"in": "path",
31+
"required": true,
32+
"schema": {
33+
"minLength": 5,
34+
"type": "string"
35+
}
36+
}
37+
],
38+
"responses": {
39+
"200": {
40+
"description": "OK",
41+
"content": {
42+
"text/plain": {
43+
"schema": {
44+
"type": "string"
45+
}
46+
},
47+
"application/json": {
48+
"schema": {
49+
"type": "string"
50+
}
51+
},
52+
"text/json": {
53+
"schema": {
54+
"type": "string"
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}
61+
},
62+
"/gettypedresult": {
63+
"get": {
64+
"tags": [
65+
"Test"
66+
],
67+
"responses": {
68+
"200": {
69+
"description": "OK",
70+
"content": {
71+
"application/json": {
72+
"schema": {
73+
"$ref": "#/components/schemas/MvcTodo"
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
},
81+
"/forms": {
82+
"post": {
83+
"tags": [
84+
"Test"
85+
],
86+
"requestBody": {
87+
"content": {
88+
"application/x-www-form-urlencoded": {
89+
"schema": {
90+
"type": "object",
91+
"properties": {
92+
"Title": {
93+
"type": "string"
94+
},
95+
"Description": {
96+
"type": "string"
97+
},
98+
"IsCompleted": {
99+
"type": "boolean"
100+
}
101+
}
102+
}
103+
}
104+
},
105+
"required": true
106+
},
107+
"responses": {
108+
"200": {
109+
"description": "OK"
110+
}
111+
}
112+
}
113+
},
114+
"/getcultureinvariant": {
115+
"get": {
116+
"tags": [
117+
"Test"
118+
],
119+
"responses": {
120+
"200": {
121+
"description": "OK",
122+
"content": {
123+
"application/json": {
124+
"schema": {
125+
"$ref": "#/components/schemas/CurrentWeather"
126+
}
127+
}
128+
}
129+
}
130+
}
131+
}
132+
}
133+
},
134+
"components": {
135+
"schemas": {
136+
"CurrentWeather": {
137+
"type": "object",
138+
"properties": {
139+
"temperature": {
140+
"maximum": 100.5,
141+
"minimum": -100.5,
142+
"type": "number",
143+
"format": "float",
144+
"default": 0.1
145+
}
146+
}
147+
},
148+
"MvcTodo": {
149+
"required": [
150+
"title",
151+
"description",
152+
"isCompleted"
153+
],
154+
"type": "object",
155+
"properties": {
156+
"title": {
157+
"type": "string"
158+
},
159+
"description": {
160+
"type": "string"
161+
},
162+
"isCompleted": {
163+
"type": "boolean"
164+
}
165+
}
166+
}
167+
}
168+
},
169+
"tags": [
170+
{
171+
"name": "Test"
172+
}
173+
]
174+
}

0 commit comments

Comments
 (0)