使用 Swagger 注释显示自定义 HashMap 键 [英] Display Custom HashMap Key Using Swagger Annotations

查看:107
本文介绍了使用 Swagger 注释显示自定义 HashMap 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Java 代码生成 Swagger 文档,我的嵌套模型属性之一是 HashMap.为它生成的示例如下:

I'm trying to generate Swagger documentation from Java code and one of my nested model properties is a HashMap. The generated example for it is as follows:

      "additionalProp1": {
        "customObject": {}
      },
      "additionalProp2": {
        "customObject": {}
      },
      "additionalProp3": {
        "customObject": {}
      }

我需要它看起来像:

      "objectName": {
        "customObject": {}
      }

换句话说,我需要告诉他们 additionalProp String 应该是什么,最好不要重复三遍.有没有办法使用 Swagger Annotations 来做到这一点?我不想硬编码一个完整的示例,因为 CustomObject 本身具有许多属性并且仍在不断变化.是否有可能按照

In other words, I need to tell them what the additionalProp String should be and preferably not have it repeated three times. Is there any way to do this using Swagger Annotations? I'd prefer not to hardcode a full example since the CustomObject has a number of properties itself and is still in flux. Is it possible to maybe do something along the lines of

@ApiModelProperty(example = "objectName:{package.CustomObject}")

(除了一些实际有效的东西)?

(except something that actually works)?

我曾尝试以这种方式和其他方式使用 @ApiModelProperty,但到目前为止都没有成功.

I have tried playing with the @ApiModelProperty in this and other ways but so far unsuccessfully.

@ApiModelProperty
public HashMap<String, CustomObject> getObjectMap(){
    return objectMap;
}

我使用的是 1.5.18 Swagger jar.到目前为止,我只能找到使用直接 yaml 或 json 操作解决的类似问题.

I am using the 1.5.18 Swagger jars. So far I've only been able to find similar problems solved using direct yaml or json manipulation.

推荐答案

我确实有一个 hacky 解决方案,我制作了一个虚拟类,如下所示:

I do have a hacky solution where I made a dummy class as follows:

public class SwaggerObject {
    public CustomObject getObjectName() {
        return null;
    }
}

并在主代码中添加:

@ApiModelProperty(dataType="package.SwaggerObject")
public HashMap<String, CustomObject> getObjectMap(){
    return objectMap;
}

产生

"objectName": {
  "customObject": {}
}

然而,为它需要一个全新的类似乎很愚蠢.它还在底部的模型中添加了虚拟类,这可能会引起混淆.

however, it seems silly to need an entire new class for it. It also adds the dummy class to the Models at the bottom which could be confusing.

这篇关于使用 Swagger 注释显示自定义 HashMap 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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