Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created August 13, 2021 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/32474da3f9e512b6ab4e34192fbbb4b8 to your computer and use it in GitHub Desktop.
Save justinyoo/32474da3f9e512b6ab4e34192fbbb4b8 to your computer and use it in GitHub Desktop.
Azure Functions OpenAPI on .NET 5
public static void Main()
{
var host = new HostBuilder()
// πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡ Remove this line below πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡
.ConfigureFunctionsWorkerDefaults()
// πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘† Remove this line above πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘†
.Build();
host.Run();
}
public static void Main()
{
var host = new HostBuilder()
// πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡ Add these lines below πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
.ConfigureOpenApi()
// πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘† Add these lines above πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘†
.Build();
host.Run();
}
// πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡ Add OpenAPI related decorators below πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡
[OpenApiOperation(operationId: "greeting", tags: new[] { "greeting" }, Summary = "Greetings", Description = "This shows a welcome message.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Summary = "The response", Description = "This returns the response")]
// πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘† Add OpenAPI related decorators above πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘†
[Function("Function1")]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
FunctionContext executionContext)
{
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment