基于动态键值哈希Map的Swagger复杂响应模型

anauzrmj  于 2022-12-23  发布在  其他
关注(0)|答案(3)|浏览(193)

我正在努力使用swagger的语法来描述一个响应类型。我尝试建模的是一个带有动态键和值的散列Map。这是允许本地化所必需的。语言可能会有所不同,但英语应该总是提供的。
JSON中的响应如下所示:

{
  id: "1234",
  name: {
    en: "english text",
    de: "Deutscher Text"
  }
}

我的第一次尝试是这样的,但是我不知道如何编写名称部分。AdditionalProperties似乎是一个关键字,但是我无法理解它。另外,在这个语法中,对英语文本的要求对我来说是一个谜,这个例子似乎也没有像预期的那样工作。它生成了一个空的$folded:在用户界面中。

delayReason:
  type: object
  properties:
    id:
      type: string
      description: Identifier for a delay reason.
    name:
      type: object
      additionalProperties: 
        type: string
  required: [id, name]
  example:
    id: 123
    name: 
      en: english text
      de: Deutscher Text

但这会产生:

在这方面也没有线索表明结果将以语言代码作为键,以文本作为散列Map的值。

3yhwsihp

3yhwsihp1#

您对additionalProperties的使用是正确的,您的型号也是正确的。

附加属性

在Swagger/OpenAPI中,hashmap键被假定为字符串,因此键类型没有显式定义。additionalProperties定义了hashmap值的类型。

type: object
additionalProperties: 
  type: string

定义字符串到字符串Map,例如:

{
  "en": "English text",
  "de": "Deutscher Text"
}

如果需要字符串到整数的Map,例如:

{
  "en": 5,
  "de": 3
}

您可以将additionalProperties定义为具有值类型integer

type: object
additionalProperties: 
  type: integer

哈希Map中的必需键

要将en定义为散列表中的必需键:

type: object
properties:
  en:
    type: string
required: [en]
additionalProperties: 
  type: string

完整示例

definitions:
  delayReason:
    type: object
    properties:
      id:
        type: string
        description: Identifier for a delay reason.
      name:
        type: object
        description: A hashmap with language code as a key and the text as the value.
        properties:
          en:
            type: string
            description: English text of a delay reason.
        required: [en]
        additionalProperties: 
          type: string
    required: [id, name]
    example:
      id: '123' # Note the quotes to force the value as a string
      name: 
        en: English text
        de: Deutscher Text

在这方面也没有线索表明结果将以语言代码作为键,以文本作为散列Map的值。
诸如此类的事情可以在description中口头记录。
这个例子看起来也不像预期的那样工作。2它生成了一个空的$folded:在用户界面中。
不确定您的原始规范有什么问题,但上面的规范是有效的,在Swagger Editor中看起来很好。

k7fdbhmy

k7fdbhmy2#

您似乎遇到了至少三个独立的错误和/或限制:

  1. Swagger-Editor和Swagger-UI都没有在文档格式中提供任何指示来显示additionalProperties在您的对象模式中是允许的。因此,即使您正确使用了additionalProperties,并且它被Swagger解析器识别,这些文档格式也不会显示它。您需要将此细节添加到您的模式description中,以便用户了解他们可以包括额外的字符串属性。
    • 注意:**您可能还希望附加属性名称遵循约定,例如两个字母的语言代码。虽然完整的JSON Schema允许您使用patternProperties指定此名称,但遗憾的是,Swagger的schema对象不支持此名称。因此,您应该在schema描述中指定此名称。
  1. Swagger-Editor格式有时会显示这个奇怪的"folded:"属性。今天早上我看到了它,现在奇怪的是我不能重现它。它可能今天已经被热修复了。但是不管怎样,它肯定是一个bug,并且是特定于Swagger-Editor的。它不应该影响您的下游代码生成,也不应该影响在运行时向客户端开发人员呈现API文档的标准Swagger-UI。(尽管Swagger-Editor中的文档窗格看起来类似于Swagger-UI,但它是一个独立的实现。)
    1.在Swagger中使用additionalProperties有一些微妙但重要的限制,虽然Helen的示例没有显示任何可见的错误,但事实上,Swagger解析器将无法正确处理此模式;它将忽略您显式声明的en属性,或者忽略additionalProperties!
    最后一个问题归结为Swagger-Model中的一个设计缺陷,Swagger-Model是整个Swagger Java堆栈(包括Swagger-Codegen)中使用的核心组件之一。在某些上下文中定义的模式可以与propertiesadditionalProperties的组合一起工作,但在其他上下文中定义的模式不能。
    我们有documented this in detail here
    好消息是:只需做一点小调整,我们就可以使Helen的示例正常工作,我们只需要将嵌套的对象模式提取到它自己的顶层定义中,我将其命名为LocalizedName
definitions:
  delayReason:
    type: object
    properties:
      id:
        type: string
        description: Identifier for a delay reason.
      name:
        $ref: '#/definitions/LocalizedName'
    required: [id, name]
    example:
      id: '123' # Note the quotes to force the value as a string
      name:
        en: English text
        de: Deutscher Text

  LocalizedName:
    type: object
    description: A hashmap with language code as a key and the text as the value.
    properties:
      en:
        type: string
        description: English text of a delay reason.
    required: [en]
    additionalProperties:
      type: string
eoxn13cs

eoxn13cs3#

如果你还在设计阶段,你可以不使用动态键,而是将所有其他语言的引用推入一个数组中,其结构如下:

{
"launguage":"en"
"text":"Hello World"
}

这将大大简化工作,而且由于您不依赖于additionalProperties,因此您生成的客户端库将发现这更容易理解。

相关问题