Skip to content

Commit a3553d3

Browse files
author
bnasslahsen
committed
This resolved issus is supported by Kotlin?. Fixes #889
1 parent 6e252c5 commit a3553d3

File tree

8 files changed

+201
-15
lines changed

8 files changed

+201
-15
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/fn/AbstractRouterFunctionVisitor.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public class AbstractRouterFunctionVisitor {
6767
protected boolean isOr;
6868

6969
/**
70-
* The Is and.
70+
* The Is nested.
7171
*/
72-
protected boolean isAnd;
72+
protected boolean isNested;
7373

7474
/**
7575
* The Router function data.
@@ -93,10 +93,10 @@ public void method(Set<HttpMethod> methods) {
9393
public void path(String pattern) {
9494
if (routerFunctionData != null)
9595
routerFunctionData.setPath(pattern);
96-
else if (isAnd)
97-
nestedAndPaths.add(pattern);
9896
else if (isOr)
9997
nestedOrPaths.add(pattern);
98+
else if (isNested)
99+
nestedAndPaths.add(pattern);
100100
}
101101

102102
/**
@@ -158,7 +158,7 @@ public void param(String name, String value) {
158158
* Start and.
159159
*/
160160
public void startAnd() {
161-
isAnd = true;
161+
// Not yet needed
162162
}
163163

164164
/**
@@ -210,25 +210,34 @@ public void endNegate() {
210210
// Not yet needed
211211
}
212212

213+
/**
214+
* Compute nested.
215+
*/
213216
protected void computeNested() {
214217
if (!nestedAndPaths.isEmpty()) {
215218
String nestedPath = String.join(StringUtils.EMPTY, nestedAndPaths);
216-
routerFunctionDatas.forEach(existingRouterFunctionData -> existingRouterFunctionData.setPath(nestedPath+existingRouterFunctionData.getPath()));
219+
routerFunctionDatas.forEach(existingRouterFunctionData -> existingRouterFunctionData.setPath(nestedPath + existingRouterFunctionData.getPath()));
220+
nestedAndPaths.clear();
217221
}
218222
if (!nestedOrPaths.isEmpty()) {
219223
List<RouterFunctionData> routerFunctionDatasClone = new ArrayList<>();
220224
for (RouterFunctionData functionData : routerFunctionDatas) {
221225
for (String nestedOrPath : nestedOrPaths) {
222-
RouterFunctionData routerFunctionDataClone = new RouterFunctionData(nestedOrPath +functionData.getPath(),functionData.getConsumes(),functionData.getProduces(), functionData.getHeaders(), functionData.getQueryParams(), functionData.getMethods());
226+
RouterFunctionData routerFunctionDataClone = new RouterFunctionData(nestedOrPath + functionData.getPath(), functionData.getConsumes(), functionData.getProduces(), functionData.getHeaders(), functionData.getQueryParams(), functionData.getMethods());
223227
routerFunctionDatasClone.add(routerFunctionDataClone);
224228
}
225229
}
226230
this.routerFunctionDatas = routerFunctionDatasClone;
231+
nestedAndPaths.clear();
227232
}
228-
if (!nestedAcceptHeaders.isEmpty())
233+
if (!nestedAcceptHeaders.isEmpty()){
229234
routerFunctionDatas.forEach(existingRouterFunctionData -> existingRouterFunctionData.addProduces(nestedAcceptHeaders));
230-
if (!nestedContentTypeHeaders.isEmpty())
235+
nestedAcceptHeaders.clear();
236+
}
237+
if (!nestedContentTypeHeaders.isEmpty()){
231238
routerFunctionDatas.forEach(existingRouterFunctionData -> existingRouterFunctionData.addConsumes(nestedContentTypeHeaders));
239+
nestedContentTypeHeaders.clear();
240+
}
232241
}
233242

234243
/**
@@ -275,5 +284,4 @@ private void calculateAccept(String value) {
275284
}
276285
}
277286

278-
279287
}

springdoc-openapi-common/src/main/java/org/springdoc/core/fn/RouterOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public void setOperationModel(io.swagger.v3.oas.models.Operation operationModel)
340340
@Override
341341
public int compareTo(RouterOperation routerOperation) {
342342
int result = path.compareTo(routerOperation.getPath());
343-
if (result == 0)
343+
if (result == 0 && !ArrayUtils.isEmpty(methods))
344344
result = methods[0].compareTo(routerOperation.getMethods()[0]);
345345
if (result == 0 && operationModel != null && routerOperation.getOperationModel() != null)
346346
result = operationModel.getOperationId().compareTo(routerOperation.getOperationModel().getOperationId());

springdoc-openapi-webflux-core/src/main/java/org/springdoc/webflux/core/visitor/RouterFunctionVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void route(RequestPredicate predicate, HandlerFunction<?> handlerFunction
4949

5050
@Override
5151
public void startNested(RequestPredicate predicate) {
52+
this.isNested = true;
5253
predicate.accept(this);
5354
}
5455

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app80/BookRouter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,20 @@ RouterFunction<?> routes3(BookRepository br) {
8888
.and(route(GET("/books").and(accept(MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN)), req -> ok().body(br.findAll(), Book.class)))
8989
.andRoute(GET("/books/{author}"), req -> ok().body(br.findByAuthor(req.pathVariable("author")), Book.class)));
9090
}
91+
92+
@Bean
93+
@RouterOperations({
94+
@RouterOperation(path = "/test/greeter/greeter2/books", produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }, beanClass = BookRepository.class, beanMethod = "findAll"),
95+
@RouterOperation(path = "/test/greeter/greeter2/books", produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE }, beanClass = BookRepository.class, beanMethod = "findAll"),
96+
@RouterOperation(path = "/test/greeter/greeter2/books/{author}", beanClass = BookRepository.class, beanMethod = "findByAuthor",
97+
operation = @Operation(operationId = "findByAuthor"
98+
, parameters = { @Parameter(in = ParameterIn.PATH, name = "author") })) })
99+
RouterFunction<?> routes4(BookRepository br) {
100+
return
101+
nest(path("/test"),
102+
nest(path("/greeter").and(path("/greeter2")),
103+
route(GET("/books").and(accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)), req -> ok().body(br.findAll(), Book.class))
104+
.and(route(GET("/books").and(accept(MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN)), req -> ok().body(br.findAll(), Book.class)))
105+
.andRoute(GET("/books/{author}"), req -> ok().body(br.findByAuthor(req.pathVariable("author")), Book.class))));
106+
}
91107
}

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,78 @@
298298
}
299299
}
300300
}
301+
},
302+
"/test/greeter/greeter2/books": {
303+
"get": {
304+
"tags": [
305+
"book-repository"
306+
],
307+
"operationId": "findAll_2_3",
308+
"responses": {
309+
"200": {
310+
"description": "OK",
311+
"content": {
312+
"application/json": {
313+
"schema": {
314+
"type": "array",
315+
"items": {
316+
"$ref": "#/components/schemas/Book"
317+
}
318+
}
319+
},
320+
"application/xml": {
321+
"schema": {
322+
"type": "array",
323+
"items": {
324+
"$ref": "#/components/schemas/Book"
325+
}
326+
}
327+
},
328+
"text/plain": {
329+
"schema": {
330+
"type": "array",
331+
"items": {
332+
"$ref": "#/components/schemas/Book"
333+
}
334+
}
335+
}
336+
}
337+
}
338+
}
339+
}
340+
},
341+
"/test/greeter/greeter2/books/{author}": {
342+
"get": {
343+
"tags": [
344+
"book-repository"
345+
],
346+
"operationId": "findByAuthor_4",
347+
"parameters": [
348+
{
349+
"name": "author",
350+
"in": "path",
351+
"required": true,
352+
"schema": {
353+
"type": "string"
354+
}
355+
}
356+
],
357+
"responses": {
358+
"200": {
359+
"description": "OK",
360+
"content": {
361+
"*/*": {
362+
"schema": {
363+
"type": "array",
364+
"items": {
365+
"$ref": "#/components/schemas/Book"
366+
}
367+
}
368+
}
369+
}
370+
}
371+
}
372+
}
301373
}
302374
},
303375
"components": {

springdoc-openapi-webmvc-core/src/main/java/org/springdoc/webmvc/api/RouterFunctionProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public void unknown(RequestPredicate predicate) {
107107

108108
@Override
109109
public void startNested(RequestPredicate predicate) {
110+
this.isNested = true;
110111
predicate.accept(this);
111112
}
112113

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app135/BookRouter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,20 @@ RouterFunction<?> routes3(BookRepository br) {
8888
.and(route(RequestPredicates.GET("/books").and(RequestPredicates.accept(MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN)), req -> ok().body(br.findAll())))
8989
.andRoute(RequestPredicates.GET("/books/{author}"), req -> ok().body(br.findByAuthor(req.pathVariable("author")))));
9090
}
91+
92+
@Bean
93+
@RouterOperations({
94+
@RouterOperation(path = "/test/greeter/greeter2/books", produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }, beanClass = BookRepository.class, beanMethod = "findAll"),
95+
@RouterOperation(path = "/test/greeter/greeter2/books", produces = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE }, beanClass = BookRepository.class, beanMethod = "findAll"),
96+
@RouterOperation(path = "/test/greeter/greeter2/books/{author}", beanClass = BookRepository.class, beanMethod = "findByAuthor",
97+
operation = @Operation(operationId = "findByAuthor"
98+
, parameters = { @Parameter(in = ParameterIn.PATH, name = "author") })) })
99+
RouterFunction<?> routes4(BookRepository br) {
100+
return
101+
nest(RequestPredicates.path("/test"),
102+
nest(RequestPredicates.path("/greeter").and(RequestPredicates.path("/greeter2")),
103+
route(RequestPredicates.GET("/books").and(RequestPredicates.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)), req -> ok().body(br.findAll()))
104+
.and(route(RequestPredicates.GET("/books").and(RequestPredicates.accept(MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN)), req -> ok().body(br.findAll())))
105+
.andRoute(RequestPredicates.GET("/books/{author}"), req -> ok().body(br.findByAuthor(req.pathVariable("author"))))));
106+
}
91107
}

springdoc-openapi-webmvc-core/src/test/resources/results/app135.json

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
}
156156
}
157157
},
158-
"/greeter/books": {
158+
"/test/greeter/greeter2/books": {
159159
"get": {
160160
"tags": [
161161
"book-repository"
@@ -194,7 +194,7 @@
194194
}
195195
}
196196
},
197-
"/greeter/books/{author}": {
197+
"/test/greeter/greeter2/books/{author}": {
198198
"get": {
199199
"tags": [
200200
"book-repository"
@@ -227,7 +227,7 @@
227227
}
228228
}
229229
},
230-
"/greeter2/books": {
230+
"/greeter/books": {
231231
"get": {
232232
"tags": [
233233
"book-repository"
@@ -266,7 +266,7 @@
266266
}
267267
}
268268
},
269-
"/greeter2/books/{author}": {
269+
"/greeter/books/{author}": {
270270
"get": {
271271
"tags": [
272272
"book-repository"
@@ -298,6 +298,78 @@
298298
}
299299
}
300300
}
301+
},
302+
"/greeter2/books": {
303+
"get": {
304+
"tags": [
305+
"book-repository"
306+
],
307+
"operationId": "findAll_1_4",
308+
"responses": {
309+
"200": {
310+
"description": "OK",
311+
"content": {
312+
"application/json": {
313+
"schema": {
314+
"type": "array",
315+
"items": {
316+
"$ref": "#/components/schemas/Book"
317+
}
318+
}
319+
},
320+
"application/xml": {
321+
"schema": {
322+
"type": "array",
323+
"items": {
324+
"$ref": "#/components/schemas/Book"
325+
}
326+
}
327+
},
328+
"text/plain": {
329+
"schema": {
330+
"type": "array",
331+
"items": {
332+
"$ref": "#/components/schemas/Book"
333+
}
334+
}
335+
}
336+
}
337+
}
338+
}
339+
}
340+
},
341+
"/greeter2/books/{author}": {
342+
"get": {
343+
"tags": [
344+
"book-repository"
345+
],
346+
"operationId": "findByAuthor_4",
347+
"parameters": [
348+
{
349+
"name": "author",
350+
"in": "path",
351+
"required": true,
352+
"schema": {
353+
"type": "string"
354+
}
355+
}
356+
],
357+
"responses": {
358+
"200": {
359+
"description": "OK",
360+
"content": {
361+
"*/*": {
362+
"schema": {
363+
"type": "array",
364+
"items": {
365+
"$ref": "#/components/schemas/Book"
366+
}
367+
}
368+
}
369+
}
370+
}
371+
}
372+
}
301373
}
302374
},
303375
"components": {

0 commit comments

Comments
 (0)