首页 > 解决方案 > OpenAPI 类型模式写入 Swagger 类型字段

问题描述

我的问题是关于 Azure API Management, resource type 中的 API 架构Microsoft.ApiManagement/service/apis/schemas

通过 Azure 门户创建的架构使用内容类型创建application/vnd.oai.openapi.components+json并写入document/components/schemas,这是 OpenAPI 定义中架构定义的正确路径。

样本:

{
    "value": [
        {
            "id": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.ApiManagement/service/xxx/apis/xxx/schemas/1629566051926",
            "type": "Microsoft.ApiManagement/service/apis/schemas",
            "name": "1629566051926",
            "properties": {
                "contentType": "application/vnd.oai.openapi.components+json",
                "document": {
                    "components": {
                        "schemas": {
                            "patch-components-id-request-1": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "description": {
                                        "type": "string"
                                    },
                                    "documentationURL": {
                                        "type": "string"
                                    },
                                    "iacURL": {
                                        "type": "string"
                                    },
                                    "duration": {
                                        "type": "integer"
                                    },
                                    "statusID": {
                                        "type": "integer"
                                    },
                                    "owner": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ],
    "count": 1
}

通过REST APIGo SDK创建的模式是使用 设置的properties.document.definitions,这导致它被写入document/definitions,无论 contentType 是什么。

样本:

{
    "value": [
        {
            "id": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.ApiManagement/service/xxx/apis/marble-dev-fctn/schemas/marbleschemas",
            "type": "Microsoft.ApiManagement/service/apis/schemas",
            "name": "marbleschemas",
            "properties": {
                "contentType": "application/vnd.oai.openapi.components+json",
                "document": {
                    "definitions": {
                        "patch-components-id-request-1": {
                            "type": "object",
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "description": {
                                    "type": "string"
                                },
                                "documentationURL": {
                                    "type": "string"
                                },
                                "iacURL": {
                                    "type": "string"
                                },
                                "duration": {
                                    "type": "integer"
                                },
                                "statusID": {
                                    "type": "integer"
                                },
                                "owner": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        }
    ],
    "count": 1
}

将 contentType 设置application/vnd.ms-azure-apim.swagger.definitions+json为此很好,但是将 contentType 设置application/vnd.oai.openapi.components+json为时会出现两个问题: 导出 OpenAPI Schema 时将不包含定义(至少从 Azure 门户导出时,我没有尝试任何其他方式),因为definitions不是有效的 OpenAPI 字段,并且不会显示在 Developer Portal 中。

据我了解,需要将定义写入document/components/schemasforapplication/vnd.oai.openapi.components+jsondocument/definitionsfor application/vnd.ms-azure-apim.swagger.definitions+json

我可以通过在 ARM 模板或 REST API 调用中手动设置来将定义导入正确的路径,但我正在努力让 terraform 资源正常工作,这依赖于 Go SDK。可能也有解决方法,但我真的很想知道我的理解是否错误,或者这里是否存在问题。

标签: azuregoterraformazure-api-management

解决方案


推荐阅读