繁体   English   中英

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

[英]Can Azure Api Management expose OpenAPI documentation?

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

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

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

您可以通过 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>

更多信息可以在https://www.devprotocol.com/2021/07/20/expose-openapi-documentation-on-azure-api-management.html上找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM