ModelResolver: How to ref enum type with enum as schema component?
In the ModelResolver, there is this piece of code.
Given an enum property and model class
public class Test {
public State state;
public enum State { UP, DOWN }
}
This generates a string property with the enum values exploded out.
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"type" : "string",
"enum" : [ "UP", "DOWN" ]
}
}
}
According to the Enums documentation, under the section Reusable Enums, it could instead be formatted like so:
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"$ref": "#/components/schemas/State"
}
}
},
"State" : {
"type" : "string",
"enum" : [ "UP", "DOWN" ]
}
How does one achieve this?
I tried the following variants:
1
public class Test {
public State state;
@Schema
public enum State { UP, DOWN }
}
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"type" : "string",
"enum" : [ "UP", "DOWN" ]
}
}
}
2
public class Test {
public State state;
@Schema(
type = "string",
implementation = String.class,
allowableValues = ["UP", "DOWN"]
)
public enum State { UP, DOWN }
}
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"type" : "string",
"enum" : [ "UP", "DOWN" ]
}
}
}
3
public class Test {
public State state;
@Schema(
type = "string",
implementation = String.class,
allowableValues = ["UP", "DOWN"],
ref = "#/components/schemas/State"
)
public enum State { UP, DOWN }
}
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"$ref" : "#/components/schemas/State"
}
}
},
"State" : {
"$ref" : "#/components/schemas/State"
}
4
public class Test {
@Schema(ref = "#/components/schemas/State")
public State state;
@Schema(
type = "string",
implementation = String.class,
allowableValues = ["UP", "DOWN"]
)
public enum State { UP, DOWN }
}
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"type" : "string"
}
}
}
5
public class Test {
@Schema(ref = "#/components/schemas/State")
public State state;
@Schema(
type = "string",
allowableValues = ["UP", "DOWN"]
)
public enum State { UP, DOWN }
}
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"type" : "string",
"enum" : [ "UP", "DOWN" ]
}
}
}
6
public class Test {
@Schema(ref = "#/components/schemas/State")
public State state;
@Schema(
implementation = String.class,
allowableValues = ["UP", "DOWN"]
)
public enum State { UP, DOWN }
}
"Test" : {
"type" : "object",
"properties" : {
"state" : {
"type" : "string"
}
}
}
I just ran into the same problem. This is especially bad for classes that expose access rights, e.g. read / readwrite. Then you get X repetitions of the same enum, which is rather cumbersome to use.
I was taking a look through the open PRs, I think #2996 handles this (original issue reported with #2994).
Changes are performed on the following lines: https://github.com/swagger-api/swagger-core/pull/2996/files#diff-d4e9202b05579faab0e8109441f491cfR243 https://github.com/swagger-api/swagger-core/pull/2996/files#diff-098e4372496f1a4de0a865a99aafdad8R75
Thanks for reporting and analyzing, and for the PR; this is fixed in #3352 which includes and replaces #3294
public class Test {
public State state;
}
@Schema(enumAsRef=true)
public enum State { UP, DOWN }