简体   繁体   中英

Swashbuckle very slow for large schema

I have a database with over 90 tables, and the tables are all cross linked to each other with foreign keys.

When I open the swagger page for my API, it takes over 2 minutes to load. The reason appears to be that it is generating Model and Example Value for each API. Because of the cross linking, pretty much every entity references every other entity (by recursive transitive closure)!

Is there any way to disable or limit the recursion when swagger generates the Model and Example Value ?

For example,

class A {
   int id;
   List<B> Blist;
}

class B {
   int id;
   List<C> Clist;
}

class C {
   int id;
   List<D> Dlist;
}

/// etc...

If I have an API for GET /api/A then I don't want to pull all the classes into the swagger page Model . It's too huge!! I only want to pull in A.

The answer to your question is short

Is there any way to limit the recursion when swagger generates the Model and Example?

Unfortunately No!

Currently there is no such an option. But it is possible, and we are discussing it:
https://github.com/swagger-api/swagger-ui/issues/4411
Add a comment there and +1 that issue, let the team know that is important to you.

Swagger-UI does not handle complex schemas very well, sometimes is just slow some other it crashes the browser, the team is aware and hopefully we will get a fix soon.

Now your version (2.x) is no longer supported, so there will not be a fix there...
Have you tried Swagger-Net ? That is my fork and I'm using the latest version of the UI

Here's a hack that seems to "fix" it. I added this to my SwaggerConfig.cs

  c.MapType<MasterModel>(() => new Schema { type = "integer", format = "int32" });
  c.MapType<MasterLocationModel>(() => new Schema { type = "integer", format = "int32" });
  c.MapType<LocationModel>(() => new Schema { type = "integer", format = "int32" });

I have lots more types, but these three are the central ones, so by mapping them to integer it limits the recursion to reasonable levels. It also makes the swagger page wrong, but at least it loads!

Perhaps there is a way to do this better using a SchemaFilter ? Any help would be appreciated.

EDIT:

In the end, we decided to split up our classes so that swagger cannot see the references more than one level deep. For example,

class A_Base {
   int id;
}

class A : A_Base {
   List<B_Base> Blist;
}

class B_Base {
   int id;
}

class B : B_Base {
   List<C_Base> Clist;
}

class C_Base {
   int id;
}

class C : C_Base {
   List<D_Base> Dlist;
}

and all our controller API use types A, B, C. The swagger doc will only go one level deep on each. The recursion does not go wildly out of control. Also it is more correct, because the objects returned by our API are usually just zero or one level deep.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM