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

Invalid JSON output #2700

Closed
christopheblin opened this issue Aug 31, 2023 · 2 comments · Fixed by #2677
Closed

Invalid JSON output #2700

christopheblin opened this issue Aug 31, 2023 · 2 comments · Fixed by #2677

Comments

@christopheblin
Copy link

If you have a double property with an example in one of your models, the swagger.json is invalid JSON if the culture is not en-US

class MyDto 
{ 
    /// <summary>
    ///   Test
    /// </summary>
    /// <example>84420.01</example>
    public double? Test { get; set; }
}

JSON output :

     "test": {
        "type": "number",
        "description": "Test",
        "format": "double",
        "nullable": true,
        "example": 84420,01
      },

In order for it to work, I needed this hack :

public static void GenerateSwaggerFile(this IHost app, string url, string apiVersion = "v1", string outputJsonFilePath = "swagger.json")
{
    //super hacky way in order to generate correct examples for double
    //see https://stackoverflow.com/q/76810078/1545567
    CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
    CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;

    var swaggerProvider = app.Services.GetRequiredService<ISwaggerProvider>();
    var swagger = swaggerProvider.GetSwagger(apiVersion);
    swagger.Servers = new[] { new OpenApiServer { Url = url } };
    using var streamWriter = File.CreateText(outputJsonFilePath);
    var writer = new OpenApiJsonWriter(streamWriter);
    swagger.SerializeAsV3(writer);
}
@Havunen
Copy link

Havunen commented Feb 18, 2024

This is fixed in DotSwashbuckle, can you test using it please.
https://github.com/Havunen/DotSwashbuckle
https://www.nuget.org/packages/DotSwashbuckle.AspNetCore

@martincostello
Copy link
Collaborator

Proposed fix in #2726

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

Successfully merging a pull request may close this issue.

3 participants