17

One of my endpoints returns a JSON (not huge, around 2MB). Trying to run GET on this endpoint in swagger-ui results in the browser hanging for a few minutes. After this time, it finally displays the JSON.

Is there a way to define that the response shouldn't be rendered but provided as a file to download instead?

I'm using OpenAPI 3, and I tried the following:

content:
    application/json:
        schema:
            type: string
            format: binary

taken from the documentation. Still, swagger-ui renders the response.

Has anyone had the same problem?

idetyp
  • 486
  • 1
  • 3
  • 13
  • 4
    Known issue: https://github.com/swagger-api/swagger-ui/issues/3832, https://github.com/swagger-api/swagger-ui/issues/6206, https://github.com/swagger-api/swagger-ui/issues/6202 – Helen Aug 27 '20 at 16:39
  • saw these ones, so it seems there is no solution for now – idetyp Aug 28 '20 at 12:35
  • 2
    Check https://github.com/swagger-api/swagger-ui/issues/3832#issuecomment-860730706 - by setting the Content-Disposition header, it is possible to control the download behaviour. Works for us. – Toke Eskildsen Aug 02 '21 at 07:55
  • 3
    Problem is known for more than 5 years know. Still not fixed :( – spyro Aug 27 '21 at 15:23

3 Answers3

17

Lex45x proposes in this github issue to disable syntax highlighting. In ASP.Net Core you can do this with

app.UseSwaggerUI(config =>
{
    config.ConfigObject.AdditionalItems["syntaxHighlight"] = new Dictionary<string, object>
    {
        ["activated"] = false
    };
});

This significantly improves render performance.

PeterB
  • 886
  • 10
  • 18
  • How do I add the `syntaxHighlight` false for the OpenAPI Quarkus service? I am facing a problem so I tried a couple of things but still not working. Can you please have a look at this post? https://stackoverflow.com/q/73984320/7584240 – BATMAN_2008 Oct 07 '22 at 09:30
3

The equivalent to PeterB's answer on newer versions of NSwag (using UseSwaggerUI3) looks like this:

app.UseSwaggerUi3(settings =>
{
   settings.AdditionalSettings.Add("syntaxHighlight", false);
});
Luke
  • 743
  • 6
  • 20
  • How do I add the `syntaxHighlight` false for the OpenAPI Quarkus service? I am facing a problem so I tried a couple of things but still not working. Can you please have a look at this post? https://stackoverflow.com/q/73984320/7584240 – BATMAN_2008 Oct 07 '22 at 09:30
1

The equivalent in spring-boot, if using SpringDoc-openapi, is to add this to your application.yml

springdoc:
  swagger-ui:
    syntaxHighlight:
      activated: false

Here's the link to the official docs: https://springdoc.org/#swagger-ui-properties

Alan
  • 303
  • 3
  • 10