Skip to content

Commit e7483a8

Browse files
author
bnasslahsen
committed
Improve support of Webflux with Functional Endpoints
1 parent 3f8e941 commit e7483a8

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ public static void buildContentFromDoc(Components components, ApiResponses apiRe
167167
if (apiResponsesOp.containsKey(apiResponseAnnotations.responseCode())) {
168168
// Merge with the existing content
169169
Content existingContent = apiResponsesOp.get(apiResponseAnnotations.responseCode()).getContent();
170-
if (optionalContent.isPresent() && existingContent != null) {
170+
if (optionalContent.isPresent()) {
171171
Content newContent = optionalContent.get();
172-
if (methodAttributes.isMethodOverloaded()) {
172+
if (methodAttributes.isMethodOverloaded() && existingContent != null) {
173173
Arrays.stream(methodAttributes.getMethodProduces()).filter(mediaTypeStr -> (newContent.get(mediaTypeStr) != null)).forEach(mediaTypeStr -> mergeSchema(existingContent, newContent.get(mediaTypeStr).getSchema(), mediaTypeStr));
174174
apiResponse.content(existingContent);
175175
}

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,7 @@ private Optional<ApiResponses> getApiResponses(
295295
setDescription(response, apiResponseObject);
296296
setExtensions(response, apiResponseObject);
297297

298-
if (apiResponsesOp == null)
299-
SpringDocAnnotationsUtils.getContent(response.content(),
300-
classProduces == null ? new String[0] : classProduces,
301-
methodProduces == null ? new String[0] : methodProduces, null, components, null)
302-
.ifPresent(apiResponseObject::content);
303-
else
304-
GenericResponseBuilder.buildContentFromDoc(components, apiResponsesOp, methodAttributes, response, apiResponseObject);
298+
buildResponseContent(methodAttributes, components, classProduces, methodProduces, apiResponsesOp, response, apiResponseObject);
305299

306300
AnnotationsUtils.getHeaders(response.headers(), null).ifPresent(apiResponseObject::headers);
307301
// Make schema as string if empty
@@ -320,6 +314,16 @@ private Optional<ApiResponses> getApiResponses(
320314
return Optional.of(apiResponsesObject);
321315
}
322316

317+
private void buildResponseContent(MethodAttributes methodAttributes, Components components, String[] classProduces, String[] methodProduces, ApiResponses apiResponsesOp, io.swagger.v3.oas.annotations.responses.ApiResponse response, ApiResponse apiResponseObject) {
318+
if (apiResponsesOp == null)
319+
SpringDocAnnotationsUtils.getContent(response.content(),
320+
classProduces == null ? new String[0] : classProduces,
321+
methodProduces == null ? new String[0] : methodProduces, null, components, null)
322+
.ifPresent(apiResponseObject::content);
323+
else
324+
GenericResponseBuilder.buildContentFromDoc(components, apiResponsesOp, methodAttributes, response, apiResponseObject);
325+
}
326+
323327
private boolean isResponseObject(ApiResponse apiResponseObject) {
324328
return StringUtils.isNotBlank(apiResponseObject.getDescription()) || apiResponseObject.getContent() != null
325329
|| apiResponseObject.getHeaders() != null;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public Optional<RequestBody> buildRequestBodyFromDoc(
8484
if (isEmpty)
8585
return Optional.empty();
8686

87+
buildResquestBodyContent(requestBody, requestBodyOp, methodAttributes, components, jsonViewAnnotation, classConsumes, methodConsumes, requestBodyObject);
88+
89+
return Optional.of(requestBodyObject);
90+
}
91+
92+
private void buildResquestBodyContent(io.swagger.v3.oas.annotations.parameters.RequestBody requestBody, RequestBody requestBodyOp, MethodAttributes methodAttributes, Components components, JsonView jsonViewAnnotation, String[] classConsumes, String[] methodConsumes, RequestBody requestBodyObject) {
8793
Optional<Content> optionalContent = AnnotationsUtils
8894
.getContent(requestBody.content(), classConsumes == null ? new String[0] : classConsumes,
8995
methodConsumes == null ? new String[0] : methodConsumes, null, components, jsonViewAnnotation);
@@ -101,8 +107,6 @@ public Optional<RequestBody> buildRequestBodyFromDoc(
101107
requestBodyObject.content(newContent);
102108
}
103109
}
104-
105-
return Optional.of(requestBodyObject);
106110
}
107111

108112
public Optional<RequestBody> buildRequestBodyFromDoc(io.swagger.v3.oas.annotations.parameters.RequestBody requestBody,

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app73/QuoteRouter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON;
4343
import static org.springframework.http.MediaType.APPLICATION_STREAM_JSON_VALUE;
4444
import static org.springframework.http.MediaType.TEXT_PLAIN;
45+
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;
4546
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
4647
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
4748
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
@@ -50,8 +51,11 @@
5051
@Configuration
5152
public class QuoteRouter {
5253

53-
@RouterOperations({ @RouterOperation(path = "/hello", operation = @Operation(operationId = "hello", responses = @ApiResponse(responseCode = "200"))),
54-
@RouterOperation(path = "/echo", operation = @Operation(operationId = "echo", requestBody = @RequestBody(content = @Content(schema = @Schema(type = "string"))),
54+
@RouterOperations({
55+
@RouterOperation(path = "/hello", operation = @Operation(operationId = "hello", responses = @ApiResponse(responseCode = "200"))),
56+
@RouterOperation(path = "/echo", produces = TEXT_PLAIN_VALUE, operation = @Operation(operationId = "echo", requestBody = @RequestBody(content = @Content(schema = @Schema(type = "string"))),
57+
responses = @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(type = "string"))))),
58+
@RouterOperation(path = "/echo",produces = APPLICATION_JSON_VALUE, operation = @Operation(operationId = "echo", requestBody = @RequestBody(content = @Content(schema = @Schema(type = "string"))),
5559
responses = @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(type = "string"))))),
5660
@RouterOperation(path = "/quotes", produces = APPLICATION_JSON_VALUE, operation = @Operation(operationId = "fetchQuotes", parameters = @Parameter(name = "size", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
5761
responses = @ApiResponse(responseCode = "200", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Quote.class)))))),
@@ -62,6 +66,7 @@ public RouterFunction<ServerResponse> route(QuoteHandler quoteHandler) {
6266
return RouterFunctions
6367
.route(GET("/hello").and(accept(TEXT_PLAIN)), quoteHandler::hello)
6468
.andRoute(POST("/echo").and(accept(TEXT_PLAIN).and(contentType(TEXT_PLAIN))), quoteHandler::echo)
69+
.andRoute(POST("/echo").and(accept(APPLICATION_JSON).and(contentType(APPLICATION_JSON))), quoteHandler::echo)
6570
.andRoute(GET("/quotes").and(accept(APPLICATION_JSON)), quoteHandler::fetchQuotes)
6671
.andRoute(GET("/quotes").and(accept(APPLICATION_STREAM_JSON)), quoteHandler::streamQuotes);
6772
}

springdoc-openapi-webflux-core/src/test/resources/results/app73.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323
},
2424
"/echo": {
2525
"post": {
26-
"operationId": "echo",
26+
"operationId": "echo_1",
2727
"requestBody": {
2828
"content": {
2929
"text/plain": {
3030
"schema": {
3131
"type": "string"
3232
}
33+
},
34+
"application/json": {
35+
"schema": {
36+
"type": "string"
37+
}
3338
}
3439
}
3540
},
@@ -41,6 +46,11 @@
4146
"schema": {
4247
"type": "string"
4348
}
49+
},
50+
"application/json": {
51+
"schema": {
52+
"type": "string"
53+
}
4454
}
4555
}
4656
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.List;
2222

2323
import io.swagger.v3.oas.annotations.Operation;
24-
import io.swagger.v3.oas.annotations.media.Content;
2524
import io.swagger.v3.oas.annotations.responses.ApiResponse;
2625

2726
import org.springframework.http.MediaType;
@@ -32,7 +31,7 @@
3231
public class HelloController {
3332

3433
@Operation(description = "Some operation", responses = { @ApiResponse(responseCode = "401") })
35-
@ApiResponse(responseCode = "401", content = @Content())
34+
//@ApiResponse(responseCode = "401", content = @Content())
3635
@GetMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE)
3736
List<String> list() {
3837
return null;

0 commit comments

Comments
 (0)