具有动态键值哈希映射的 Swagger 复杂响应模型 [英] Swagger complex response model with dynamic key value hash maps

查看:17
本文介绍了具有动态键值哈希映射的 Swagger 复杂响应模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用 swagger 的语法来描述响应类型.我要建模的是具有动态键和值的哈希映射.这是允许本地化所必需的.语言可能会有所不同,但应始终提供英语.

I'm struggling with the syntax of swagger to describe a response type. What I'm trying to model is a hash map with dynamic keys and values. This is needed to allow a localization. The languages may vary, but english should always be provided.

响应在 JSON 中如下所示:

The response would look like this in JSON:

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

我的第一次尝试是这样的,但我不知道如何写名字的部分.AdditionalProperties 似乎是一个关键,但我无法理解它.在这种语法中,对英文文本的要求对我来说也是一个谜,并且该示例似乎也没有按预期工作.它会在 UI 中生成一个空的 $folded:.

My first try was looking like that, but I have no idea how to write the part for the name. AdditionalProperties seems to be a key, but I can't wrap my head around it. Also the requirement for english text is a mystery to me in this syntax and the example also does not seem to work as expected. It generates an empty $folded: in the UI.

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

但这会产生:

这也没有任何线索表明结果将以语言代码作为键,将文本作为哈希映射的值.

There is also no clue in this that the result will have a language code as a key and the text as the value of the hash map.

推荐答案

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

It seems you are running into at least three separate bugs and/or limitations:

  1. Swagger-Editor 和 Swagger-UI 均未在文档格式中提供任何指示,以表明您的对象架构中允许使用 additionalProperties.因此,即使您正确使用了 additionalProperties 并且 Swagger 解析器可以识别它,这些文档格式也不会显示它.您需要将此详细信息添加到您的架构 description 中,以便用户了解它们可以包含其他字符串属性.

    注意:您可能还希望其他属性名称遵循约定,例如两个字母的语言代码.虽然完整的 JSON Schema 允许您使用 patternProperties 指定它,但不幸的是,Swagger 的模式对象不支持这一点.同样,您应该在架构描述中指定这一点.

  1. Neither Swagger-Editor nor Swagger-UI provide any indication in the documentation format to show that additionalProperties are allowed in your object schema. So even where you've used additionalProperties correctly, and it's recognized by the Swagger parser, these documentation formats won't show it. You need to add this detail to your schema description, so users understand that they can include additional string properties.

    Note: You probably also expect the additional property names to follow a convention, e.g. a two-letter language code. While full JSON Schema lets you specify this with patternProperties, unfortunately that's not supported in Swagger's schema object. So again, you should specify that in your schema description.

Swagger-Editor 格式有时会显示这种奇怪的折叠:"属性.我今天早上看到它,现在奇怪的是我无法重现它.今天可能已经修复了.但无论如何,这肯定是一个错误,并且特定于 Swagger-Editor.它不应影响您的下游代码生成,也不应影响在运行时向客户端开发人员提供 API 文档的标准 Swagger-UI.(虽然 Swagger-Editor 中的文档窗格看起来与 Swagger-UI 相似,但它是一个单独的实现.)

The Swagger-Editor format sometimes shows this odd "folded:" property. I saw it this morning, now strangely I cannot reproduce it. It might have been hotfixed today. But regardless, it's certainly a bug, and specific to Swagger-Editor. It should not affect your downstream code generation, nor the standard Swagger-UI that presents your API documentation to client developers at runtime. (Though the documentation pane in Swagger-Editor looks similar to Swagger-UI, it is a separate implementation.)

在 Swagger 中使用 additionalProperties 有一些微妙但重要的限制.虽然 Helen 的示例没有显示任何可见的错误,但实际上 Swagger 解析器将无法正确处理此架构;它将忽略您明确声明的 en 属性,或者将忽略 additionalProperties

There are some subtle but significant limitations to the use of additionalProperties in Swagger. While Helen's example doesn't show any visible errors, in fact the Swagger parser will fail to process this schema correctly; it will ignore either your explicitly declared en property, or will ignore additionalProperties!

最后一个问题归结为 Swagger-Model 的设计缺陷,它是整个 Swagger Java 堆栈(包括 Swagger-Codegen)中使用的核心组件之一.在某些上下文中定义的模式可以使用 propertiesadditionalProperties 的组合.但是在其他上下文中定义的模式不能.

This last issue comes down to a design flaw in Swagger-Model, one of the core components used throughout the Swagger Java stack (including Swagger-Codegen). Schemas defined in certain contexts can work with a combination of properties and additionalProperties. But schemas defined in other contexts cannot.

我们已经 在此处详细记录了这一点.

好消息:只要稍加调整,我们就可以让 Helen 的示例正常工作.我们只需要将嵌套对象模式提取到它自己的顶级定义中.我称之为LocalizedName:

The good news: with a small tweak, we can make Helen's example work correctly. We just need to extract the nested object schema into its own top-level definition. I'll call it 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

这篇关于具有动态键值哈希映射的 Swagger 复杂响应模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆