{"id":1365,"date":"2018-04-23T10:00:27","date_gmt":"2018-04-23T08:00:27","guid":{"rendered":"http:\/\/tpodolak.com\/blog\/?p=1365"},"modified":"2018-05-01T14:02:27","modified_gmt":"2018-05-01T12:02:27","slug":"asp-net-web-api-2-missing-swagger-query-parameters","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2018\/04\/23\/asp-net-web-api-2-missing-swagger-query-parameters\/","title":{"rendered":"\u00a0ASP.NET Web API 2 &#8211; missing swagger query parameters"},"content":{"rendered":"<p>Last week I&#8217;ve encountered an interesting bug in swagger documentation of our API. For some reason couple of <i>Get<\/i> operations were missing some of the query parameters. The issue itself was only visible in a couple of controllers, simplified version of one of them is shown below<\/p>\n<pre lang=\"csharp\">\r\npublic class ValuesController : ApiController\r\n{\r\n    \/\/ GET api\/values\r\n    public IEnumerable<string> Get([FromUri] FullAvailabilityRequest request)\r\n    {\r\n        return new string[] {\"value1\", \"value2\"};\r\n    }\r\n}\r\n<\/pre>\n<p>As you can see the controller has a Get method which takes as an argument following class<\/p>\n<pre lang=\"csharp\">\r\npublic class FullAvailabilityRequest : AvailabilityRequest\r\n{\r\n    public string TermsOfService { get; set; }\r\n}\r\n\r\n[DataContract]\r\npublic class AvailabilityRequest\r\n{\r\n    [DataMember]\r\n    public string Origin { get; set; }\r\n\r\n    [DataMember]\r\n    public string Destination { get; set; }\r\n}\r\n<\/pre>\n<p>If you run swagger for <i>Get<\/i> operation you will see that only two parameters are listed in a parameter list<br \/>\n<a href=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/MissingParameter.png\"><img decoding=\"async\" src=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/MissingParameter.png\" alt=\"\" height=\"481\" class=\"aligncenter size-full wp-image-1368\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/MissingParameter.png 978w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/MissingParameter-150x74.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/MissingParameter-300x148.png 300w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/a><br \/>\nAfter some digging I found the actual problem were basically <i>[DataMember]<\/i> and <i>[DataContract]<\/i> attributes randomly scattered in the models used in <i>Get<\/i> methods arguments. Even though these attributes are not used for model binding from the query string, swagger takes them into account either way. It happens basically because swagger under the hood uses <i>JsonObjectContract<\/i> (even for [FromUri] request classes) and by default it respects <i>[DataContract]<\/i> attribute &#8211; more info can be found <a href=\"http:\/\/james.newtonking.com\/archive\/2009\/10\/23\/efficient-json-with-json-net-reducing-serialized-json-size\">here<\/a>. Removing redundant <i>[DataContract]<\/i> and <i>[DataMember]<\/i> attributes from respective models solved the issue.<br \/>\n<a href=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/FixedMissingParameter.png\"><img decoding=\"async\" src=\"\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/FixedMissingParameter.png\" alt=\"\" height=\"510\" class=\"aligncenter size-full wp-image-1367\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/FixedMissingParameter.png 975w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/FixedMissingParameter-150x78.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2018\/04\/asp-net-web-api-2-missing-swagger-query-parameters\/FixedMissingParameter-300x157.png 300w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><\/a><\/p>\n<p>PS. This particular corner case is not an issue for <i>ASP.NET Core<\/i> swagger implementation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week I&#8217;ve encountered an interesting bug in swagger documentation of our API. For some reason couple of Get operations were missing some of the query parameters. The issue itself was only visible in a couple of controllers, simplified version of one of them is shown below public class ValuesController : ApiController { \/\/ GET [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[333],"tags":[334,331],"class_list":["post-1365","post","type-post","status-publish","format-standard","hentry","category-asp-net-web-api","tag-asp-net-web-api","tag-swagger"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=1365"}],"version-history":[{"count":5,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1365\/revisions"}],"predecessor-version":[{"id":1372,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/1365\/revisions\/1372"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}