Skip to content

Commit 37d8574

Browse files
committed
Change report: #1783
1 parent 8e2b99a commit 37d8574

File tree

5 files changed

+440
-8
lines changed

5 files changed

+440
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,11 @@ private void buildGenericApiResponses(Components components, MethodParameter met
365365
// Use response parameters with no description filled - No documentation
366366
// available
367367
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), Objects.requireNonNull(methodParameter.getMethod()).getClass(), true);
368-
ApiResponse apiResponse = methodAttributes.getGenericMapResponse().containsKey(httpCode) ? methodAttributes.getGenericMapResponse().get(httpCode)
369-
: new ApiResponse();
370-
if (httpCode != null)
368+
if (Objects.nonNull(httpCode)) {
369+
ApiResponse apiResponse = methodAttributes.getGenericMapResponse().containsKey(httpCode) ? methodAttributes.getGenericMapResponse().get(httpCode)
370+
: new ApiResponse();
371371
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, apiResponse, true);
372+
}
372373
}
373374
}
374375

@@ -393,14 +394,18 @@ private void buildApiResponses(Components components, MethodParameter methodPara
393394
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, apiResponse, false);
394395
}
395396
}
397+
if (AnnotatedElementUtils.hasAnnotation(methodParameter.getMethod(), ResponseStatus.class)) {
398+
// Handles the case with @ResponseStatus, if the specified response is not already handled explicitly
399+
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), Objects.requireNonNull(methodParameter.getMethod()).getClass(), false);
400+
if (Objects.nonNull(httpCode) && !apiResponsesOp.containsKey(httpCode) && !apiResponsesOp.containsKey(ApiResponses.DEFAULT)) {
401+
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, new ApiResponse(), false);
402+
}
403+
}
396404
}
397405
else {
398-
// Use response parameters with no description filled - No documentation
399-
// available
400406
String httpCode = evaluateResponseStatus(methodParameter.getMethod(), Objects.requireNonNull(methodParameter.getMethod()).getClass(), false);
401-
ApiResponse apiResponse = new ApiResponse();
402-
if (httpCode != null)
403-
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, apiResponse, false);
407+
if (Objects.nonNull(httpCode))
408+
buildApiResponses(components, methodParameter, apiResponsesOp, methodAttributes, httpCode, new ApiResponse(), false);
404409
}
405410
}
406411

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.app192;
24+
25+
import io.swagger.v3.oas.annotations.Operation;
26+
import io.swagger.v3.oas.annotations.media.Content;
27+
import io.swagger.v3.oas.annotations.media.Schema;
28+
import io.swagger.v3.oas.annotations.parameters.RequestBody;
29+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
30+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
31+
32+
import org.springframework.http.HttpStatus;
33+
import org.springframework.http.MediaType;
34+
import org.springframework.http.ResponseEntity;
35+
import org.springframework.web.bind.annotation.GetMapping;
36+
import org.springframework.web.bind.annotation.PostMapping;
37+
import org.springframework.web.bind.annotation.ResponseStatus;
38+
import org.springframework.web.bind.annotation.RestController;
39+
40+
@RestController
41+
public class HelloController {
42+
@Operation(description = "Adds 200 as api response, because there are nothing defined to get another response")
43+
@RequestBody(description = "test value", required = true, content = @Content(schema = @Schema(implementation = String.class)))
44+
@PostMapping(value = "/postWithoutAnyResponse", produces = MediaType.APPLICATION_JSON_VALUE)
45+
public void postWithoutAnyResponse(String test) {}
46+
47+
@Operation(description = "Adds 201 as api response, because it defined by @ResponseStatus")
48+
@PostMapping(value = "/postWithResponseStatusOnly", produces = MediaType.APPLICATION_JSON_VALUE)
49+
@ResponseStatus(value = HttpStatus.CREATED)
50+
public ResponseEntity<String> postWithResponseStatusOnly() {
51+
return ResponseEntity.ok("hello");
52+
}
53+
54+
@Operation(description = "Adds 200 as additional api response, because it defined by @ResponseStatus",
55+
responses = {
56+
@ApiResponse(responseCode = "422", description = "Test")
57+
})
58+
@ApiResponses({
59+
@ApiResponse(responseCode = "409", description = "Test 2")
60+
})
61+
@GetMapping(value = "/withResponseStatus", produces = MediaType.APPLICATION_JSON_VALUE)
62+
@ResponseStatus(value = HttpStatus.OK)
63+
public ResponseEntity<String> withResponseStatus() {
64+
return ResponseEntity.ok("hello");
65+
}
66+
67+
@Operation(description = "Adds 200 as api response, because it defined by @ResponseStatus")
68+
@GetMapping(value = "/withResponseStatusOnly", produces = MediaType.APPLICATION_JSON_VALUE)
69+
@ResponseStatus(value = HttpStatus.OK)
70+
public ResponseEntity<String> withResponseStatusOnly() {
71+
return ResponseEntity.ok("hello");
72+
}
73+
74+
@Operation(description = "Doesn't creates the default 200 Response, because there are explicit declared api responses." +
75+
"This test ensures that the current default handling is not changed, because otherwise very many tests will fail.",
76+
responses = {
77+
@ApiResponse(responseCode = "422", description = "Test")
78+
})
79+
@ApiResponses({
80+
@ApiResponse(responseCode = "409", description = "Test 2") })
81+
@GetMapping(value = "/withoutResponseStatus", produces = MediaType.APPLICATION_JSON_VALUE)
82+
public ResponseEntity<String> withoutResponseStatus() {
83+
return ResponseEntity.ok("hello");
84+
}
85+
86+
87+
@Operation(description = "Results in the default handling like before")
88+
@GetMapping(value = "/withoutAnyResponseInformation", produces = MediaType.APPLICATION_JSON_VALUE)
89+
public ResponseEntity<String> withoutAnyResponseInformation() {
90+
return ResponseEntity.ok("hello");
91+
}
92+
93+
@Operation(description = "Overwrites the 200 @ResponseStatus-Information by the explicit declared @ApiResponse",
94+
responses = {
95+
@ApiResponse(responseCode = "200", description = "Test2", content = @Content),
96+
@ApiResponse(responseCode = "422", description = "Test")
97+
})
98+
@ApiResponses({
99+
@ApiResponse(responseCode = "409", description = "Test 2")
100+
})
101+
@GetMapping(value = "/overwrite200InOperation", produces = MediaType.APPLICATION_JSON_VALUE)
102+
@ResponseStatus(value = HttpStatus.OK)
103+
public ResponseEntity<String> overwrite200InOperation() {
104+
return ResponseEntity.ok("hello");
105+
}
106+
107+
@Operation(description = "Overwrites the 200 @ResponseStatus-Information by the explicit declared @ApiResponse",
108+
responses = {
109+
@ApiResponse(responseCode = "422", description = "Test")
110+
})
111+
@ApiResponses({
112+
@ApiResponse(responseCode = "200", description = "Test2", content = @Content),
113+
@ApiResponse(responseCode = "409", description = "Test 2")
114+
})
115+
@GetMapping(value = "/overwrite200InDoc", produces = MediaType.APPLICATION_JSON_VALUE)
116+
@ResponseStatus(value = HttpStatus.OK)
117+
public ResponseEntity<String> overwrite200InDoc() {
118+
return ResponseEntity.ok("hello");
119+
}
120+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.app192;
24+
25+
26+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
27+
28+
29+
public class SpringDocApp192Test extends AbstractSpringDocV30Test { }
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.app192;
24+
25+
import org.springframework.boot.SpringApplication;
26+
import org.springframework.boot.autoconfigure.SpringBootApplication;
27+
28+
@SpringBootApplication
29+
public class SpringDocTestApp {
30+
31+
public static void main(String[] args) {
32+
SpringApplication.run(SpringDocTestApp.class, args);
33+
}
34+
}

0 commit comments

Comments
 (0)