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

SwaggerSchemaFilterAttribute #445

Closed
wants to merge 1 commit into from
Closed

SwaggerSchemaFilterAttribute #445

wants to merge 1 commit into from

Conversation

xMarkos
Copy link

@xMarkos xMarkos commented Jul 31, 2015

Changes in this pull request

  • allowing to use multiple attributes
  • when applying SwaggerSchemaFilterAttribute, also applying them from base class

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):

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

Multiple filters
Motivation:

[SwaggerSchemaFilter(typeof(A))]
[SwaggerSchemaFilter(typeof(CommonFilter))]
class A : ISchemaFilter {}
  • with multiple attributes it is possible to have
    • filters specific to the class
    • generic filters applied to set of classes
  • before the patch, we needed to create wrapping filter that applies both filters manually, which is
    • tedious
    • needs to create wrapping class filters for each scenario
    • hard to maintain

Signed-off-by: Marek Hübsch [email protected]

- 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]>
@ugumba
Copy link

ugumba commented Oct 14, 2016

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>();
Copy link
Owner

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.

Copy link
Author

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.

Copy link
Owner

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

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