Skip to content

Latest commit

 

History

History
185 lines (143 loc) · 8.82 KB

openapi-in-proc.md

File metadata and controls

185 lines (143 loc) · 8.82 KB

Microsoft.Azure.WebJobs.Extensions.OpenApi

Build and Test

This enables Azure Functions to render OpenAPI document and Swagger UI. The more details around the Swagger UI on Azure Functions can be found on this blog post.

Note

This extension supports both OpenAPI 2.0 (aka Swagger) and OpenAPI 3.0.1 spec.

Looking for Isolated Worker Support?

Please find this document, Microsoft.Azure.Functions.Worker.Extensions.OpenApi.

Acknowledgement

Issues?

While using this library, if you find any issue, please raise a ticket on the Issue page.

Getting Started

For detailed getting started document, find this Enable OpenAPI Endpoints on Azure Functions – In-Process Model page.

Advanced Configuration in General

If you look for the advanced configurations in general, please find the document, Advanced Configurations for OpenAPI Extension

Injecting OpenApiConfigurationOptions during Startup

You may want to inject the OpenApiConfigurationOptions instance during startup, through the Startup.cs class. Here's the example:

[assembly: FunctionsStartup(typeof(MyFunctionApp.Startup))]
namespace MyFunctionApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            /* ⬇️⬇️⬇️ Add this ⬇️⬇️⬇️ */
            builder.Services.AddSingleton<IOpenApiConfigurationOptions>(_ =>
                            {
                                var options = new OpenApiConfigurationOptions()
                                {
                                    Info = new OpenApiInfo()
                                    {
                                        Version = "1.0.0",
                                        Title = "Swagger Petstore",
                                        Description = "This is a sample server Petstore API designed by [http://swagger.io](http://swagger.io).",
                                        TermsOfService = new Uri("https://github.com/Azure/azure-functions-openapi-extension"),
                                        Contact = new OpenApiContact()
                                        {
                                            Name = "Enquiry",
                                            Email = "[email protected]",
                                            Url = new Uri("https://github.com/Azure/azure-functions-openapi-extension/issues"),
                                        },
                                        License = new OpenApiLicense()
                                        {
                                            Name = "MIT",
                                            Url = new Uri("http://opensource.org/licenses/MIT"),
                                        }
                                    },
                                    Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
                                    OpenApiVersion = OpenApiVersionType.V2,
                                    IncludeRequestingHostName = true,
                                    ForceHttps = false,
                                    ForceHttp = false,
                                };

                                return options;
                            });
            /* ⬆️⬆️⬆️ Add this ⬆️⬆️⬆️ */
        }
    }
}

Injecting OpenApiCustomUIOptions during Startup

You may want to inject the OpenApiCustomUIOptions instance during startup, through the Startup.cs class. Here's the example:

[assembly: FunctionsStartup(typeof(MyFunctionApp.Startup))]
namespace MyFunctionApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            /* ⬇️⬇️⬇️ Add this ⬇️⬇️⬇️ */
            builder.Services.AddSingleton<IOpenApiCustomUIOptions>(_ =>
                            {
                                var assembly = Assembly.GetExecutingAssembly();
                                var options = new OpenApiCustomUIOptions(assembly)
                                {
                                    CustomStylesheetPath = "https://contoso.com/dist/my-custom.css",
                                    CustomJavaScriptPath = "https://contoso.com/dist/my-custom.js",

                                    // ⬇️⬇️⬇️ Add your logic to get your custom stylesheet ⬇️⬇️⬇️
                                    // GetStylesheet = () =>
                                    // {
                                    //     var result = string.Empty;

                                    //     //
                                    //     // CUSTOM LOGIC TO GET STYLESHEET
                                    //     //

                                    //     return Task.FromResult(result);
                                    // },
                                    // ⬆️⬆️⬆️ Add your logic to get your custom stylesheet ⬆️⬆️⬆️

                                    // ⬇️⬇️⬇️ Add your logic to get your custom JavaScript ⬇️⬇️⬇️
                                    // GetJavaScript = () =>
                                    // {
                                    //     var result = string.Empty;

                                    //     //
                                    //     // CUSTOM LOGIC TO GET JAVASCRIPT
                                    //     //

                                    //     return Task.FromResult(result);
                                    // },
                                    // ⬆️⬆️⬆️ Add your logic to get your custom JavaScript ⬆️⬆️⬆️
                                };

                                return options;
                            });
            /* ⬆️⬆️⬆️ Add this ⬆️⬆️⬆️ */
        }
    }
}

Injecting OpenApiHttpTriggerAuthorization during Startup

You may want to inject the OpenApiHttpTriggerAuthorization instance during startup, through the Startup.cs class. Here's the example:

[assembly: FunctionsStartup(typeof(MyFunctionApp.Startup))]
namespace MyFunctionApp
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            /* ⬇️⬇️⬇️ Add this ⬇️⬇️⬇️ */
            builder.Services.AddSingleton<IOpenApiHttpTriggerAuthorization>(_ =>
                            {
                                var auth = new OpenApiHttpTriggerAuthorization(async req =>
                                {
                                    var result = default(OpenApiAuthorizationResult);

                                    var authtoken = (string)req.Headers["Authorization"];
                                    if (authtoken.IsNullOrWhiteSpace())
                                    {
                                        result = new OpenApiAuthorizationResult()
                                        {
                                            StatusCode = HttpStatusCode.Unauthorized,
                                            ContentType = "text/plain",
                                            Payload = "Unauthorized",
                                        };

                                        return await Task.FromResult(result).ConfigureAwait(false);
                                    }

                                    return await Task.FromResult(result).ConfigureAwait(false);
                                });

                                return auth;
                            });
            /* ⬆️⬆️⬆️ Add this ⬆️⬆️⬆️ */
        }
    }
}