Wednesday, 3 February 2016

Ignoring parameters in Swashbuckle

I’m working with a WebApi that is passed the HttpRequestMessage class which is always available in the ASP.NET WebApi pipeline. However, when the documentation is exposed in Swashbuckle, I get a textbox that shows up where it expects me to enter a value for this parameter.

The operation is defined as follows…

  public async Task Get(HttpRequestMessage request)

The problem is, when the documentation is emitted I end up with this ugly mess...

image

I set about finding a way to instruct Seashbuckle to ignore this parameter, and finding there isn’t anything in the box I set about writing a filter that would do this for me.

In Swashbuckle you can plug-in operation “filters” that can be used to alter the emitted data – the filter is passed the context of the operation being emitted, and you can monkey around with the data that pops out. All I had to do then was create a filter that would look for this datatype, and remove the corresponding data from the results. I ended up with this…

  public class IgnoreHttpRequestMessageOperationFilter : IOperationFilter
  {
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, 
                      ApiDescription apiDescription)
    {
      apiDescription.ParameterDescriptions
        .Where(desc => desc.ParameterDescriptor.ParameterType 
            == typeof(HttpRequestMessage))
        .ToList()
        .ForEach(param =>
        {
          var toRemove = operation.parameters
            .SingleOrDefault(p => p.name == param.Name);

          if (null != toRemove)
            operation.parameters.Remove(toRemove);
        });
    }
  }

With that class in place, I just needed to plug this in to the swagger config file as follows...

  c.OperationFilter();

And Bob's you're uncle...

image

27 comments:

Anton Bocharov said...

Cool! thanks for pointing this out! On stackowerflow they suggest to rewrite controllers not to use request as a parameter, which is not suitable for us.

Brave Technologies said...
This comment has been removed by a blog administrator.
Akshay Bheda said...

I was searching solution for the same problem, Thanks to you :)

ZeroTolerance said...

Good job mate. Exactly what I was looking for

Harry said...

Worked like charm. Thank you.

Tarek Mohamed El-Mallah said...
This comment has been removed by the author.
Tarek Mohamed El-Mallah said...
This comment has been removed by the author.
Prachetan Anubhuti - Pragna Prachetas said...

works like a magic ... thanks a ton

Richard Majece said...

I have a huge amount of applications in my phone so I was scared when I lost it. https://cellspyapps.org/track-phone-number/ helped me to track my phone number and find it)

HomeWorkFor.me said...

Be sure to check out the new persuasive essay topics.

MyAssignmentHelpSG said...

Blog Commenting: An unmatched and nonpareil post i have ever seen. The content is so appealing that it has created an impulse to avail Assignment Help Singapore services.

Praylin S said...

Remarkable post! That's a lot of information. Thanks for taking your time and effort to share this with us. Keep us updated.
Tally Course in Chennai
Tally Classes in Chennai
JavaScript Training in Chennai
JavaScript Course in Chennai
C C++ Training in Chennai
C Training in Chennai
Tally Course in Anna Nagar
Tally Course in T Nagar

Additri Kapoor said...

Having an assignment due tomorrow and understand you can’t do it yourself? But you’re afraid of delegating your paper to some scam services as well? Why not check https://scamfighter.net/review/unemployedprofessors.com on Scamfighter.net

Elan Progret said...

There are so many types of essays; it can be hard to know where to start. History papers aren’t just limited to history classes. These tasks can be assigned to examine any important historical event or a person. While they’re more common in history classes, you can find this type of assignment in sociology or political science course syllabus, or just get a history essay task for your scholarship.

What is a political essay? A political is just as the name suggests an essay based on politics or a political situation. Completing a political essay is impossible without the proper research to fully understand your subject. First, you should study the primary texts, to analyze its contents. You may take advantage of using reliable Internet sources, with available government reports and political parties' news. Scan through reputable newspapers and magazines to compile relevant data for your political essay. Find more political essay at http://writing-a-thesis.net/

jacklinemelda said...

Our non plagiarized custom term papers are reputable and known for offering high quality custom term papers. We normally write academic papers in all disciplines of study including those seeking for write my nursing paper services.

meldaresearch said...

We are prepared to deliver reliable and trusted Custom Research Paper Writing Services in all the outlined levels of study due to the many experts that we have in our company.Our Affordable Writing Services are affordable and cheap as well. Students have very limited cash flows, and their budgets are usually constrained.

Lisa Sequeira said...

Is myassignmenthelp fake? Find out the complete truth behind the services MyAssignmentHelp provides.

meldaresearch said...

We are the best writing company providing Essay Writer Servicesof the highest quality. We guarantee our customers of nothing but the highest quality document. Our Custom Term Paper Writing Services are well referenced, formatted and edited to ensure that the article is rid of unnecessary details.

Shop Sous Vide said...

A nice post shared.
tree trimmers loxahatchee

Clark said...

Nice post.
tile resurfacing minneapolis

Shop Sous Vide said...

Amaizng.
emergency restoration services fort lauderdale

Clark said...

Great post.
bathroom remodelers clearfield ut

Clark said...

Nice post.
bathroom remodeling clearfield county

Shop Sous Vide said...

A nice post shared.
concrete driveway contractors fort pierce

Shop Sous Vide said...

I really appreciate such a post.
kitchen remodel contractor ogden ut

Shop Sous Vide said...

Good thoughts and well wishes to you and your family. Stay strong!


kitchen remodel contractor fort lauderdale fl

Puremelda said...

It is understood that you do not have adequate time to write. Buy Custom College Papers and submit the Pre Written Research Papers. Make a decision to order Term Paper Writing Help today.