Skip to content

Commit ffc8b0d

Browse files
committed
Changes report: @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) #1663
1 parent 99abf29 commit ffc8b0d

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
340340
Type type = ReturnTypeParser.getType(methodParameter);
341341
if(type instanceof Class clazz && optionalWebConversionServiceProvider.isPresent()){
342342
WebConversionServiceProvider webConversionServiceProvider = optionalWebConversionServiceProvider.get();
343-
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType(clazz))
343+
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && methodParameter.getParameterType().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class)==null)
344344
type = webConversionServiceProvider.getSpringConvertedType(methodParameter.getParameterType());
345345
}
346346
schemaN = SpringDocAnnotationsUtils.extractSchema(components, type, jsonView, methodParameter.getParameterAnnotations());
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app188;
26+
27+
import io.swagger.v3.oas.annotations.media.Schema;
28+
29+
@Schema(type = "String", allowableValues = { "foo", "bar" })
30+
public enum FooBar {
31+
FOO,
32+
BAR;
33+
34+
public String toLowerCase() {
35+
var lower = this.name().toLowerCase();
36+
return lower.replace("_", "-");
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app188;
26+
27+
import org.springframework.core.convert.converter.Converter;
28+
import org.springframework.stereotype.Component;
29+
30+
@Component
31+
public class FooBarConverter implements Converter<String, FooBar> {
32+
33+
@Override
34+
public FooBar convert(String source) {
35+
return FooBar.valueOf(fooBarToUpperCase(source));
36+
}
37+
38+
private String fooBarToUpperCase(String source) {
39+
var upper = source.toUpperCase();
40+
return upper.replace("-", "_");
41+
}
42+
}

springdoc-openapi-starter-webmvc-api/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
@@ -40,6 +40,7 @@
4040
import org.springframework.context.annotation.Bean;
4141
import org.springframework.http.MediaType;
4242
import org.springframework.web.bind.annotation.GetMapping;
43+
import org.springframework.web.bind.annotation.PathVariable;
4344
import org.springframework.web.bind.annotation.RestController;
4445
import org.springframework.web.method.HandlerMethod;
4546

@@ -49,6 +50,11 @@ public class HelloController {
4950
@GetMapping("/test")
5051
public void test(){}
5152

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

springdoc-openapi-starter-webmvc-api/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)