Skip to content

Commit 209c55b

Browse files
committed
Java enumeration and Spring Converter no longer generates enum drop-down. Fixes #1992
1 parent d27acd9 commit 209c55b

File tree

6 files changed

+159
-2
lines changed

6 files changed

+159
-2
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/GenericParameterService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878

7979
/**
8080
* The type Generic parameter builder.
81+
*
8182
* @author bnasslahsen, coutin
8283
*/
8384
@SuppressWarnings("rawtypes")
@@ -355,8 +356,11 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
355356
Type type = ReturnTypeParser.getType(methodParameter);
356357
if (type instanceof Class && optionalWebConversionServiceProvider.isPresent()) {
357358
WebConversionServiceProvider webConversionServiceProvider = optionalWebConversionServiceProvider.get();
358-
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && methodParameter.getParameterType().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class) == null)
359-
type = webConversionServiceProvider.getSpringConvertedType(methodParameter.getParameterType());
359+
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && methodParameter.getParameterType().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class) == null) {
360+
Class<?> springConvertedType = webConversionServiceProvider.getSpringConvertedType(methodParameter.getParameterType());
361+
if (!(String.class.equals(springConvertedType) && ((Class<?>) type).isEnum()))
362+
type = springConvertedType;
363+
}
360364
}
361365
schemaN = SpringDocAnnotationsUtils.extractSchema(components, type, jsonView, methodParameter.getParameterAnnotations());
362366
}
@@ -569,6 +573,7 @@ public Optional<WebConversionServiceProvider> getOptionalWebConversionServicePro
569573
/**
570574
* Resolve the given annotation-specified value,
571575
* potentially containing placeholders and expressions.
576+
*
572577
* @param value the value
573578
* @return the object
574579
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package test.org.springdoc.api.v30.app200;
2+
3+
4+
import javax.annotation.Generated;
5+
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonValue;
8+
9+
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
10+
public enum FooBar {
11+
FOO("foo"),
12+
BAR("bar");
13+
14+
private String value;
15+
16+
FooBar(String value) {
17+
this.value = value;
18+
}
19+
20+
@JsonValue
21+
public String getValue() {
22+
return value;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return String.valueOf(value);
28+
}
29+
30+
@JsonCreator
31+
public static FooBar fromValue(String value) {
32+
for (FooBar b : FooBar.values()) {
33+
if (b.value.equals(value)) {
34+
return b;
35+
}
36+
}
37+
throw new IllegalArgumentException("Unexpected value '" + value + "'");
38+
}
39+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.v30.app200;
2+
3+
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class FooBarController {
10+
@GetMapping(value = "/example/{fooBar}")
11+
public String getFooBar(@PathVariable FooBar fooBar) {
12+
return fooBar.name();
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test.org.springdoc.api.v30.app200;
2+
3+
4+
import org.springframework.core.convert.converter.Converter;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class FooBarConverter implements Converter<String, FooBar> {
9+
10+
@Override
11+
public FooBar convert(String source) {
12+
return FooBar.fromValue(source);
13+
}
14+
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app200;
24+
25+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp200Test extends AbstractSpringDocV30Test {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/example/{fooBar}": {
15+
"get": {
16+
"tags": [
17+
"foo-bar-controller"
18+
],
19+
"operationId": "getFooBar",
20+
"parameters": [
21+
{
22+
"name": "fooBar",
23+
"in": "path",
24+
"required": true,
25+
"schema": {
26+
"type": "string",
27+
"enum": [
28+
"foo",
29+
"bar"
30+
]
31+
}
32+
}
33+
],
34+
"responses": {
35+
"200": {
36+
"description": "OK",
37+
"content": {
38+
"*/*": {
39+
"schema": {
40+
"type": "string"
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
},
49+
"components": {}
50+
}

0 commit comments

Comments
 (0)