我们有一些通过API管理暴露的Azure函数?API管理可以自动公开A/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]?-version = 2021-01-01-Preview
只需在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-expose-openapi-documentation-on-azure-api-management.html