Swagger崩溃与圆形模型参考 [英] Swagger crashes with circular model references

查看:134
本文介绍了Swagger崩溃与圆形模型参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.Net Core 2.2 OData API,我想为此实现Swagger文档.

I have a .Net Core 2.2 OData API, for which I'm tying to implement Swagger documentation.

我一直在关注此示例: https://github.com/Microsoft/aspnet-api-versioning/tree/master/samples/aspnetcore/SwaggerODataSample

I've been following this example: https://github.com/Microsoft/aspnet-api-versioning/tree/master/samples/aspnetcore/SwaggerODataSample

我已经做到了一点.现在,我的模型以循环方式相互引用时面临一个问题,让我解释一下:

I've got it working up to a point. Now I'm facing an issue with my models referencing each other in a circular manner, let me explain:

注意:我使用EFCore Code first方法处理数据库.

我有以下模型(作为示例):Project,ProjectLocation,ProjectRegion.我们称它们为A,B和C使内容简短.

I have these models (as an example): Project, ProjectLocation, ProjectRegion. Lets call them A, B & C to keep things short.

A引用了B&C像这样:

A has references to B & C like so:

public virtual ICollection<X> X{ get; set; }

而B&C像这样直接引用A:

And both B & C reference A directly like so:

public A A{ get; set; }

这都是标准的关系数据库模型,但是SwaggerUI似乎无法处理.

This is all pretty standard relational DB model stuff, but it seems SwaggerUI can't handle this.

我得到的错误如下:

无法加载API定义.错误:提取错误:内部服务器错误/swagger/v1/swagger.json

Failed to load API definition. Errors: Fetch error: Internal Server Error /swagger/v1/swagger.json

处理请求时发生未处理的异常.TypeLoadException:无法从程序集"Tc2fc56a7babe40419a678a075439246c.DynamicModels,版本= 0.0.0.0,文化=中性,PublicKeyToken =空"中加载类型"NCCRD.Services.DataV2.Database.Models.ProjectLocation".

An unhandled exception occurred while processing the request. TypeLoadException: Could not load type 'NCCRD.Services.DataV2.Database.Models.ProjectLocation' from assembly 'Tc2fc56a7babe40419a678a075439246c.DynamicModels, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

System.Signature.GetSignature(Void * pCorSig,int cCorSig,RuntimeFieldHandleInternal fieldHandle,IRuntimeMethodInfo methodHandle,RuntimeType declaringType)

System.Signature.GetSignature(Void* pCorSig, int cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)

如果需要,还可以使用堆栈跟踪.

我已尽我所能阅读了这两篇文章和所有参考链接,但找不到有效的解决方案:

I've read these two posts and all of the references links as far as I could go, but did not find a solution that works:

https://github.com/swagger-api/swagger-codegen/issues/741

https://github.com/swagger-api/swagger-codegen/issues/728

一次似乎不错的解决方案是:

Once solution that seemed good was this: https://systemout.net/2017/04/07/swagger-asp-net-core-fixing-circular-self-references/, but this too had no effect. (I contacted to author for help too)

从B或C中删除对A的引用后,SwaggerUI就会很好地加载.

The moment I remove the reference to A from either B or C, then SwaggerUI loads up fine.

如果有人可以对此有所了解,我将非常感激.预先感谢您提供的任何帮助.

If anyone could shed some light on this I'd really appreciate it. Thanks in advance to any help offered.

推荐答案

经过两天的深入研究和实验,我找不到开箱即用的解决方案.Swashbuckle曾经是我的首选,但是因为我在.NetCore中使用Odata,所以这不是一个选择.Swashbuckle为OData和.NetCore分别提供了选项,但不幸的是,不能同时使用.

After two days of intense research and experimentation, I was unable to find any solutions that work out of the box. Swashbuckle used to be my goto for this, but because I'm using Odata in .NetCore that's not an option. Swashbuckle offers options for OData and .NetCore separately, but unfortunately not when used together.

但是,我确实找到了一个解决方案,通过我的一些努力,我能够将其转换为适合我需要的东西.以下帖子构成了我最终解决方案的起点(仍然是一个正在进行的BTW): https://stackoverflow.com/a/51774147/4261155

I did however find a solution, that with some effort I was able to convert into something that works for my needs. The following post formed the starting point of my eventual solution (which is still a work-in-progress BTW): https://stackoverflow.com/a/51774147/4261155

我的"CustomDocumentFilter"版本位于GitHub:

My version of "CustomDocumentFilter" is up on GitHub: https://github.com/SAEONData/NCCRD/blob/swagger_odata_netcore/NCCRD_API/NCCRD.Services.DataV2/Database/Contexts/CustomDocumentFilter.cs

如前所述,这项工作仍在进行中,因此将在未来几天内进行更改,但是与原始版本一起,我希望这可以对处于相同情况的其他人有所帮助.另外请记住,该类经过修改以满足我的特定需求,而不是为其他任何人提供的开箱即用"的解决方案.

As mentioned, this is still a work in progress so it'll be changing over the next few days, but together with the original version, I'm hoping this might help someone else in this same situation. Also keep in mind, this class was adapted to suit my specific needs, and is not intended as an "out of the box" solution for anyone else.

这篇关于Swagger崩溃与圆形模型参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆