-
Notifications
You must be signed in to change notification settings - Fork 678
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
SwaggerSchemaFilterAttribute #445
SwaggerSchemaFilterAttribute #445
Conversation
- now allowing to use multiple attributes - when applying SwaggerSchemaFilterAttribute, also applying them from base class Motivation: When having class B derived from class A, while class A applies filter and modifies its fields in a way, if we also want to modify (the new) fields in class B, we needed to do something like this (pseudo code): class BFilter: ISchemaFilter { void Apply(schema) { ISchemaFilter baseFilter = new AFilter(); baseFilter.Apply(schema); //now apply logic for BFilter } } Which is: - tedious - error prone - needs to know what class is the filter of my base class With the fix it is possible to have code: [SwaggerSchemaFilter(typeof(AFilter))] class A {} [SwaggerSchemaFilter(typeof(BFilter))] class B : A {} - this will first apply AFilter, then BFilter - this will work seamlessly without any additional code in filters Signed-off-by: Marek Hübsch <[email protected]>
I've been hit by this - any progress on getting this merged? |
using System.Web.Http.Description; | ||
|
||
namespace Swashbuckle.Swagger.Annotations | ||
{ | ||
public class ApplySwaggerSchemaFilterAttributes : ISchemaFilter | ||
{ | ||
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) | ||
{ | ||
var attributes = type.GetCustomAttributes(false).OfType<SwaggerSchemaFilterAttribute>(); |
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.
Shouldn't simply changing the "inherit" flag to true in the call to GetCustomAttributes satisfy this requirement. I just tried it out and it appears to work. No need for all the code below.
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.
The point of the additional code was to apply filters in the correct order. Just using true gets attributes in order derived class -> base class.
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.
Does ordering really matter? If there's a specific use-case I might be willing to make the trade-off (i.e. introducing more complex code), otherwise I'd just opt for the simple solution
1b3dcfa
to
0996fa3
Compare
Changes in this pull request
Inheriting attributes
Motivation:
When having class B derived from class A, while class A applies filter and modifies its fields in a way, if we also want to modify (the new) fields in class B, we needed to do something like this (pseudo code):
Which is:
With the fix it is possible to have code:
Multiple filters
Motivation:
Signed-off-by: Marek Hübsch [email protected]