Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

Set culture of Json Serializer to InvariantCulture

Open michaelrazmgah opened this issue 3 years ago • 5 comments

Version 6.1.1

I noticed a bug where the examples in our swagger.json files that were generated did not include correctly formatted values for the data type "double". Instead of being formatted as "123.123" it was being formatted as "123,123". The issue was that our application is using the sv-SE culture CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("sv-SE");.

I imagine a lot of applications want to specify their own culture, but shouldn't the generated swagger.json always be using InvariantCulture so we won't have to face these issues?

I know this is an easy fix for Newtonsoft.Json, but since Swashbuckle is using System.Text.Json, I'm not sure what the best way to solve this is, otherwise I would have submitted a PR.

michaelrazmgah avatar Apr 23 '21 14:04 michaelrazmgah

For people searching for a temporary work around, set environment variable DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 as described here: https://learn.microsoft.com/en-US/dotnet/core/runtime-config/globalization#invariant-mode.

i.e. in csproj:

<Exec EnvironmentVariables="DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1" Command="dotnet tool run swagger tofile --yaml --output openapi.yaml $(OutputPath)$(AssemblyName).dll service" />

desjoerd avatar Jan 23 '23 17:01 desjoerd

Nice, @domaindrivendev does the PR by @dinar007007 look like something you accept?

The workaround purposed by @desjoerd does not work for me, it disables all access to other cultures and would result in

Unhandled exception. System.AggregateException: One or more errors occurred. (Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
nl-NL is an invalid culture identifier.)

kaylumah avatar Apr 11 '23 09:04 kaylumah

@desjoerd can you confirm you are not doing anything culture specific in your project?

kaylumah avatar Apr 13 '23 13:04 kaylumah

Proposed fix in #2726

martincostello avatar Apr 14 '24 09:04 martincostello

@desjoerd can you confirm you are not doing anything culture specific in your project?

Not in our apis or in the codepath the generator uses, so thats probably why it works for us.

desjoerd avatar Apr 14 '24 13:04 desjoerd