Skip to content

Commit 078bdec

Browse files
committed
Java enumeration and Spring Converter no longer generates enum drop-down
1 parent 13e6282 commit 078bdec

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ java -jar build/libs/enum-problem-example-0.0.1-SNAPSHOT.jar
2222

2323
![img_166.png](img_166.png)
2424

25-
### In 1.6.8 there isn't drop-down (so enum from @schema-annotation isn't generated as expected)
25+
### In 1.6.7 and 1.6.13 there isn't drop-down (so enum from @schema-annotation isn't generated as expected)
2626

2727
```
2828
[{"name":"fooBar","in":"path","required":true,"schema":{"type":"string"}}]

build.gradle.kts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ repositories {
1515

1616
dependencies {
1717
implementation("org.springframework.boot:spring-boot-starter-web")
18-
// Change version to 1.6.7 or 1.6.8 to test problem with enum in swagger ui
18+
// Change version to 1.6.7 or 1.6.13 to test problem with enum in swagger ui
1919
implementation("org.springdoc:springdoc-openapi-ui:1.6.6")
2020
implementation("org.springdoc:springdoc-openapi-data-rest:1.6.6")
2121

22-
//implementation("org.springdoc:springdoc-openapi-ui:1.6.8")
23-
//implementation("org.springdoc:springdoc-openapi-data-rest:1.6.8")
22+
// implementation("org.springdoc:springdoc-openapi-ui:1.6.7")
23+
// implementation("org.springdoc:springdoc-openapi-data-rest:1.6.7")
24+
25+
// implementation("org.springdoc:springdoc-openapi-ui:1.6.13")
26+
// implementation("org.springdoc:springdoc-openapi-data-rest:1.6.13")
2427
}
Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
package com.example.enumproblemexample;
22

3-
import io.swagger.v3.oas.annotations.media.Schema;
3+
import javax.annotation.Generated;
44

5-
@Schema(type = "String", allowableValues = { "foo", "bar" })
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonValue;
7+
8+
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
69
public enum FooBar {
7-
FOO,
8-
BAR;
10+
FOO("foo"),
11+
BAR("bar");
12+
13+
private String value;
14+
15+
FooBar(String value) {
16+
this.value = value;
17+
}
18+
19+
@JsonValue
20+
public String getValue() {
21+
return value;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return String.valueOf(value);
27+
}
928

10-
public String toLowerCase() {
11-
var lower = this.name().toLowerCase();
12-
return lower.replace("_", "-");
29+
@JsonCreator
30+
public static FooBar fromValue(String value) {
31+
for (FooBar b : FooBar.values()) {
32+
if (b.value.equals(value)) {
33+
return b;
34+
}
35+
}
36+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
1337
}
1438
}

src/main/java/com/example/enumproblemexample/FooBarConverter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ public class FooBarConverter implements Converter<String, FooBar> {
88

99
@Override
1010
public FooBar convert(String source) {
11-
return FooBar.valueOf(fooBarToUpperCase(source));
11+
return FooBar.fromValue(source);
1212
}
1313

14-
private String fooBarToUpperCase(String source) {
15-
var upper = source.toUpperCase();
16-
return upper.replace("-", "_");
17-
}
1814
}

0 commit comments

Comments
 (0)