swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Swagger page failing to load for API endpoints with large JSON responses

Open VarunReddy1111 opened this issue 1 year ago • 13 comments

Example Swagger/OpenAPI definition: openapi.yaml.zip

I am using Quarkus 3.5.0 (OpenAPI 3.0.3) embedded swagger, When dealing with API endpoints, due to large json response that gets populated in example schema section, the UI is crashing. Attaching the openapi definition. Please let me know if any other information is needed.

image

VarunReddy1111 avatar Mar 04 '24 20:03 VarunReddy1111

Related to https://github.com/swagger-api/swagger-ui/issues/3832

The current known workaround is using this setting:

    syntaxHighlight: {
      activated: false,
      theme: "agate"
    }

It seems like it's not enough in this case - the examples will sometimes load after hanging and sometimes still crash the browser.

There is a PR https://github.com/swagger-api/swagger-ui/pull/9625 addressing this issue, I'll take a look at it next.

glowcloud avatar Mar 05 '24 10:03 glowcloud

I will be tracking this further in the related issue #3832.

glowcloud avatar Mar 05 '24 14:03 glowcloud

NO PR https://github.com/swagger-api/swagger-ui/pull/9625 is not addressing this issue ... and this issue is not related to issue https://github.com/swagger-api/swagger-ui/issues/3832

heldersepu avatar Mar 07 '24 13:03 heldersepu

The schema in the given openapi.yaml.zip is massive!! once unpacked that is 2.5MB

this is not about "large JSON responses" heck with that schema the UI crashes even without making any requests

Just expanding the "put_api_agm_disease_annotation" crashes, there was no large response because we did not make any request, the [Try it out] button did not even show, you can test it here: https://petstore.swagger.io/?docExpansion=None&defaultModelsExpandDepth=None&url=https://gist.githubusercontent.com/heldersepu/ab37902d2f687d2c71aa1b36bb72ee48/raw/0f68906074dfbad5528a81132ae7975b6bf2ef75/openapi.yml#/CRUD%20-%20AGM%20Disease%20Annotations

the problem on issue 3832 is an actual large response from the API, in that case the syntax highlighter takes too long to render and browsers crashes

heldersepu avatar Mar 07 '24 13:03 heldersepu

Thank you for the response @heldersepu. Yes, the swagger ui crashing while expanding the end point. Is this because of massive openAPI file? and do you have any resolution for this issue?

VarunReddy1111 avatar Mar 07 '24 14:03 VarunReddy1111

This issue is closer in relation to issue 4411, see my comment there:

https://github.com/swagger-api/swagger-ui/issues/4411#issuecomment-385172945

The expansion of a truly complex object creates a LOT of code, the browser will ultimately crash and burn

The problem presented on this issue I would put this schema on the "huge and extra complex" category, it has 84962 lines, that is mind blowing for an API description ... everyone is moving toward micro-service architectures but it looks like this is the opposite movement

heldersepu avatar Mar 07 '24 14:03 heldersepu

Thank you for the response @heldersepu. Yes, the swagger ui crashing while expanding the end point. Is this because of massive openAPI file? and do you have any resolution for this issue?

Sorry but no resolution or even a workaround, and no the syntaxHighlight deactivation is not a workaround for this ... if that is the schema your team wants to use, then Swagger-UI is not the tool for you...

have you heard that saying: "when you are a hammer everything is a nail"

quite often we developers fall in that trap... yes we can use a hammer to cut down a tree, but that is not the right tool.

My recommendation: refactor your code, move to a micro-service architecture

heldersepu avatar Mar 07 '24 14:03 heldersepu

If we want to keep this open for someone to investigate ... here is small schema but with nested definitions that causes the same crash:

https://petstore.swagger.io/?url=https://raw.githubusercontent.com/heldersepu/hs-scripts/master/swagger/NestedSwag.json

heldersepu avatar Mar 07 '24 14:03 heldersepu

Hi there,

I started tracking this as a related issue, because the changes to HighlightCode in https://github.com/swagger-api/swagger-ui/pull/9625 also affect the rendering of example values and with those changes I was able to expand the operations, although after some seconds of waiting. I looked at the issue of rendering such large values, whether for server responses or just example responses, as one.

In the case of this specification, there are further issues with out of memory crashes when using try it out, changing from example to schema or expanding more definitions, which I’m investigating. As I mentioned in https://github.com/swagger-api/swagger-ui/issues/3832#issuecomment-1983179886, this might be related to doing many operations on immutable values, in which case it might be a separate issue.

I can also confirm that your example @heldersepu https://raw.githubusercontent.com/heldersepu/hs-scripts/master/swagger/NestedSwag.json will also be possible to expand with the PR changes, but the memory usage will be high:

Screenshot 2024-03-07 at 16 21 47

glowcloud avatar Mar 07 '24 15:03 glowcloud

Yes the changes to HighlightCode offer some improvement to the examples, but the examples are significantly more complex unlike the responses from the API that we just show what we receive, the nested examples the entire tree is created, that will cause some serious memory consumption...

but back to my point: this issue is not related to issue https://github.com/swagger-api/swagger-ui/issues/3832 I see no reason to close this issue

@glowcloud try expanding the models in that nested example see if that helped there

heldersepu avatar Mar 07 '24 16:03 heldersepu

I can expand the models

Screenshot 2024-03-08 at 08 41 03

but opening more of them (as in, not the nested ones in the screenshot, just separate models) will cause a crash at some point. Seems like it's also a memory related crash.

glowcloud avatar Mar 08 '24 08:03 glowcloud

Reopening this as it's confirmed now that this is unrelated to https://github.com/swagger-api/swagger-ui/issues/3832

char0n avatar Mar 08 '24 08:03 char0n

The swagger-client either creates unnecessary nesting instead of stopping on first detected cycle. It might also be the case that the resolver is functioning OK, but then the SwaggerUI is doing it's additional primitive "dereferencing" itself which might again introduce cycles.

@glowcloud confirmed that the definition resolved immediately, so the probably candidate is the primitive "dereferencing" inside the SwaggerUI.

char0n avatar Mar 08 '24 08:03 char0n