Swashbuckle.AspNetCore
Swashbuckle.AspNetCore copied to clipboard
Set culture of Json Serializer to InvariantCulture
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.
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" />
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.)
@desjoerd can you confirm you are not doing anything culture specific in your project?
Proposed fix in #2726
@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.