-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor filter descriptor type checks in SwaggerGen #3125
Refactor filter descriptor type checks in SwaggerGen #3125
Conversation
Introduce `IsAssignableTo` method in `FilterDescriptor` class to check if `FilterInstance` is an instance of a specified type or if the `Type` property is assignable to the specified type. Update `ConfigureSwaggerGeneratorOptions.cs` to use this new method. Remove direct `Type` property assignments in `SwaggerGenOptionsExtensions.cs` for various filter descriptors.
src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test that validates the NullReferenceException
has gone away with the change?
src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs
Outdated
Show resolved
Hide resolved
src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptions.cs
Outdated
Show resolved
Hide resolved
test/Swashbuckle.AspNetCore.SwaggerGen.Test/ConfigureSwaggerGeneratorOptionsTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Martin Costello <martin@martincostello.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #3125 +/- ##
=======================================
Coverage 91.43% 91.44%
=======================================
Files 76 76
Lines 3119 3121 +2
Branches 519 520 +1
=======================================
+ Hits 2852 2854 +2
Misses 267 267
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Pull Request
Given that the public API allows to register a filter in this form without any specific validation on not assigning the Type property:
in which case, opening the swagger UI returns a
NullReferenceException
(in developer error page) with no reference to user source code.Looking into the library source code, I found out that the "proper" way to do the registration is:
but I just felt that the first option should also be available with minimal behavior changes, given that it is a possible choice in the public API.
The issue or feature being addressed
Details on the issue fix or feature implementation
I added an alternative validation on the filter type for when the
FilterInstance
is set but not theType
of theFilterDescriptor
.