<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Swagger-OpenAPi - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/swagger-openapi/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sun, 10 Mar 2024 15:10:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>Swagger-OpenAPi - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Post File in Swagger OpenAPI V3.0 and V2.0 examples</title>
		<link>https://thecodebuzz.com/post-file-in-swagger-openapi-v3-0-and-v2-0-examples/</link>
					<comments>https://thecodebuzz.com/post-file-in-swagger-openapi-v3-0-and-v2-0-examples/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 24 Jul 2022 04:27:00 +0000</pubDate>
				<category><![CDATA[Swagger-OpenAPi]]></category>
		<category><![CDATA[open api specification]]></category>
		<category><![CDATA[open api swagger]]></category>
		<category><![CDATA[openapi 3.0 example]]></category>
		<category><![CDATA[openapi 3.0 example json]]></category>
		<category><![CDATA[openapi additionalproperties]]></category>
		<category><![CDATA[openapi example json]]></category>
		<category><![CDATA[openapi example yaml]]></category>
		<category><![CDATA[openapi examples]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22492</guid>

					<description><![CDATA[<p>Post File in Swagger OpenAPI V3.0 and V2.0 examples Today in this article, we will learn how to Post File in Swagger OpenAPI V3.0 and V2.0 examples using OpenAPI Specification or Swagger specifications. Swagger&#160;or&#160;Open API specification&#160;provides the advantage of understanding the REST services easily (especially if developers are consuming any new API ) plus helps [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/post-file-in-swagger-openapi-v3-0-and-v2-0-examples/">Post File in Swagger OpenAPI V3.0 and V2.0 examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading" id="aioseo-post-file-in-swagger-openapi-v3-0-and-v2-0-examples"><strong>Post File in Swagger OpenAPI V3.0 and V2.0 examples</strong></h1>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="431" src="https://www.thecodebuzz.com/wp-content/uploads/2022/08/Upload-files-in-swagger-2.0-and-OpenAPi-3.0-1024x431.jpg" alt="Post File in Swagger OpenAPI V3.0 and V2.0" class="wp-image-23039" srcset="https://thecodebuzz.com/wp-content/uploads/2022/08/Upload-files-in-swagger-2.0-and-OpenAPi-3.0-1024x431.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/08/Upload-files-in-swagger-2.0-and-OpenAPi-3.0-300x126.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/08/Upload-files-in-swagger-2.0-and-OpenAPi-3.0-768x323.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/08/Upload-files-in-swagger-2.0-and-OpenAPi-3.0-1536x646.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/08/Upload-files-in-swagger-2.0-and-OpenAPi-3.0-785x330.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/08/Upload-files-in-swagger-2.0-and-OpenAPi-3.0.jpg 1892w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today in this article, we will learn how to Post File in Swagger OpenAPI V3.0 and V2.0 examples using <strong><em>OpenAPI Specification</em></strong> or <strong><em>Swagger </em></strong>specifications.</p>



<p></p>



<p><strong>Swagger&nbsp;</strong>or&nbsp;<strong>Open API specification</strong>&nbsp;provides the advantage of understanding the REST services easily (especially if developers are consuming any new API ) plus helps provide easy documentation and details of capabilities given Service/API owns.</p>



<p></p>



<p>We will cover below in today&#8217;s article,</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-post-file-in-swagger-openapi-v3-0-and-v2-0-examples">Post File in Swagger OpenAPI V3.0 and V2.0 examples</a><ul><li><a href="#aioseo-openapi-json-schema">Swagger 2.0 JSON schema &#8211; Upload/Post file</a></li><li><a href="#aioseo-swagger-2-0-upload-single-file">Swagger 2.0 Upload single file</a></li><li><a href="#aioseo-openapi-json-schema">Swagger 2.0 Upload multiple files</a></li><li><a href="#aioseo-openapi-json-schema">OpenAPI 3.0 JSON schema &#8211; Upload/Post file</a></li><li><a href="#aioseo-openapi-3-0-swagger-upload-single-file">OpenAPI 3.0 swagger Upload single file</a></li><li><a href="#aioseo-openapi-3-0-swagger-upload-multiple-files">OpenAPI 3.0 swagger Upload multiple files</a></li></ul></li></ul></div>
</div></div>



<p></p>



<h2 class="wp-block-heading" id="aioseo-openapi-json-schema">Swagger 2.0 JSON schema &#8211; Upload/Post file </h2>



<p></p>



<p>S<em>wa</em>gger  2.0 JSON schema defines the form parameter like <strong><em>in:formData</em></strong> with type as a file. Below are two fields that can be used to specify files within swagger 3.0 specifications,</p>



<p></p>



<ul class="wp-block-list"><li>Field Name: consumes </li></ul>



<ul class="wp-block-list"><li>Value :  &#8220;multipart/form-data&#8221;</li></ul>



<p></p>



<p>Also </p>



<ul class="wp-block-list"><li>Field Name &#8220;parameters&#8221; can be defined with the below value,</li></ul>



<pre class="wp-block-preformatted has-medium-font-size">            "in": "formData",
            "name": "file",
            "type": "file"</pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-swagger-2-0-upload-single-file">Swagger 2.0 Upload single file </h2>



<p></p>



<p>Below is <strong><em>OpenAPI </em></strong>V3.0 JSON schema definition for the image file,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2,13,14,15,16,17,18,19,20,21,22]; title: ; notranslate">
{
  &quot;swagger&quot;: &quot;2.0&quot;,
  &quot;info&quot;: {
    &quot;title&quot;: &quot;TheCodeBuzz&quot;,
    &quot;version&quot;: &quot;v1&quot;
  },
  &quot;paths&quot;: {
    &quot;/WeatherForecast/UploadFile&quot;: {
      &quot;post&quot;: {
        &quot;tags&quot;: &#x5B;
          &quot;WeatherForecast&quot;
        ],
        &quot;consumes&quot;: &#x5B;
          &quot;multipart/form-data&quot;
        ],
        &quot;parameters&quot;: &#x5B;
          {
            &quot;in&quot;: &quot;formData&quot;,
            &quot;name&quot;: &quot;file&quot;,
            &quot;type&quot;: &quot;file&quot;
          }
        ],
        &quot;responses&quot;: {
          &quot;200&quot;: {
            &quot;description&quot;: &quot;Success&quot;
          }
        }
      }
    }
  }
}
</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-openapi-json-schema">Swagger 2.0 Upload multiple files  </h2>



<p></p>



<p>Below is <strong><em>OpenAPI </em></strong>V3.0 JSON schema definition for the image file,</p>



<p></p>



<p><strong>Example </strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2,13,14,15,16,17,18,19,20,21,22,23,24]; title: ; notranslate">
{
  &quot;swagger&quot;: &quot;2.0&quot;,
  &quot;info&quot;: {
    &quot;title&quot;: &quot;TheCodeBuzz&quot;,
    &quot;version&quot;: &quot;v1&quot;
  },
  &quot;paths&quot;: {
    &quot;/WeatherForecast/UploadFileMulti&quot;: {
      &quot;post&quot;: {
        &quot;tags&quot;: &#x5B;
          &quot;WeatherForecast&quot;
        ],
        &quot;consumes&quot;: &#x5B;
          &quot;multipart/form-data&quot;
        ],
        &quot;parameters&quot;: &#x5B;
          {
            &quot;in&quot;: &quot;formData&quot;,
            &quot;name&quot;: &quot;files&quot;,
            &quot;type&quot;: &quot;array&quot;,
            &quot;items&quot;: {
              &quot;format&quot;: &quot;binary&quot;,
              &quot;type&quot;: &quot;string&quot;
            }
          }
        ],
        &quot;responses&quot;: {
          &quot;200&quot;: {
            &quot;description&quot;: &quot;Success&quot;
          }
        }
      }
    }
  }
}
</pre></div>


<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-openapi-json-schema">OpenAPI 3.0 JSON schema &#8211; Upload/Post file</h2>



<p></p>



<p></p>



<p>As per  <strong>OpenAPI 3.0</strong>  specification files are defined as binary strings, that is<code>,</code></p>



<p></p>



<pre class="wp-block-code has-medium-font-size"><code>  "file": {
          "type": "string",
          "format": "binary",
          "nullable": true
         }</code></pre>



<p></p>



<p>We will cover how to upload single or multiple files with the example of swagger/open API JSON</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-openapi-3-0-swagger-upload-single-file">OpenAPI 3.0 swagger Upload single file</h2>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="555" src="https://www.thecodebuzz.com/wp-content/uploads/2022/08/how-to-post-files-in-swagger-openapi-1024x555.jpg" alt="how-to-post-files-in-swagger-openapi" class="wp-image-23031" srcset="https://thecodebuzz.com/wp-content/uploads/2022/08/how-to-post-files-in-swagger-openapi-1024x555.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/08/how-to-post-files-in-swagger-openapi-300x163.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/08/how-to-post-files-in-swagger-openapi-768x416.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/08/how-to-post-files-in-swagger-openapi-1536x833.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/08/how-to-post-files-in-swagger-openapi-2048x1111.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2022/08/how-to-post-files-in-swagger-openapi-785x426.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Below is <strong><em>OpenAPI </em></strong>V3.0 JSON schema definition for the single file,</p>



<p></p>



<p> <strong>Example </strong> </p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2,14,15,16,17,18,19,20,21,22,23,24,25]; title: ; notranslate">
{
  &quot;openapi&quot;: &quot;3.0.1&quot;,
  &quot;info&quot;: {
    &quot;title&quot;: &quot;TheCodeBuzz&quot;,
    &quot;version&quot;: &quot;v1&quot;
  },
  &quot;paths&quot;: {
    &quot;/WeatherForecast/UploadFile&quot;: {
      &quot;post&quot;: {
        &quot;tags&quot;: &#x5B;
          &quot;WeatherForecast&quot;
        ],
        &quot;requestBody&quot;: {
          &quot;content&quot;: {
            &quot;multipart/form-data&quot;: {
              &quot;schema&quot;: {
                &quot;type&quot;: &quot;object&quot;,
                &quot;properties&quot;: {
                  &quot;file&quot;: {
                    &quot;type&quot;: &quot;string&quot;,
                    &quot;format&quot;: &quot;binary&quot;,
                    &quot;nullable&quot;: true
                  }
                }
              },
              &quot;encoding&quot;: {
                &quot;file&quot;: {
                  &quot;style&quot;: &quot;form&quot;
                }
              }
            }
          }
        },
        &quot;responses&quot;: {
          &quot;200&quot;: {
            &quot;description&quot;: &quot;Success&quot;
          }
        }
      }
    }
  },
  &quot;components&quot;: { }
}
</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-openapi-3-0-swagger-upload-multiple-files">OpenAPI 3.0 swagger Upload multiple files </h2>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2022/08/How-to-post-files-in-Swagger-OpenAPI-1-1024x605.jpg" alt="" class="wp-image-23033"/></figure>



<p>As per  <strong>OpenAPI 3.0</strong>  specification files are defined as binary strings, that is<code>,</code> </p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">"files": {
            "type": "array",
            "items": 
             {
                "type": "string",
                "format": "binary"
             },
             "nullable": true
          }</pre>



<p></p>



<p>Below is <strong><em>OpenAPI </em></strong>V3.0 JSON schema definition for the file where swagger can let you upload multiple files at a time.</p>



<p></p>



<p> <strong>Example </strong> </p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]; title: ; notranslate">
{
  &quot;openapi&quot;: &quot;3.0.1&quot;,
  &quot;info&quot;: {
    &quot;title&quot;: &quot;TheCodeBuzz&quot;,
    &quot;version&quot;: &quot;v1&quot;
  },
  &quot;paths&quot;: {
    &quot;/WeatherForecast&quot;: {
      &quot;post&quot;: {
        &quot;tags&quot;: &#x5B;
          &quot;WeatherForecast&quot;
        ],
        &quot;requestBody&quot;: {
          &quot;content&quot;: {
            &quot;multipart/form-data&quot;: {
              &quot;schema&quot;: {
                &quot;type&quot;: &quot;object&quot;,
                &quot;properties&quot;: {
                  &quot;files&quot;: {
                    &quot;type&quot;: &quot;array&quot;,
                    &quot;items&quot;: {
                      &quot;type&quot;: &quot;string&quot;,
                      &quot;format&quot;: &quot;binary&quot;
                    },
                    &quot;nullable&quot;: true
                  }
                }
              },
              &quot;encoding&quot;: {
                &quot;files&quot;: {
                  &quot;style&quot;: &quot;form&quot;
                }
              }
            }
          }
        },
        &quot;responses&quot;: {
          &quot;200&quot;: {
            &quot;description&quot;: &quot;Success&quot;
          }
        }
      }
    }
  },
  &quot;components&quot;: { }
}
</pre></div>


<p></p>



<p><strong><em>References</em></strong>: </p>



<p></p>



<ul class="has-medium-font-size wp-block-list"><li><a href="https://www.thecodebuzz.com/oauth2-authentication-iswagger-open-api-asp-net-core-3-0/" target="_blank" rel="noopener" title="OAuth2 Authentication in OpenAPI  Swagger ASP.NET Core"><em>OAuth2 Authentication in OpenAPI  Swagger ASP.NET Core</em></a></li></ul>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/post-file-in-swagger-openapi-v3-0-and-v2-0-examples/">Post File in Swagger OpenAPI V3.0 and V2.0 examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/post-file-in-swagger-openapi-v3-0-and-v2-0-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How To Convert Swagger 2.0 To OpenAPI 3.1</title>
		<link>https://thecodebuzz.com/how-to-convert-swagger-2-0-to-openapi-3-1/</link>
					<comments>https://thecodebuzz.com/how-to-convert-swagger-2-0-to-openapi-3-1/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 18 Jul 2022 02:00:00 +0000</pubDate>
				<category><![CDATA[Swagger-OpenAPi]]></category>
		<category><![CDATA[How To Convert Swagger 2.0 To OpenAPI 3.1]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22955</guid>

					<description><![CDATA[<p>How To Convert Swagger 2.0 To OpenAPI 3.1 Today in this article, we will see a few easy-to-use techniques to Convert Swagger 2.0 To OpenAPI 3.1 We shall cover the below aspects of the article, We will generate Open-definition JSON using 2.0 schema and 3.0 schema specifications. Using Online Tooling for OpenAPI JSON Conversion https://editor.swagger.io/ [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/how-to-convert-swagger-2-0-to-openapi-3-1/">How To Convert Swagger 2.0 To OpenAPI 3.1</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">How To Convert Swagger 2.0 To OpenAPI 3.1</h1>



<figure class="wp-block-image size-full"><img decoding="async" width="278" height="209" src="https://www.thecodebuzz.com/wp-content/uploads/2022/08/Swagger-Convert-2.0-To-OpenAPI-3.0.jpg" alt="" class="wp-image-23034"/></figure>



<p>Today in this article, we will see a few easy-to-use techniques to Convert Swagger 2.0 To OpenAPI 3.1</p>



<p></p>



<p>We shall cover the below aspects of the article,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-using-online-tooling-for-openapi-json-conversion">Using Online Tooling for OpenAPI JSON Conversion</a><ul><li><a href="#aioseo-how-to-convert-swagger-2-0-to-openapi-3-1-using-swagger-editor">How To Convert Swagger 2.0 To OpenAPI 3.1 Using Swagger Editor</a></li></ul></li><li><a href="#aioseo-using-api---swagger-converter">Using API &#8211; Swagger Converter</a></li><li><a href="#aioseo-how-to-convert-swagger-3-1-to-2-0-in-asp-net-core">How to Convert Swagger 3.1 to 2.0 in ASP.NET Core</a></li></ul></div>



<p></p>



<p>We will generate Open-definition <strong><em>JSON </em></strong>using <strong>2.0 schema and 3.0 </strong>schema specifications.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-using-online-tooling-for-openapi-json-conversion">Using Online Tooling for OpenAPI JSON Conversion</h2>



<p></p>



<p></p>



<div data-schema-only="false" class="wp-block-aioseo-faq" id="aioseo-how-to-convert-swagger-2-0-to-openapi-3-1-using-swagger-editor"><h3 class="aioseo-faq-block-question">How To Convert Swagger 2.0 To OpenAPI 3.1 Using Swagger Editor</h3><div class="aioseo-faq-block-answer">
<p>We can use Swagger Converter or Swagger Editor which are online tools to convert Swagger specification</p>
</div></div>



<p></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><a href="https://editor.swagger.io/" target="_blank" rel="noopener"><strong>https://editor.swagger.io/</strong></a></p>
</blockquote>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="517" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-1024x517.jpg" alt="Convert Swagger 2.0 To OpenAPI 3.1" class="wp-image-22964" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-1024x517.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-300x151.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-768x388.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-1536x775.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-2048x1033.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-785x396.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>The above conversion will give you,</p>



<p></p>



<pre class="wp-block-preformatted">openapi: 3.0.1
info:
  title: Swagger Petstore
  description: 'This is a sample server Petstore server.  You can find out more about     Swagger
    at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).      For
    this sample, you can use the api key `special-key` to test the authorization     filters.'
  termsOfService: http://swagger.io/terms/
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
externalDocs:
  description: Find out more about Swagger
  url: http://swagger.io
servers:
- url: https://petstore.swagger.io/v2
- url: http://petstore.swagger.io/v2
tags:
- name: pet
  description: Everything about your Pets
  externalDocs:
    description: Find out more
    url: http://swagger.io
- name: store
  description: Access to Petstore o</pre>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-using-api---swagger-converter">Using API &#8211; Swagger Converter</h2>



<p></p>



<p> If you have access to the below link, </p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><a href="https://converter.swagger.io/" target="_blank" rel="noopener">https://converter.swagger.io/</a></p>
</blockquote>



<p></p>



<p>The above POST route can be executed via Swagger for POSTMAN or SOAP UI.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="591" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/Converts-a-1.x-or-2.x-Swagger-definition-to-the-OpenAPI-3.0.1-format-1024x591.jpg" alt="Convert Swagger 2.0 To OpenAPI 3.1" class="wp-image-22972" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/Converts-a-1.x-or-2.x-Swagger-definition-to-the-OpenAPI-3.0.1-format-1024x591.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/07/Converts-a-1.x-or-2.x-Swagger-definition-to-the-OpenAPI-3.0.1-format-300x173.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/07/Converts-a-1.x-or-2.x-Swagger-definition-to-the-OpenAPI-3.0.1-format-768x443.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/07/Converts-a-1.x-or-2.x-Swagger-definition-to-the-OpenAPI-3.0.1-format-1536x886.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/07/Converts-a-1.x-or-2.x-Swagger-definition-to-the-OpenAPI-3.0.1-format-785x453.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/07/Converts-a-1.x-or-2.x-Swagger-definition-to-the-OpenAPI-3.0.1-format.jpg 1570w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-how-to-convert-swagger-3-1-to-2-0-in-asp-net-core">How to Convert Swagger 3.1 to 2.0 in ASP.NET Core</h2>



<p></p>



<p>If you already have API based on .NET core 3.1 or above supporting OpenApi then it&#8217;s very easy to create an OpenAPI v2 definition using the below code,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="431" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-ASP.NET-Core-1024x431.jpg" alt="" class="wp-image-22973" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-ASP.NET-Core-1024x431.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-ASP.NET-Core-300x126.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-ASP.NET-Core-768x324.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-ASP.NET-Core-1536x647.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-ASP.NET-Core-785x331.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/07/How-To-Convert-OpenAPI-2.0-To-OpenAPI-3.0-ASP.NET-Core.jpg 1908w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Above using <strong><em>SerializeAsV2 </em></strong> set as true convert and generates the swagger.json for Swagger 2.0 definition.</p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/how-to-convert-swagger-2-0-to-openapi-3-1/">How To Convert Swagger 2.0 To OpenAPI 3.1</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/how-to-convert-swagger-2-0-to-openapi-3-1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Add a Header Parameter to .NET Core API in Swagger</title>
		<link>https://thecodebuzz.com/add-custom-header-parameter-net-core-api-swagger/</link>
					<comments>https://thecodebuzz.com/add-custom-header-parameter-net-core-api-swagger/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 04 Jun 2021 00:32:00 +0000</pubDate>
				<category><![CDATA[Swagger-OpenAPi]]></category>
		<category><![CDATA[.net core swagger add authorization header]]></category>
		<category><![CDATA[Add a Header parameter to .NET Core API in Swagger]]></category>
		<category><![CDATA[add custom request header in web api]]></category>
		<category><![CDATA[how to add custom header to asp.net core web api request]]></category>
		<category><![CDATA[how to configure swagger in asp.net core web api]]></category>
		<category><![CDATA[openapi add header to all requests]]></category>
		<category><![CDATA[swagger add header spring boot]]></category>
		<category><![CDATA[swagger add header to all requests]]></category>
		<category><![CDATA[swagger request headers example]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=5854</guid>

					<description><![CDATA[<p>Add a Custom header parameter to .NET Core API in Swagger &#8211;OpenAPI In this article, we will learn how to add a custom header parameter to .NET Core API in Swagger (OpenAPI) documentation. Today in this article, we will cover below aspects, Custom headers are helpful when you want to add metadata to your API [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/add-custom-header-parameter-net-core-api-swagger/">How to Add a Header Parameter to .NET Core API in Swagger</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Add a Custom header parameter to .NET Core API in Swagger</strong> &#8211;<strong>OpenAPI</strong></h1>


<div class="wp-block-image">
<figure class="aligncenter is-resized"><img loading="lazy" decoding="async" width="1347" height="801" src="https://i1.wp.com/thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-IOperationFilter.jpg?fit=785%2C467&amp;ssl=1" alt="Add header parameter to .NET Core API in Swagger" class="wp-image-5859" style="width:400px;height:238px" srcset="https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-IOperationFilter.jpg 1347w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-IOperationFilter-300x178.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-IOperationFilter-1024x609.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-IOperationFilter-768x457.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-IOperationFilter-785x467.jpg 785w" sizes="auto, (max-width: 1347px) 100vw, 1347px" /></figure>
</div>


<p>In this article, we will learn how to add a custom header parameter to .NET Core API in Swagger (OpenAPI) documentation.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-create-asp-net-core-api">Step1 &#8211; Create ASP.NET Core API</a></li><li><a href="#aioseo-step2-install-the-required-nuget-package">Step2: Install the required NuGet package</a></li><li><a href="#aioseo-approach1-using-ioperationfilter-to-add-a-header-to-swagger">Approach1 &#8211; Using  IOperationFilter to add a header to swagger</a></li><li><a href="#aioseo-approach2-using-fromheaderattribute-to-add-a-header-to-swagger">Approach2 &#8211; Using  FromHeaderAttribute to add a header to swagger</a></li></ul></div>



<p></p>



<p>Custom headers are helpful when you want to add metadata to your API requests or responses or when you need specific headers for authentication, security, or other purposes. </p>



<p></p>



<p>Documenting custom headers in Swagger ensures that developers consuming your API understand the header requirements and can use the API effectively.</p>



<p></p>



<p>In Swagger/OpenAPI, you can document and define custom headers to be used in your API requests and responses.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-create-asp-net-core-api">Step1 &#8211; Create ASP.NET Core API </h2>



<p></p>



<p>Create .NET API application</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-step2-install-the-required-nuget-package">Step2: Install the required NuGet package</h2>



<p></p>



<p>Using CLI tool</p>



<p></p>



<pre class="wp-block-preformatted">dotnet add package Swashbuckle.AspNetCore</pre>



<p></p>



<p>OR</p>



<p></p>



<p>Using <strong>PMC</strong>(package manager console)</p>



<p></p>



<pre class="wp-block-preformatted">PM&gt; Install-Package <a href="https://www.nuget.org/packages/Swashbuckle.AspNetCore" target="_blank" rel="noopener" title="">Swashbuckle.AspNetCore</a> -Version &lt;version&gt;

</pre>



<h2 class="wp-block-heading" id="aioseo-approach1-using-ioperationfilter-to-add-a-header-to-swagger">Approach1 &#8211; Using  <em><em>IOperationFilter</em></em> to add a header to swagger  </h2>



<p></p>



<p>Create a custom class with the name of your choice &#8216;<strong><em>CustomHeaderSwaggerAttribute</em></strong>&#8216; derived from &#8216;<em><strong>IOperationFilter</strong></em>&#8216; and overriding Apply method as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [1,9,10,11,12,13,14,15,16,17,18]; title: ; notranslate">
  public class CustomHeaderSwaggerAttribute : IOperationFilter
    {
       
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List&lt;OpenApiParameter&gt;();

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = &quot;X-CustomHeader&quot;,
                In = ParameterLocation.Header,
                Required = true,
                Schema = new OpenApiSchema
                {
                    Type = &quot;string&quot; 
                }
            });
        }

    }
</pre></div>


<p></p>



<p>The above logic will be applied globally to all APIs. You can control the same based on each API if required using attribute filter techniques.</p>



<p></p>



<p>Let&#8217;s enable this custom Operation filter in the <em><strong>ConfigureServices </strong></em>method as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [7]; title: ; notranslate">
 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =&gt;
            {
                c.SwaggerDoc(&quot;v1&quot;, new OpenApiInfo { Title =&quot;MyTestService&quot;, Version = &quot;v1&quot;, });
                c.OperationFilter&lt;CustomHeaderSwaggerAttribute&gt;();

                var xmlFile = $&quot;{Assembly.GetExecutingAssembly().GetName().Name}.xml&quot;;
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);

            });
        }
</pre></div>


<p> </p>



<p>Please see the swagger OpenAPI documentation below, </p>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="1533" height="924" src="https://i2.wp.com/thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger.jpg?fit=785%2C473&amp;ssl=1" alt="Add a Custom header parameter to .NET Core API in Swagger" class="wp-image-5857" srcset="https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger.jpg 1533w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-300x181.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-1024x617.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-768x463.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/10/Add-a-Header-parameter-to-.NET-Core-API-in-Swagger-785x473.jpg 785w" sizes="auto, (max-width: 1533px) 100vw, 1533px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-approach2-using-fromheaderattribute-to-add-a-header-to-swagger">Approach2 &#8211; Using  <em>FromHeaderAttribute</em> to add a header to swagger </h2>



<p></p>



<p>One can also use a <strong><em>declarative </em></strong>approach by using <strong><em>FromHeaderAttribute</em> </strong>using <strong>FromHeader</strong>,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2]; title: ; notranslate">
        &#x5B;HttpPost]
        public IActionResult Weather(&#x5B;FromHeader(Name = &quot;x-CustomHeader2&quot;)] string city)
        {
            return Ok();
        }
</pre></div>


<p></p>



<p>Let&#8217;s view generated swagger,</p>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="1653" height="924" src="https://i1.wp.com/thecodebuzz.com/wp-content/uploads/2019/10/Swagger-FromHeader-Attribute-Add-a-Header-parameter-to-.NET-Core-API.jpg?fit=785%2C438&amp;ssl=1" alt="Add a Custom header parameter to .NET Core API in Swagger" class="wp-image-5866" srcset="https://thecodebuzz.com/wp-content/uploads/2019/10/Swagger-FromHeader-Attribute-Add-a-Header-parameter-to-.NET-Core-API.jpg 1653w, https://thecodebuzz.com/wp-content/uploads/2019/10/Swagger-FromHeader-Attribute-Add-a-Header-parameter-to-.NET-Core-API-300x168.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/10/Swagger-FromHeader-Attribute-Add-a-Header-parameter-to-.NET-Core-API-1024x572.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/10/Swagger-FromHeader-Attribute-Add-a-Header-parameter-to-.NET-Core-API-768x429.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/10/Swagger-FromHeader-Attribute-Add-a-Header-parameter-to-.NET-Core-API-1536x859.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2019/10/Swagger-FromHeader-Attribute-Add-a-Header-parameter-to-.NET-Core-API-785x439.jpg 785w" sizes="auto, (max-width: 1653px) 100vw, 1653px" /></figure>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<p><em><strong>References</strong>:</em></p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/swagger-api-documentation-in-net-core-3-0/" target="_blank" rel="noreferrer noopener"><strong><em>Swagger API Documentation in .NET Core 3.1 or .NET 5 </em></strong></a></li>
</ul>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/add-custom-header-parameter-net-core-api-swagger/" title="Add a Header parameter to .NET Core API in Swagger" target="_blank" rel="noreferrer noopener"><strong><em>Add a Header parameter to .NET Core API in S</em>wagger v2.0 specification</strong></a></li>
</ul>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p><p>The post <a href="https://thecodebuzz.com/add-custom-header-parameter-net-core-api-swagger/">How to Add a Header Parameter to .NET Core API in Swagger</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/add-custom-header-parameter-net-core-api-swagger/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
		<item>
		<title>Upload Files in Swagger UI IOperationFilter</title>
		<link>https://thecodebuzz.com/upload-files-in-swagger-ui-ioperationfilter/</link>
					<comments>https://thecodebuzz.com/upload-files-in-swagger-ui-ioperationfilter/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 01 May 2021 16:27:00 +0000</pubDate>
				<category><![CDATA[API Best Practices]]></category>
		<category><![CDATA[Swagger-OpenAPi]]></category>
		<category><![CDATA[file upload in swagger]]></category>
		<category><![CDATA[file upload swagger]]></category>
		<category><![CDATA[openapi file upload]]></category>
		<category><![CDATA[swagger 3.0 file upload example]]></category>
		<category><![CDATA[swagger file upload]]></category>
		<category><![CDATA[swagger file upload annotation]]></category>
		<category><![CDATA[swagger file upload example]]></category>
		<category><![CDATA[swagger file upload example c]]></category>
		<category><![CDATA[swagger file upload nestjs]]></category>
		<category><![CDATA[swagger image upload]]></category>
		<category><![CDATA[swagger multipart file upload example]]></category>
		<category><![CDATA[swagger post file]]></category>
		<category><![CDATA[swagger send file]]></category>
		<category><![CDATA[swagger ui file upload]]></category>
		<category><![CDATA[swagger ui file upload example]]></category>
		<category><![CDATA[swagger ui file upload not working]]></category>
		<category><![CDATA[swagger ui upload file]]></category>
		<category><![CDATA[swagger ui upload file spring boot]]></category>
		<category><![CDATA[swagger upload]]></category>
		<category><![CDATA[swagger upload file]]></category>
		<category><![CDATA[swagger upload file example]]></category>
		<category><![CDATA[swagger-ui multiple files]]></category>
		<category><![CDATA[upload file swagger]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=15962</guid>

					<description><![CDATA[<p>Upload Files in Swagger UI using OperationFilter Today in this article, we shall show how to perform upload Files in Swagger UI using OpenAPI Specification V3.0. We already looked at the approach of using IFormFile inbuilt support for Upload. Today we will see how to handle the file Upload using the IOperationFilter interface as well. [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/upload-files-in-swagger-ui-ioperationfilter/">Upload Files in Swagger UI IOperationFilter</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Upload Files in Swagger UI using OperationFilter</strong></h1>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="278" height="209" src="https://www.thecodebuzz.com/wp-content/uploads/2021/05/How-to-upload-upload-multiple-files-in-swagger-UI-IOperationFilter.jpg" alt="swagger ui upload file, Upload Files in Swagger UI" class="wp-image-15963" style="width:399px;height:auto"/></figure>



<p>Today in this article, we shall show how to perform upload Files in Swagger UI using <strong><em>OpenAPI Specification V3</em>.0. </strong></p>



<p></p>



<p>We already looked at the approach of using <strong><em>IFormFile </em></strong>inbuilt support for Upload. </p>



<p></p>



<p>Today we will see how to handle the file Upload using the <strong><em>IOperationFilter</em></strong> interface as well.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-prerequisites">Prerequisites</a></li><li><a href="#aioseo-ioperationfilter-for-swagger-upload-file">IOperationFilter for Swagger upload file</a></li></ul></div>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>



<p><strong>Swagger&nbsp;</strong>or&nbsp;<strong>Open API specification</strong>&nbsp;provides the advantage of understanding the REST services easily (especially if developers are consuming any new API ) plus helps provide easy documentation and details of capabilities given Service/API owns.</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading" id="aioseo-prerequisites">Prerequisites </h2>



<p></p>



<p>Before we see <strong><em>IOperationFilter </em></strong>usage, I recommend you to visit the below article to enable Swagger API definition in simple 2-3 steps,</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/swagger-api-documentation-in-net-core-3-0/" target="_blank" rel="noreferrer noopener"><em><strong>Add Swagger API Documentation in ASP.NET Core</strong></em></a></li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-ioperationfilter-for-swagger-upload-file">IOperationFilter for Swagger upload file</h2>



<p></p>



<p><strong><em><a href="https://www.thecodebuzz.com/ioperationfilter-idocumentfilter-in-asp-net-core/" target="_blank" rel="noopener" title="IOperationFilter and IDocumentFilter in ASP.NET Core">IOperationFilter </a></em></strong>is a very useful interface and provides better control on how you want to define or customize the swagger UI behavior.</p>



<p></p>



<p>In <a href="https://swagger.io/docs/specification/describing-request-body/file-upload/" target="_blank" rel="noreferrer noopener"><strong><em>OpenAPI 3.0 </em></strong>schema</a> representation can be depicted as below,</p>



<p></p>



<p><strong><em>For images/png,</em></strong></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2,3]; title: ; notranslate">
requestBody:
  content:
    image/png:
      schema:
        type: string
        format: binary
</pre></div>


<p></p>



<ul class="wp-block-list">
<li><code>requestBody</code>&nbsp;&#8211; The schema represents this keyword for request payload containing a file. </li>
</ul>



<ul class="wp-block-list">
<li><code>content</code> &#8211; specify the request media type. Example &nbsp;<code>image/png</code>&nbsp;or&nbsp;<code>application/octet-stream</code>. </li>
</ul>



<ul class="wp-block-list">
<li>schema &#8211; For file Files use a&nbsp;<code>type as string</code>&nbsp;schema and <code>format as binary or base64</code>.</li>
</ul>



<p></p>



<p>Please implement the interface as below by overriding the <em><strong>Apply() </strong></em>method supporting <strong><em>OpenAPI </em></strong>specs.</p>



<p></p>



<p> Let&#8217;s define the code per the above specification,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
public class FileOperationFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            operation.RequestBody = new OpenApiRequestBody
            {
                Description = &quot;upload file &quot;,
                Content = new Dictionary&lt;String, OpenApiMediaType&gt;
                {
                    {
                        &quot;multipart/form-data&quot;, new OpenApiMediaType
                        {
                            Schema = new OpenApiSchema
                            {
                                Type = &quot;object&quot;,
                                Required = new HashSet&lt;String&gt;{ &quot;file&quot; },
                                Properties = new Dictionary&lt;String, OpenApiSchema&gt;
                                {
                                    {
                                        &quot;file&quot;, new OpenApiSchema()
                                        {
                                            Type = &quot;string&quot;,
                                            Format = &quot;binary&quot;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

        }
    }
}
</pre></div>


<p></p>



<p><strong>For all other file types</strong> &#8211; Use Multipart requests</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2,3]; title: ; notranslate">
requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                orderId:
                  type: integer
                userId:
                  type: integer
                fileName:
                  type: string
                  format: binary
</pre></div>


<p></p>



<p>The next steps are to register the above Filter interface within the API pipeline as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
       
          services.AddSwaggerGen(c =&gt;
            {
                c.SwaggerDoc(&quot;v1&quot;, new OpenApiInfo { Title = &quot;TheCodeBuzz&quot;, Version = &quot;v1&quot; });
               
                c.ResolveConflictingActions(x =&gt; x.First());
                c.OperationFilter&lt;FileOperationFilter&gt;();
            });
</pre></div>


<p></p>



<p></p>



<p>Now choose file button will be visible on Swagger UI for performing Swagger UI file upload</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="696" src="https://www.thecodebuzz.com/wp-content/uploads/2021/05/IOperationFilter-How-to-upload-files-via-Swagger-OpenAPI-1-1024x696.jpg" alt="swagger file upload" class="wp-image-15969" style="width:527px;height:358px" srcset="https://thecodebuzz.com/wp-content/uploads/2021/05/IOperationFilter-How-to-upload-files-via-Swagger-OpenAPI-1-1024x696.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/05/IOperationFilter-How-to-upload-files-via-Swagger-OpenAPI-1-300x204.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/05/IOperationFilter-How-to-upload-files-via-Swagger-OpenAPI-1-768x522.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/05/IOperationFilter-How-to-upload-files-via-Swagger-OpenAPI-1-1536x1044.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/05/IOperationFilter-How-to-upload-files-via-Swagger-OpenAPI-1-765x520.jpg 765w, https://thecodebuzz.com/wp-content/uploads/2021/05/IOperationFilter-How-to-upload-files-via-Swagger-OpenAPI-1.jpg 1766w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Below is a simple <strong><em>API</em></strong> method. We are not using <strong><em>IFormFile </em></strong>but still, we can add the Upload file support to the Swagger definition </p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
        &#x5B;HttpPost]
        public Task UploadFile()
        {
            return Task.CompletedTask;
        }
</pre></div>


<p></p>



<p></p>



<p class="has-medium-font-size">Do you have any <strong><em>comments</em> </strong>or <em><strong>ideas </strong></em>or any <em>better suggestions </em>to share?</p>



<p></p>



<p class="has-medium-font-size">Please sound off your comments below.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/upload-files-in-swagger-ui-ioperationfilter/">Upload Files in Swagger UI IOperationFilter</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/upload-files-in-swagger-ui-ioperationfilter/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
