Skip to content

Commit 60e0512

Browse files
committed
ConversionFailedException: When accessing v3/api-docs. Fixes springdoc#1097
1 parent c3555ac commit 60e0512

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2626
import org.apache.commons.lang3.StringUtils;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2729

2830
import org.springframework.core.MethodParameter;
2931
import org.springframework.core.convert.TypeDescriptor;
@@ -69,6 +71,8 @@ public class ParameterInfo {
6971
*/
7072
private String paramType;
7173

74+
private static final Logger LOGGER = LoggerFactory.getLogger(ParameterInfo.class);
75+
7276

7377
/**
7478
* Instantiates a new Parameter info.
@@ -99,10 +103,18 @@ else if (cookieValue != null)
99103

100104
if (StringUtils.isNotBlank(this.pName))
101105
this.pName = propertyResolverUtils.resolve(this.pName);
102-
if (this.defaultValue !=null && !ValueConstants.DEFAULT_NONE.equals(this.defaultValue.toString())){
106+
if (this.defaultValue != null && !ValueConstants.DEFAULT_NONE.equals(this.defaultValue.toString())) {
103107
this.defaultValue = propertyResolverUtils.resolve(this.defaultValue.toString());
104108
parameterBuilder.getOptionalWebConversionServiceProvider()
105-
.ifPresent(conversionService ->this.defaultValue= conversionService.convert(this.defaultValue, new TypeDescriptor(methodParameter)));
109+
.ifPresent(conversionService -> {
110+
try {
111+
this.defaultValue = conversionService.convert(this.defaultValue, new TypeDescriptor(methodParameter));
112+
}
113+
catch (Exception e) {
114+
LOGGER.warn("Using the following default value : {}, without spring conversionService", this.defaultValue);
115+
}
116+
}
117+
);
106118
}
107119

108120
this.required = this.required && !methodParameter.isOptional();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.org.springdoc.api.app153;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
6+
@Schema(type="string", allowableValues={"finished", "new"})
7+
public enum OrderState {
8+
FINISHED("finished"),
9+
NEW("new");
10+
11+
OrderState(String value) {
12+
this.value = value;
13+
}
14+
15+
public String getValue() {
16+
return value;
17+
}
18+
19+
private final String value;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package test.org.springdoc.api.app153;
2+
3+
import java.beans.PropertyEditorSupport;
4+
import java.util.Arrays;
5+
6+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
7+
8+
public class OrderStateMapper extends PropertyEditorSupport {
9+
10+
@Override
11+
public void setAsText(String text) {
12+
setValue(
13+
Arrays.stream(OrderState.class.getEnumConstants())
14+
.filter(e -> e.getValue().equals(text))
15+
.findFirst()
16+
.orElseThrow(() -> new MethodArgumentTypeMismatchException(
17+
text, OrderState.class, "orderState", null, null)));
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app153;
20+
21+
22+
import test.org.springdoc.api.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
26+
/**
27+
* Tests Spring meta-annotations as method parameters
28+
*/
29+
public class SpringDocApp153Test extends AbstractSpringDocTest {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.app153;
2+
3+
import org.springframework.web.bind.WebDataBinder;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.InitBinder;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
class TestController {
11+
12+
@InitBinder
13+
public void initBinder(WebDataBinder dataBinder) {
14+
dataBinder.registerCustomEditor(OrderState.class, new OrderStateMapper());
15+
}
16+
17+
@GetMapping(value = {"/orders"})
18+
public Object method(
19+
@RequestParam(value = "state", defaultValue = "finished") OrderState orderState) {
20+
return null;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
"/orders": {
15+
"get": {
16+
"tags": [
17+
"test-controller"
18+
],
19+
"operationId": "method",
20+
"parameters": [
21+
{
22+
"name": "state",
23+
"in": "query",
24+
"required": false,
25+
"schema": {
26+
"type": "string",
27+
"enum": [
28+
"finished",
29+
"new"
30+
],
31+
"default": "finished"
32+
}
33+
}
34+
],
35+
"responses": {
36+
"200": {
37+
"description": "OK",
38+
"content": {
39+
"*/*": {
40+
"schema": {
41+
"type": "object"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
},
50+
"components": {}
51+
}

0 commit comments

Comments
 (0)