Skip to content

Commit 208882c

Browse files
committed
code review
2 parents 2e9420e + efceecd commit 208882c

File tree

8 files changed

+330
-48
lines changed

8 files changed

+330
-48
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.lang.reflect.Type;
3030
import java.util.ArrayList;
3131
import java.util.Arrays;
32+
import java.util.Collection;
3233
import java.util.HashSet;
3334
import java.util.LinkedHashMap;
3435
import java.util.List;
@@ -41,6 +42,8 @@
4142
import java.util.stream.Stream;
4243

4344
import com.fasterxml.jackson.annotation.JsonView;
45+
import com.fasterxml.jackson.core.JsonProcessingException;
46+
import com.fasterxml.jackson.databind.ObjectMapper;
4447
import io.swagger.v3.core.util.AnnotationsUtils;
4548
import io.swagger.v3.oas.models.Components;
4649
import io.swagger.v3.oas.models.Operation;
@@ -50,7 +53,11 @@
5053
import io.swagger.v3.oas.models.responses.ApiResponses;
5154
import org.apache.commons.lang3.ArrayUtils;
5255
import org.apache.commons.lang3.StringUtils;
56+
import org.slf4j.Logger;
57+
import org.slf4j.LoggerFactory;
58+
import org.springdoc.core.converters.AdditionalModelsConverter;
5359
import org.springdoc.core.providers.JavadocProvider;
60+
import org.springdoc.core.providers.ObjectMapperProvider;
5461

5562
import org.springframework.core.MethodParameter;
5663
import org.springframework.core.ResolvableType;
@@ -74,6 +81,7 @@
7481

7582
/**
7683
* The type Generic response builder.
84+
*
7785
* @author bnasslahsen
7886
*/
7987
public class GenericResponseService {
@@ -118,6 +126,11 @@ public class GenericResponseService {
118126
*/
119127
private final List<ControllerAdviceInfo> controllerAdviceInfos = new ArrayList<>();
120128

129+
/**
130+
* The constant LOGGER.
131+
*/
132+
private static final Logger LOGGER = LoggerFactory.getLogger(GenericResponseService.class);
133+
121134
/**
122135
* Instantiates a new Generic response builder.
123136
*
@@ -242,9 +255,9 @@ private Map<String, ApiResponse> filterAndEnrichGenericMapResponseByDeclarations
242255
JavadocProvider javadocProvider = operationService.getJavadocProvider();
243256
for (Map.Entry<String, ApiResponse> genericResponse : genericMapResponse.entrySet()) {
244257
Map<String, Object> extensions = genericResponse.getValue().getExtensions();
245-
Set<Class<?>> genericExceptions = (Set<Class<?>>) extensions.get(EXTENSION_EXCEPTION_CLASSES);
258+
Collection<String> genericExceptions = (Collection<String>) extensions.get(EXTENSION_EXCEPTION_CLASSES);
246259
for (Class<?> declaredException : handlerMethod.getMethod().getExceptionTypes()) {
247-
if (genericExceptions.contains(declaredException)) {
260+
if (genericExceptions.contains(declaredException.getName())) {
248261
Map<String, String> javadocThrows = javadocProvider.getMethodJavadocThrows(handlerMethod.getMethod());
249262
String description = javadocThrows.get(declaredException.getName());
250263
if (description == null)
@@ -675,7 +688,16 @@ private synchronized Map<String, ApiResponse> getGenericMapResponse(Class<?> bea
675688
});
676689
}
677690

678-
return genericApiResponseMap;
691+
LinkedHashMap<String, ApiResponse> genericApiResponsesClone;
692+
try {
693+
ObjectMapper objectMapper = ObjectMapperProvider.createJson(springDocConfigProperties);
694+
genericApiResponsesClone = objectMapper.readValue(objectMapper.writeValueAsString(genericApiResponseMap), ApiResponses.class);
695+
return genericApiResponsesClone;
696+
}
697+
catch (JsonProcessingException e) {
698+
LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage());
699+
return genericApiResponseMap;
700+
}
679701
}
680702

681703
/**

springdoc-openapi-javadoc/src/test/resources/results/app162.json

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
}
3939
],
4040
"responses": {
41-
"400": {
42-
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
41+
"404": {
42+
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#find(String)</code> method",
4343
"content": {
4444
"*/*": {
4545
"schema": {
@@ -48,11 +48,11 @@
4848
}
4949
},
5050
"x-exception-class": [
51-
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
51+
"test.org.springdoc.api.app162.exception.NoResultException"
5252
]
5353
},
54-
"404": {
55-
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
54+
"400": {
55+
"description": "the <code>return</code> javadoc for the <code>#handleNonUniqueResultException(NonUniqueResultException)</code> method",
5656
"content": {
5757
"*/*": {
5858
"schema": {
@@ -61,7 +61,7 @@
6161
}
6262
},
6363
"x-exception-class": [
64-
"test.org.springdoc.api.app162.exception.NoResultException"
64+
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
6565
]
6666
},
6767
"200": {
@@ -105,8 +105,8 @@
105105
"required": true
106106
},
107107
"responses": {
108-
"400": {
109-
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
108+
"404": {
109+
"description": "the <code>return</code> javadoc for the <code>#handleNotFoundException(NoResultException)</code> method",
110110
"content": {
111111
"*/*": {
112112
"schema": {
@@ -115,11 +115,11 @@
115115
}
116116
},
117117
"x-exception-class": [
118-
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
118+
"test.org.springdoc.api.app162.exception.NoResultException"
119119
]
120120
},
121-
"404": {
122-
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
121+
"400": {
122+
"description": "the <code>return</code> javadoc for the <code>#handleNonUniqueResultException(NonUniqueResultException)</code> method",
123123
"content": {
124124
"*/*": {
125125
"schema": {
@@ -128,7 +128,7 @@
128128
}
129129
},
130130
"x-exception-class": [
131-
"test.org.springdoc.api.app162.exception.NoResultException"
131+
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
132132
]
133133
},
134134
"200": {
@@ -153,8 +153,8 @@
153153
"description": "This is the list method's javadoc.\n The method's signature: <code>#list()</code>",
154154
"operationId": "list",
155155
"responses": {
156-
"400": {
157-
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
156+
"404": {
157+
"description": "the <code>return</code> javadoc for the <code>#handleNotFoundException(NoResultException)</code> method",
158158
"content": {
159159
"*/*": {
160160
"schema": {
@@ -163,11 +163,11 @@
163163
}
164164
},
165165
"x-exception-class": [
166-
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
166+
"test.org.springdoc.api.app162.exception.NoResultException"
167167
]
168168
},
169-
"404": {
170-
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
169+
"400": {
170+
"description": "the <code>return</code> javadoc for the <code>#handleNonUniqueResultException(NonUniqueResultException)</code> method",
171171
"content": {
172172
"*/*": {
173173
"schema": {
@@ -176,7 +176,7 @@
176176
}
177177
},
178178
"x-exception-class": [
179-
"test.org.springdoc.api.app162.exception.NoResultException"
179+
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
180180
]
181181
},
182182
"200": {
@@ -213,8 +213,8 @@
213213
"required": true
214214
},
215215
"responses": {
216-
"400": {
217-
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
216+
"404": {
217+
"description": "the <code>return</code> javadoc for the <code>#handleNotFoundException(NoResultException)</code> method",
218218
"content": {
219219
"*/*": {
220220
"schema": {
@@ -223,11 +223,11 @@
223223
}
224224
},
225225
"x-exception-class": [
226-
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
226+
"test.org.springdoc.api.app162.exception.NoResultException"
227227
]
228228
},
229-
"404": {
230-
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
229+
"400": {
230+
"description": "the <code>return</code> javadoc for the <code>#handleNonUniqueResultException(NonUniqueResultException)</code> method",
231231
"content": {
232232
"*/*": {
233233
"schema": {
@@ -236,7 +236,7 @@
236236
}
237237
},
238238
"x-exception-class": [
239-
"test.org.springdoc.api.app162.exception.NoResultException"
239+
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
240240
]
241241
},
242242
"201": {
@@ -272,8 +272,8 @@
272272
}
273273
],
274274
"responses": {
275-
"400": {
276-
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
275+
"404": {
276+
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
277277
"content": {
278278
"*/*": {
279279
"schema": {
@@ -282,11 +282,11 @@
282282
}
283283
},
284284
"x-exception-class": [
285-
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
285+
"test.org.springdoc.api.app162.exception.NoResultException"
286286
]
287287
},
288-
"404": {
289-
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
288+
"400": {
289+
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
290290
"content": {
291291
"*/*": {
292292
"schema": {
@@ -295,7 +295,7 @@
295295
}
296296
},
297297
"x-exception-class": [
298-
"test.org.springdoc.api.app162.exception.NoResultException"
298+
"test.org.springdoc.api.app162.exception.NonUniqueResultException"
299299
]
300300
},
301301
"200": {

springdoc-openapi-javadoc/src/test/resources/results/app163.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"required": true
4949
},
5050
"responses": {
51-
"400": {
52-
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
51+
"404": {
52+
"description": "the <code>return</code> javadoc for the <code>#handleNotFoundException(NoResultException)</code> method",
5353
"content": {
5454
"*/*": {
5555
"schema": {
@@ -58,11 +58,11 @@
5858
}
5959
},
6060
"x-exception-class": [
61-
"test.org.springdoc.api.app163.exception.NonUniqueResultException"
61+
"test.org.springdoc.api.app163.exception.NoResultException"
6262
]
6363
},
64-
"404": {
65-
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
64+
"400": {
65+
"description": "the <code>return</code> javadoc for the <code>#handleNonUniqueResultException(NonUniqueResultException)</code> method",
6666
"content": {
6767
"*/*": {
6868
"schema": {
@@ -71,7 +71,7 @@
7171
}
7272
},
7373
"x-exception-class": [
74-
"test.org.springdoc.api.app163.exception.NoResultException"
74+
"test.org.springdoc.api.app163.exception.NonUniqueResultException"
7575
]
7676
},
7777
"200": {
@@ -107,8 +107,8 @@
107107
"required": true
108108
},
109109
"responses": {
110-
"400": {
111-
"description": "the <code>@throws NonUniqueResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
110+
"404": {
111+
"description": "the <code>return</code> javadoc for the <code>#handleNotFoundException(NoResultException)</code> method",
112112
"content": {
113113
"*/*": {
114114
"schema": {
@@ -117,11 +117,11 @@
117117
}
118118
},
119119
"x-exception-class": [
120-
"test.org.springdoc.api.app163.exception.NonUniqueResultException"
120+
"test.org.springdoc.api.app163.exception.NoResultException"
121121
]
122122
},
123-
"404": {
124-
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
123+
"400": {
124+
"description": "the <code>return</code> javadoc for the <code>#handleNonUniqueResultException(NonUniqueResultException)</code> method",
125125
"content": {
126126
"*/*": {
127127
"schema": {
@@ -130,7 +130,7 @@
130130
}
131131
},
132132
"x-exception-class": [
133-
"test.org.springdoc.api.app163.exception.NoResultException"
133+
"test.org.springdoc.api.app163.exception.NonUniqueResultException"
134134
]
135135
},
136136
"default": {
@@ -166,9 +166,6 @@
166166
}
167167
],
168168
"responses": {
169-
"400": {
170-
"description": "API Response 400 for #findStartsBy(prefix)"
171-
},
172169
"404": {
173170
"description": "the <code>@throws NoResultException</code> javadoc for the <code>#findStartsBy(String)</code> method",
174171
"content": {
@@ -182,6 +179,9 @@
182179
"test.org.springdoc.api.app163.exception.NoResultException"
183180
]
184181
},
182+
"400": {
183+
"description": "API Response 400 for #findStartsBy(prefix)"
184+
},
185185
"200": {
186186
"description": "API Response 200 for #findStartsBy(prefix)",
187187
"content": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app199;
2+
3+
import org.springframework.web.bind.annotation.ControllerAdvice;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.bind.annotation.ResponseBody;
6+
import org.springframework.web.bind.annotation.ResponseStatus;
7+
8+
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
9+
10+
@ControllerAdvice
11+
public class CustomExceptionHandler {
12+
13+
static public class MyInternalException extends Exception {}
14+
15+
@ResponseStatus( value = INTERNAL_SERVER_ERROR )
16+
@ExceptionHandler( MyInternalException.class )
17+
@ResponseBody
18+
public ErrorDto handleMyInternalException( final MyInternalException ex ) {
19+
return new ErrorDto( ex.getMessage() );
20+
}
21+
22+
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.v30.app199;
2+
3+
public class ErrorDto {
4+
private String message;
5+
6+
public ErrorDto( final String message ) {
7+
this.message = message;
8+
}
9+
10+
public String getMessage() { return message; }
11+
public void setMessage( final String message ) { this.message = message; }
12+
}

0 commit comments

Comments
 (0)