domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.24k stars 1.31k forks source link

Swashbuckle.AspNetCore.SwaggerGen.TypeExtensions.FullNameSansTypeArguments(Type type) in TypeExtensions.cs, line 20 #715

Closed cnlizhipeng closed 6 years ago

cnlizhipeng commented 6 years ago

My abstract class has this function

void Delete(ICollection<KeyType> input);

There will be a mistake

I tried to modify the following code to solve my problem.

 internal static string FullNameSansTypeArguments(this Type type)
        {
                var fullName = type.FullName??type.ToString();
                var chopIndex = fullName.IndexOf("[[");
                return (chopIndex == -1) ? fullName : fullName.Substring(0, chopIndex);
        }

Are there any other better things to do, or my use is wrong?

aspnetlabs commented 6 years ago

Hi, I am facing a similar issue.

My application source code https://github.com/aspnetlabs/Dynamic-WebAPI-Controllers

This is the stack trace:

Microsoft.AspNetCore.Server.Kestrel:Error: Connection id "0HLE3ADDEN75D": An unhandled exception was thrown by the application.

System.NullReferenceException: Object reference not set to an instance of an object. at Swashbuckle.AspNetCore.SwaggerGen.TypeExtensions.FullNameSansTypeArguments(Type type) at Swashbuckle.AspNetCore.SwaggerGen.XmlCommentsMemberNameHelper.BuildTypeToken(Type type) at System.Linq.Enumerable.SelectArrayIterator2.MoveNext() at System.String.Join(String separator, IEnumerable1 values) at Swashbuckle.AspNetCore.SwaggerGen.XmlCommentsMemberNameHelper.GetMemberNameForMethod(MethodInfo method) at Swashbuckle.AspNetCore.SwaggerGen.XmlCommentsOperationFilter.Apply(Operation operation, OperationFilterContext context) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions, ISchemaRegistry schemaRegistry) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer`1 comparer) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] schemes) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.d6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.d8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at

Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Frame`1.d__2.MoveNext()

PatrickTetz commented 6 years ago

Just faced the same issue on a generic class, any news?

The MSDN for Type.FullName says that this property return

null if the current instance represents a generic type parameter, an array type, pointer type, or byreftype based on a type parameter, or a generic type that is not a generic type definition but contains unresolved type parameters.

It doesn't even matter if I add xml comments on that generic class as SwashBuckle seems to autodiscover the whole API. (As observed with Resharper debugging of external modules)

I'd classify the current implementation as a bug, looks like I have to disable documentation for now as the generic type controller is required to save a huge amount of redundant code.

aspnetlabs commented 6 years ago

Above solution did't completly work for me, I was not able to see the comments on swagger page, after adding this post build script it resolved the issue.

c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "$x=(Get-Content $(TargetDir)\$(ProjectName).xml) -replace '{`0}', '{T}[T]' | Out-File $(TargetDir)\$(ProjectName).xml"

domaindrivendev commented 6 years ago

See the link below - I've come up with a test to repro but have it commented out because I haven't had the time to find a solution. Those XML comments member names are extremely cryptic, particularly when generics come into play and I haven't had the time to give it the focus it needs:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/test/Swashbuckle.AspNetCore.SwaggerGen.Test/Annotations/XmlCommentsMemberNameHelperTests.cs#L29

domaindrivendev commented 6 years ago

If anyone else wants to pull it down and take a stab that would be awesome?

domaindrivendev commented 6 years ago

@cnlizhipeng, @aspnetlabs, @PatrickTetz I believe I've implemented a fix for this, pushed with the following commit - 0b6e0b2

It should be available with the following preview package on myget.org:

Swashbuckle.AspNetCore.2.5.0-preview-0510.nupkg

Could you please pull it down, test it out and provide feedback? Thanks

domaindrivendev commented 6 years ago

I believe this is now fixed as of v2.5.0. Feel free to re-open if you're still seeing issues with that version or above