Introducing Swagger UI on Azure Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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": "" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
{ | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In https://gist.github.com/justinyoo/002920c00bfbe31e427d4de4a914f58e#file-sample-function-cs-L6
Should it be typeof instead of typeo?
In https://gist.github.com/justinyoo/002920c00bfbe31e427d4de4a914f58e#file-sample-function-cs-L5
There's a comma missing
Required = false <,> Type = typeof(int)