Skip to content

[release/9.0] [OpenAPI] Use invariant culture for TextWriter #62239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: release/9.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/OpenApi/sample/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public IActionResult PostForm([FromForm] MvcTodo todo)
return Ok(todo);
}

[HttpGet]
[Produces("application/json")]
[ProducesResponseType(typeof(CurrentWeather), 200)]
[Route("/getcultureinvariant")]
public IActionResult GetCurrentWeather()
{
return Ok(new CurrentWeather(1.0f));
}

public class RouteParamsContainer
{
[FromRoute]
Expand All @@ -36,4 +45,6 @@ public class RouteParamsContainer
}

public record MvcTodo(string Title, string Description, bool IsCompleted);

public record CurrentWeather([Range(-100.5f, 100.5f)] float Temperature = 0.1f);
}
27 changes: 27 additions & 0 deletions src/OpenApi/sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Immutable;
using System.ComponentModel;
using System.Globalization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -36,6 +37,32 @@

var app = builder.Build();

// Run requests with a culture that uses commas to format decimals to
// verify the invariant culture is used to generate the OpenAPI document.
app.Use((next) =>
{
return async context =>
{
var originalCulture = CultureInfo.CurrentCulture;
var originalUICulture = CultureInfo.CurrentUICulture;

var newCulture = new CultureInfo("fr-FR");

try
{
CultureInfo.CurrentCulture = newCulture;
CultureInfo.CurrentUICulture = newCulture;

await next(context);
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUICulture;
}
};
});

app.MapOpenApi();
if (app.Environment.IsDevelopment())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public static IEndpointConventionBuilder MapOpenApi(this IEndpointRouteBuilder e
var document = await documentService.GetOpenApiDocumentAsync(context.RequestServices, context.Request, context.RequestAborted);
var documentOptions = options.Get(documentName);
using var output = MemoryBufferWriter.Get();
using var writer = Utf8BufferTextWriter.Get(output);
using var writer = new Utf8BufferTextWriter(System.Globalization.CultureInfo.InvariantCulture);
writer.SetWriter(output);
try
{
document.Serialize(new OpenApiJsonWriter(writer), documentOptions.OpenApiVersion);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;

[UsesVerify]
public sealed class OpenApiDocumentIntegrationTests(SampleAppFixture fixture) : IClassFixture<SampleAppFixture>
Expand All @@ -20,21 +15,12 @@ public sealed class OpenApiDocumentIntegrationTests(SampleAppFixture fixture) :
[InlineData("schemas-by-ref")]
public async Task VerifyOpenApiDocument(string documentName)
{
var documentService = fixture.Services.GetRequiredKeyedService<OpenApiDocumentService>(documentName);
var scopedServiceProvider = fixture.Services.CreateScope();
var document = await documentService.GetOpenApiDocumentAsync(scopedServiceProvider.ServiceProvider);
await Verifier.Verify(GetOpenApiJson(document))
using var client = fixture.CreateClient();
var json = await client.GetStringAsync($"/openapi/{documentName}.json");
await Verify(json)
.UseDirectory(SkipOnHelixAttribute.OnHelix()
? Path.Combine(Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT"), "Integration", "snapshots")
: "snapshots")
.UseParameters(documentName);
}

private static string GetOpenApiJson(OpenApiDocument document)
{
using var textWriter = new StringWriter(CultureInfo.InvariantCulture);
var jsonWriter = new OpenApiJsonWriter(textWriter);
document.SerializeAsV3(jsonWriter);
return textWriter.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "Sample | controllers",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost/"
}
],
"paths": {
"/getbyidandname/{id}/{name}": {
"get": {
Expand Down Expand Up @@ -88,9 +93,41 @@
}
}
}
},
"/getcultureinvariant": {
"get": {
"tags": [
"Test"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CurrentWeather"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"CurrentWeather": {
"type": "object",
"properties": {
"temperature": {
"type": "number",
"format": "float",
"default": 0.1
}
}
}
}
},
"components": { },
"tags": [
{
"name": "Test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "Sample | forms",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost/"
}
],
"paths": {
"/forms/form-file": {
"post": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "Sample | responses",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost/"
}
],
"paths": {
"/responses/200-add-xml": {
"get": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "Sample | schemas-by-ref",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost/"
}
],
"paths": {
"/schemas-by-ref/typed-results": {
"get": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "Sample | v1",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost/"
}
],
"paths": {
"/v1/array-of-guids": {
"get": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
},
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost/"
}
],
"paths": {
"/v2/users": {
"get": {
Expand Down
6 changes: 6 additions & 0 deletions src/SignalR/common/Shared/Utf8BufferTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public Utf8BufferTextWriter()
_encoder = _utf8NoBom.GetEncoder();
}

public Utf8BufferTextWriter(IFormatProvider formatProvider)
: base(formatProvider)
{
_encoder = _utf8NoBom.GetEncoder();
}

public static Utf8BufferTextWriter Get(IBufferWriter<byte> bufferWriter)
{
var writer = _cachedInstance;
Expand Down
Loading