Skip to content
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

Set culture of Json Serializer to InvariantCulture #2105

Closed
michaelrazmgah opened this issue Apr 23, 2021 · 5 comments · Fixed by #2677 · May be fixed by #2515
Closed

Set culture of Json Serializer to InvariantCulture #2105

michaelrazmgah opened this issue Apr 23, 2021 · 5 comments · Fixed by #2677 · May be fixed by #2515

Comments

@michaelrazmgah
Copy link

michaelrazmgah commented Apr 23, 2021

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.

@desjoerd
Copy link
Contributor

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" />

@kaylumah
Copy link

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
Copy link

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

@martincostello
Copy link
Collaborator

Proposed fix in #2726

@desjoerd
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants