【问题标题】:Multiple array definitions for single array in Azure API-managementAzure API 管理中单个数组的多个数组定义
【发布时间】:2019-09-05 14:00:11
【问题描述】:

我创建了一个返回数组的项目。查看 swagger.json 一切看起来都很好,导入这个 swagger.json 。但是,当我从开发人员门户下载 API 定义时,我会看到一些对象,例如 ...Array、...Array-1、...Array-2。我没想到:

如何防止这种情况发生?如何确保生成的行为与我的普通对象相同(所以没有破折号,而是点)。我创建了一个示例项目来重现我的问题:https://github.com/mvdiemen/SwaggerArrayGenerationExample

这是否与此处描述的 Azure API 管理更改有关? :https://blog.tomkerkhove.be/2018/04/13/changes-to-azure-api-management-openapi/

【问题讨论】:

    标签: arrays azure asp.net-core swagger azure-api-management


    【解决方案1】:

    APIM 不支持内联模式。尝试仅通过 $ref 为响应/请求指定模式,即使它是您已经定义的对象数组 - 定义一个数组类型的新对象并引用它。

    所以,不要像这样:

    "200": {
        "description": "Success",
        "schema": {
            "uniqueItems": false,
            "type": "array",
            "items": {
                "$ref": "#/definitions/SwaggerGenerationSample.Models.Response.Employee"
            }
        }
    }
    

    在定义中定义数组本身并引用它:

    "200": {
        "description": "Success",
        "schema": {
            "$ref": "#/definitions/EmployeeArray"
        }
    }
    ...
    "definitions": {
        "EmployeeArray": {
            "uniqueItems": false,
            "type": "array",
            "items": {
                "$ref": "#/definitions/SwaggerGenerationSample.Models.Response.Employee"
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答。目前我们的模式如下所示:gist.githubusercontent.com/mvdiemen/…。你指的是这个吗?你有例子吗?
    • 更新回复
    • 感谢在 APIM 中导入它使其工作。我们正在使用 SwashBuckle 从我们在 asp.net 核心中的模型生成我们的 swagger.json。如何将其与 SwashBuckle 结合使用?
    • 很遗憾,我不太确定。
    猜你喜欢
    • 2021-01-16
    • 2021-05-24
    • 2017-11-02
    • 2021-05-21
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多