gpt4 book ai didi

dictionary - Swagger:<字符串,对象> 的映射

转载 作者:行者123 更新时间:2023-12-02 22:47:38 29 4
gpt4 key购买 nike

我需要使用 Swagger 记录一个 API,该 API 使用对象映射作为输入和输出,并通过字符串键进行索引。

示例:

{
"a_property": {
"foo": {
"property_1": "a string 1",
"property_2": "a string 2"
},
"bar": {
"property_1": "a string 3",
"property_2": "a string 4"
}
}
}

“foo”和“bar”可以是任何字符串键,但它们在键集中应该是唯一的。

我知道,使用 Swagger,我可以定义一个对象数组,但这提供了一个不同的 API,因为我们将得到如下内容:

{
"a_property": [
{
"key": "foo"
"property_1": "a string 1",
"property_2": "a string 2"
},
{
"key": "bar"
"property_1": "a string 3",
"property_2": "a string 4"
}
]
}

我已阅读 'Open API Specification' - 'Add support for Map data types #38'页。据我了解,它建议使用 additionalProperties,但它似乎不能满足我的需求(或者它不适用于我使用的 Swagger UI 2.1.4)。 我错过了什么吗?

到目前为止,我找到了以下解决方法(在 Swagger JSON 中):

a_property: {
description: "This is a map that can contain several objects indexed by different keys.",
type: object,
properties: {
key: {
description: "map item",
type: "object",
properties: {
property_1: {
description: "first property",
type: string
},
property_2: {
description: "second property",
type: string
}
}
}
}
}

这几乎完成了工作,但读者必须理解“key”可以是任何字符串,并且可以重复多次。

有更好的方法来实现我的需求吗?

最佳答案

使用 additionalProperties 是使用 OpenAPI (fka.Swagger) 规范描述 hashmap 的正确方法,但 Swagger UI 目前不渲染它们。

问题在此处跟踪 https://github.com/swagger-api/swagger-ui/issues/1248

同时,您可以使用此技巧:定义与 map 对象相同类型的非必需属性(在下面的示例中为默认属性),并在描述中给出一些提示:

swagger: "2.0"
info:
version: 1.0.0
title: Hashmap

paths: {}

definitions:
MapItem:
properties:
firstname:
type: string
lastname:
type: string
Map:
description: a (key, MapItem) map. `default`is an example key
properties:
default:
$ref: '#/definitions/MapItem'
additionalProperties:
$ref: '#/definitions/MapItem'

此描述不会修改 API 契约并改进文档。

关于dictionary - Swagger:<字符串,对象> 的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36136885/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com