swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.56k stars 8.96k forks source link

Lazy load model properties #4280

Open heldersepu opened 6 years ago

heldersepu commented 6 years ago
Q A
Bug or feature request? Performance Bug
Which Swagger/OpenAPI version? 2.0
Which Swagger-UI version? 3.11
How did you install Swagger-UI? PetStore
Which browser & version? Chrome
Which operating system? Win 10

Demonstration API definition

petstore.swagger.io/...&url=https://heldersepu.github.io/hs-scripts/swagger/4248_swagger.json

Expected Behavior

Fast expansion of model properties

Current Behavior

Very slow expansion of model properties

Possible Solution

Lazy load model properties like we do the models

Context

Looking at the current implementation, the collapsed models show right away (big thanks 2 @shockey)

Expanding small models is quick, but the largest models (such as Patrimonio ) takes quite a while, once it fully expands, expanding child models takes no time... I believe that is because we are loading the entire tree of the model.

shockey commented 6 years ago

I think the best approach for this is to add a depth option to the subtree resolver, which would limit how many levels of $refs are resolved.

For example, depth: 1 would prevent resolution of any data that was placed as a result of an earlier resolution cycle.

This will be challenging: Swagger-Client will need some reworking in order to keep up with depth (we can't encode that metadata into the definition as we go, since that data may be used as base data for a later call to the resolver), and Swagger-UI will need to be able to tell when a subtree needs further resolution (currently it just checks to see if data for a given path exists at all).

naren19 commented 6 years ago

how to set content type in swagger?

bestmike007 commented 6 years ago

Profiling the expanding action:

image

Rendering the whole model property tree takes half of the time; and converting the model tree to an immutable map takes the other half.

Profiling the folding action:

image

It's worth mentioning that, lazy loading model properties should only delay the performance degradation, because the more properties loaded the less it could be efficient and fluent.

Storing the spec with a plain json object in the state can avoid the conversion; but is error-prone without enforcing a code convention and code review. And it also requires a lot of changes to the existing code.

Is it possible to delay the subtree resolving process to the render function? Other than lazy loading, delay resolving also improves the performance. Resolving includes:

There's no need to load/resolve/render unless necessary.

I've also noticed that unnecessary re-rendering happens.

image

But I'm not sure how much it is slowing down.

lakshmikanthadn commented 6 years ago

We have similar problem, where many of our models have more properties and each property and it sub properties takes lot of time to expand and browser hangs some time to load its properties. It would be helpful if we have a lazy load option for the properties. may be by setting some depth option. Thanks in advance.