Display Custom HashMap Key Using Swagger Annotatio

2019-08-02 10:19发布

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": {}
      }

What I need it to look like is:

      "objectName": {
        "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)?

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

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

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.

1条回答
劫难
2楼-- · 2019-08-02 11:08

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

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

and in the main code added:

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

which generates

"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.

查看更多
登录 后发表回答