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

213 comments:

«Oldest   ‹Older   201 – 213 of 213
Clark said...

I was exactly searching for. Thanks for such a post and please keep it up.
lean six sigma new hampshire

Angel17 said...

This is really helpful. Thanks for sharing your insights! ventshades

Clark said...

Thanks for sharing this information. Good luck with the site.
Tampa home buyers

Clark said...

Thanks for sharing this information. Good luck with the site.
www.concretecontractortampafl.com/concrete-repair-and-maintenance-tampa

Clark said...

nice post.CSTechbook

James said...

This is great, should have seen this sooner.

www.webuyhousesportstluciefl.com/how-to-sell-a-house-as-is-when-it-needs-repairs

Angel17 said...
This comment has been removed by the author.
Nishan Singh said...

I have been reading your posts frequently. I need to say that you are doing a fantastic work. Please keep up the great work.

Pharmacy Dropshipper said...

สล็อตออนไลน์โจ๊กเกอร์ เพื่อให้ลูกค้าได้รับบริการ เว็บตรง จากค่าย Joker123 ไม่ผ่านเอเย่น ผู้เล่นจึงมั่นใจได้ว่า จะได้รับการบริการอย่างตรงไปตรงมา

dbcity said...

Choosing DB City was one of the best decisions I've made. From its prime location to its top-notch amenities, every aspect surpasses expectations. Living here truly feels like a dream come true. apartments for sale in Gwalior

Wowbeauty said...

The blog is simply superb. houses in gwalior

Wowbeauty said...

Fantastic piece of content! flats in gwalior

Wowbeauty said...

Thank you for providing such valuable content and insightful information. social media promotion agency gwalior

«Oldest ‹Older   201 – 213 of 213   Newer› Newest»