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

查看:743
本文介绍了使用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注释吗?我不希望对整个示例进行硬编码,因为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罐子.到目前为止,我只能找到使用直接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.

推荐答案

我确实有一个骇人听闻的解决方案,在其中我制作了一个虚拟类,如下所示:

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天全站免登陆