I will explain an easy way to provide Open API and Swagger UI in Azure Functions v2.
- You install
AzureFunctions.Extensions.Swashbuckle
package with nuget.
2. You add startup code on your functions project.
3. You add Open API function and Swagger UI function.
You start DEBUG and access http://localhost:7071/api/swagger/ui on your browser.
Please add this API code to the project to try it out.
And start DEBUG and access swagger ui.
Next, The following describes the available Attributes.
ProducesResponseTypeAttribute
If you want to set API result per Http Status, you use to ProducesResponseTypeAttribute.
ProducesResponseTypeAttirbute can multiple set.
RequestBodyType
If you want to set RequestBody, you use to RequestBodyTypeAttribute.
RequestBodyTypeAttribute must set in HttpRequest or HttpRequestMessage paramater.
RequestModelClass’s property can set data annotation.
RequestHttpHeaderAttribute
If you want to set Http header, you use to RequestHttpHeaderAttribute.
RequestHttpHeaderAttribute can set multiple, and RequestHttpHeaderAttribute can set Class or Method.
SwaggerIgnoreAttribute
This package collect all HttpTrigger functions and add in Open API.
If you have Http Trigger Functions that you do not want to add to Open Api, use SwaggerIgnoreAttribute.
Finally, I will introduce how to import XmlCommentDocument and add it to Open API.
If you want to add XML comments in the program code to the OpenAPI Description, do the next:
- Add this xml in to functions project’s csproj file.
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>SampleFunction.xml</DocumentationFile>
</PropertyGroup>
2. Change host.json. You add extensions setting.
{
"version": "2.0",
"extensions": {
"Swashbuckle": {
"XmlPath": "SampleFunction.xml"
}
}
}
If you find bug, I want you create issue in this repository.