Skip to content
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

Merged

Conversation

iskandersierra
Copy link
Contributor

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:

genOptions.OperationFilterDescriptors.Add(new FilterDescriptor
{
    // Type = typeof(TdpHeaderParameter),
    FilterInstance = new TdpHeaderParameter(IRequestContext.UserIdKey, "The user ID."),
});

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:

genOptions.AddOperationFilterInstance(
    new TdpHeaderParameter(IRequestContext.TenantIdKey, "The tenant ID."));

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

NOTE: I didn't create a specific issue

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 the Type of the FilterDescriptor.

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.
@iskandersierra iskandersierra marked this pull request as ready for review November 2, 2024 17:33
Copy link
Collaborator

@martincostello martincostello left a 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?

Co-authored-by: Martin Costello <martin@martincostello.com>
@codecov-commenter
Copy link

codecov-commenter commented Nov 4, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.44%. Comparing base (e1c5153) to head (acb7dbc).

❗ 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           
Flag Coverage Δ
Linux 91.44% <100.00%> (+<0.01%) ⬆️
Windows 81.84% <100.00%> (-9.60%) ⬇️
macOS 91.44% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@martincostello martincostello added this to the v7.0.0 milestone Nov 4, 2024
@martincostello martincostello enabled auto-merge (squash) November 4, 2024 12:44
@martincostello martincostello merged commit 0554326 into domaindrivendev:master Nov 4, 2024
9 checks passed
@iskandersierra iskandersierra deleted the fix/public-api-issue branch November 4, 2024 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants