Azure Api 管理可以公开 OpenAPI 文档吗?

Mat*_*und 12 azure azure-api-management openapi

我们有一些通过 Api 管理公开的 Azure Functions?Api Management 是否可以自动公开 /swagger 端点,就像 Swashbuckle 包对 Asp.Net 中的 api 所做的那样。

Md *_*ron 7

Azure API 管理无法自动生成 swagger 页面。Azure API管理只能为您提供API定义文件。然后您可以使用其他工具(例如Swagger UI)配合定义文件来生成您需要的页面。

此外,Azure API管理为您提供了UI(https://youapimanagementname.portal.azure-api.net)来告诉您如何使用所有API。


jto*_*ain 5

您可以通过 API 本身公开您的 openapi 文档。API 文档可在以下位置索取:

https://management.azure.com/subscriptions/[subscriptionid]/resourceGroups/[resourcegroupname]/Microsoft.ApiManagement/service/[servicename]/apis/[apiid]?export=true&format=openapi&api-version=2021-01- 01-预览

只需在 API 上创建一个附加操作(例如 openapi.yaml),通过自定义策略调用上面的 url 并返回结果。您可以使用以下政策

<policies>
    <inbound>
        <base />
        <send-request mode="new" response-variable-name="result" timeout="300" ignore-error="false">
            <set-url>@("https://management.azure.com/subscriptions/{{azure-subscriptionid}}/resourceGroups/{{azure-resourcegroup}}/providers/Microsoft.ApiManagement/service/" + context.Deployment.ServiceName + "/apis/" + context.Api.Id + "?export=true&format=openapi&api-version=2021-01-01-preview")</set-url>
            <set-method>GET</set-method>
            <authentication-managed-identity resource="https://management.azure.com/" />
        </send-request>
        <return-response>
            <set-status code="200" reason="OK" />
            <set-header name="Content-Type" exists-action="override">
                <value>application/yaml</value>
            </set-header>
            <set-body>@((string)(((IResponse)context.Variables["result"]).Body.As<JObject>()["value"]))</set-body>
        </return-response>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Run Code Online (Sandbox Code Playgroud)

更多信息请访问https://www.devprotocol.com/2021/07/20/expose-openapi-documentation-on-azure-api-management.html