Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Last active May 9, 2022 08:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/002920c00bfbe31e427d4de4a914f58e to your computer and use it in GitHub Desktop.
Save justinyoo/002920c00bfbe31e427d4de4a914f58e to your computer and use it in GitHub Desktop.
Introducing Swagger UI on Azure Functions
{
"IsEncrypted": false,
"Values": {
...
"OpenApi__Info__Version": "2.0.0",
"OpenApi__Info__Title": "Open API Sample on Azure Functions",
"OpenApi__Info__Description": "A sample API that runs on Azure Functions either 1.x or 2.x using Open API specification.",
"OpenApi__Info__TermsOfService": "https://github.com/aliencube/AzureFunctions.Extensions",
"OpenApi__Info__Contact__Name": "Aliencube Community",
"OpenApi__Info__Contact__Email": "no-reply@aliencube.org",
"OpenApi__Info__Contact__Url": "https://github.com/aliencube/AzureFunctions.Extensions/issues",
"OpenApi__Info__License__Name": "MIT",
"OpenApi__Info__License__Url": "http://opensource.org/licenses/MIT",
"OpenApi__ApiKey": ""
}
}
[FunctionName(nameof(RenderOpenApiDocument))]
[OpenApiIgnore]
public static async Task<IActionResult> RenderOpenApiDocument(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "openapi/{version}.{extension}")] HttpRequest req,
string version,
string extension,
ILogger log)
{
var ver = GetSpecVersion(version);
var ext = GetExtension(extension);
var settings = new AppSettings();
var helper = new DocumentHelper();
var document = new Document(helper);
var result = await document.InitialiseDocument()
.AddMetadata(settings.OpenApiInfo)
.AddServer(req, settings.HttpSettings.RoutePrefix)
.Build(Assembly.GetExecutingAssembly())
.RenderAsync(ver, ext)
.ConfigureAwait(false);
var response = new ContentResult()
{
Content = result,
ContentType = "application/json",
StatusCode = (int)HttpStatusCode.OK
};
return response;
}
[FunctionName(nameof(RenderSwaggerUI))]
[OpenApiIgnore]
public static async Task<IActionResult> RenderSwaggerUI(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "swagger/ui")] HttpRequest req,
ILogger log)
{
var settings = new AppSettings();
var ui = new SwaggerUI();
var result = await ui.AddMetadata(settings.OpenApiInfo)
.AddServer(req, settings.HttpSettings.RoutePrefix)
.BuildAsync(typeof(SwaggerUI).Assembly)
.RenderAsync("swagger.json", settings.SwaggerAuthKey)
.ConfigureAwait(false);
var response = new ContentResult()
{
Content = result,
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK
};
return response;
}
// GET Method
[FunctionName(nameof(GetSample))]
[OpenApiOperation("list", "sample")]
[OpenApiParameter("name", In = ParameterLocation.Query, Required = true,Type = typeof(string))]
[OpenApiParameter("limit", In = ParameterLocation.Query, Required = false Type = typeof(int))]
[OpenApiResponseBody(HttpStatusCode.OK, "application/json", typeo(SampleResponseModel))]
public static async Task<IActionResult> GetSample(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "samples")] HttpRequest req,
ILogger log)
{
...
}
// POST Method
[FunctionName(nameof(PostSample))]
[OpenApiOperation("add", "sample")]
[OpenApiRequestBody("application/json", typeof(SampleRequestModel))]
[OpenApiResponseBody(HttpStatusCode.OK, "application/json", typeo(SampleResponseModel))]
public static async Task<IActionResult> PostSample(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "samples")] HttpRequest req,
ILogger log)
{
...
}
@fraxedas
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment