Skip to content

Commit eaaf2fd

Browse files
committed
@Schema annotation with type String and allowableValues set doesn't generate enum drop-down in swagger-ui after upgrading from 1.6.6 (when Spring custom converter is used). Fixes #1663.
1 parent d7e4a1b commit eaaf2fd

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
331331
Type type = ReturnTypeParser.getType(methodParameter);
332332
if(type instanceof Class && optionalWebConversionServiceProvider.isPresent()){
333333
WebConversionServiceProvider webConversionServiceProvider = optionalWebConversionServiceProvider.get();
334-
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type))
334+
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && methodParameter.getParameterType().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class)==null)
335335
type = webConversionServiceProvider.getSpringConvertedType(methodParameter.getParameterType());
336336
}
337337
schemaN = SpringDocAnnotationsUtils.extractSchema(components, type, jsonView, methodParameter.getParameterAnnotations());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.app188;
24+
25+
import io.swagger.v3.oas.annotations.media.Schema;
26+
27+
@Schema(type = "String", allowableValues = { "foo", "bar" })
28+
public enum FooBar {
29+
FOO,
30+
BAR;
31+
32+
public String toLowerCase() {
33+
String lower = this.name().toLowerCase();
34+
return lower.replace("_", "-");
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.app188;
24+
25+
import org.springframework.core.convert.converter.Converter;
26+
import org.springframework.stereotype.Component;
27+
28+
@Component
29+
public class FooBarConverter implements Converter<String, FooBar> {
30+
31+
@Override
32+
public FooBar convert(String source) {
33+
return FooBar.valueOf(fooBarToUpperCase(source));
34+
}
35+
36+
private String fooBarToUpperCase(String source) {
37+
String upper = source.toUpperCase();
38+
return upper.replace("-", "_");
39+
}
40+
}

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app188/HelloController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.context.annotation.Bean;
3939
import org.springframework.http.MediaType;
4040
import org.springframework.web.bind.annotation.GetMapping;
41+
import org.springframework.web.bind.annotation.PathVariable;
4142
import org.springframework.web.bind.annotation.RestController;
4243
import org.springframework.web.method.HandlerMethod;
4344

@@ -47,6 +48,11 @@ public class HelloController {
4748
@GetMapping("/test")
4849
public void test(){}
4950

51+
@GetMapping(value = "/example/{fooBar}")
52+
public String getFooBar(@PathVariable FooBar fooBar) {
53+
return fooBar.name();
54+
}
55+
5056
@Bean
5157
public OpenAPI openAPI(){return new OpenAPI().components(new Components());}
5258

springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app188.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,49 @@
3232
}
3333
}
3434
}
35+
},
36+
"/example/{fooBar}": {
37+
"get": {
38+
"tags": [
39+
"hello-controller"
40+
],
41+
"operationId": "getFooBar",
42+
"parameters": [
43+
{
44+
"name": "fooBar",
45+
"in": "path",
46+
"required": true,
47+
"schema": {
48+
"type": "string",
49+
"enum": [
50+
"foo",
51+
"bar"
52+
]
53+
}
54+
}
55+
],
56+
"responses": {
57+
"200": {
58+
"description": "OK",
59+
"content": {
60+
"*/*": {
61+
"schema": {
62+
"type": "string"
63+
}
64+
}
65+
}
66+
},
67+
"5xx": {
68+
"content": {
69+
"application/json": {
70+
"schema": {
71+
"$ref": "#/components/schemas/ErrorResponse"
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
3578
}
3679
},
3780
"components": {

0 commit comments

Comments
 (0)