将枚举模式视为对象(而不是字符串)

时间:2019-11-26 15:11:13

标签: java enums swagger-2.0

我们有一个用Jackson批注定义的枚举“ @JsonFormat(shape = JsonFormat.Shape.OBJECT)”

例如

package com.test;
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape =  JsonFormat.Shape.OBJECT)
public enum SampleEnumName {
    OPTION_1 ("NAME_1", "DESCRIPTION_1",  "CATEGORY_1"),
    OPTION_2 ("NAME_2", "DESCRIPTION_2",  "CATEGORY_2"),
    OPTION_3 ("NAME_3", "DESCRIPTION_3",  "CATEGORY_3");

    private String name;
    private String description;
    private String category;

    private SampleEnumName(String name, String description, String category) {
        this.name = name;
        this.description = description;
        this.category = category;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getCategory() {
        return category;
    }
}

生成其架构时,我们将其视为一个对象,例如

{"type":"object","id":"urn:jsonschema:com:test:SampleEnumName","properties":{"name":{"type":"string"},"description":{"type":"string"},"category":{"type":"string"}}}

但同样,当我们检查swagger生成的JSON时,架构中将显示以下内容:

{"type":"string","enum":["OPTION_1","OPTION_2","OPTION_3"]}

有没有办法(可以像杰克逊注释“ @JsonFormat(shape = JsonFormat.Shape.OBJECT)””中那样有些招摇的注释)来显示枚举的对象定义?

我们使用的是招摇的1.5.7

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,更新为Swagger 2.9.2并解决了。