Skip to content

Commit 2592600

Browse files
committed
Spring Data Rest: Wrong schema for request body in association controllers. Fixes #1082
1 parent 8adb767 commit 2592600

File tree

7 files changed

+42
-13
lines changed

7 files changed

+42
-13
lines changed

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/core/DataRestRequestService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.swagger.v3.oas.models.Components;
3535
import io.swagger.v3.oas.models.OpenAPI;
3636
import io.swagger.v3.oas.models.Operation;
37+
import io.swagger.v3.oas.models.media.Content;
3738
import io.swagger.v3.oas.models.media.Schema;
3839
import io.swagger.v3.oas.models.media.StringSchema;
3940
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -59,6 +60,8 @@
5960
import org.springframework.web.bind.annotation.RequestMethod;
6061
import org.springframework.web.method.HandlerMethod;
6162

63+
import static org.springdoc.data.rest.utils.SpringDocDataRestUtils.buildTextUriContent;
64+
6265
/**
6366
* The type Data rest request builder.
6467
* @author bnasslahsen
@@ -216,6 +219,8 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
216219
parameterInfo, requestBodyInfo);
217220
requestBuilder.applyBeanValidatorAnnotations(requestBodyInfo.getRequestBody(), parameterAnnotations, methodParameter.isOptional());
218221
operation.setRequestBody(requestBodyInfo.getRequestBody());
222+
Content content = operation.getRequestBody().getContent();
223+
buildTextUriContent(content);
219224
operation.getRequestBody().setRequired(true);
220225
}
221226
}

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/core/DataRestResponseService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import io.swagger.v3.oas.models.OpenAPI;
3636
import io.swagger.v3.oas.models.Operation;
3737
import io.swagger.v3.oas.models.media.Content;
38-
import io.swagger.v3.oas.models.media.MediaType;
39-
import io.swagger.v3.oas.models.media.StringSchema;
4038
import io.swagger.v3.oas.models.responses.ApiResponse;
4139
import io.swagger.v3.oas.models.responses.ApiResponses;
4240
import org.springdoc.core.GenericResponseService;
@@ -47,7 +45,6 @@
4745
import org.springframework.core.ResolvableType;
4846
import org.springframework.data.rest.core.mapping.MethodResourceMapping;
4947
import org.springframework.data.rest.core.mapping.ResourceMetadata;
50-
import org.springframework.data.rest.webmvc.RestMediaTypes;
5148
import org.springframework.hateoas.CollectionModel;
5249
import org.springframework.hateoas.EntityModel;
5350
import org.springframework.hateoas.Link;
@@ -61,6 +58,10 @@
6158
import org.springframework.web.bind.annotation.RequestMethod;
6259
import org.springframework.web.method.HandlerMethod;
6360

61+
import static org.springdoc.data.rest.utils.SpringDocDataRestUtils.buildTextUriContent;
62+
63+
;
64+
6465
/**
6566
* The type Data rest response builder.
6667
* @author bnasslahsen
@@ -138,8 +139,7 @@ public void buildEntityResponse(Operation operation, HandlerMethod handlerMethod
138139
ApiResponses apiResponses = new ApiResponses();
139140
ApiResponse apiResponse = new ApiResponse();
140141
Content content = genericResponseService.buildContent(openAPI.getComponents(), methodParameterReturn.getParameterAnnotations(), methodAttributes.getMethodProduces(), null, returnType);
141-
if (content.get(RestMediaTypes.TEXT_URI_LIST_VALUE) != null)
142-
content.put(RestMediaTypes.TEXT_URI_LIST_VALUE, new MediaType().schema(new StringSchema()));
142+
buildTextUriContent(content);
143143
apiResponse.setContent(content);
144144
addResponse(requestMethod, operationPath, apiResponses, apiResponse);
145145
operation.setResponses(apiResponses);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.springdoc.data.rest.utils;
2+
3+
import io.swagger.v3.oas.models.media.Content;
4+
import io.swagger.v3.oas.models.media.MediaType;
5+
import io.swagger.v3.oas.models.media.StringSchema;
6+
7+
import org.springframework.data.rest.webmvc.RestMediaTypes;
8+
9+
/**
10+
* The interface Spring doc data rest utils.
11+
* @author bnasslashen
12+
*/
13+
public interface SpringDocDataRestUtils {
14+
15+
/**
16+
* Build text uri content.
17+
*
18+
* @param content the content
19+
*/
20+
static void buildTextUriContent(Content content) {
21+
if (content.get(RestMediaTypes.TEXT_URI_LIST_VALUE) != null)
22+
content.put(RestMediaTypes.TEXT_URI_LIST_VALUE, new MediaType().schema(new StringSchema()));
23+
}
24+
}

springdoc-openapi-data-rest/src/test/resources/results/app10.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@
324324
},
325325
"text/uri-list": {
326326
"schema": {
327-
"$ref": "#/components/schemas/CollectionModelObject"
327+
"type": "string"
328328
}
329329
}
330330
},
@@ -411,7 +411,7 @@
411411
},
412412
"text/uri-list": {
413413
"schema": {
414-
"$ref": "#/components/schemas/CollectionModelObject"
414+
"type": "string"
415415
}
416416
}
417417
},

springdoc-openapi-data-rest/src/test/resources/results/app16.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@
369369
},
370370
"text/uri-list": {
371371
"schema": {
372-
"$ref": "#/components/schemas/CollectionModelObject"
372+
"type": "string"
373373
}
374374
}
375375
},
@@ -466,7 +466,7 @@
466466
},
467467
"text/uri-list": {
468468
"schema": {
469-
"$ref": "#/components/schemas/CollectionModelObject"
469+
"type": "string"
470470
}
471471
}
472472
},

springdoc-openapi-data-rest/src/test/resources/results/app17.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@
657657
},
658658
"text/uri-list": {
659659
"schema": {
660-
"$ref": "#/components/schemas/CollectionModelObject"
660+
"type": "string"
661661
}
662662
}
663663
},
@@ -744,7 +744,7 @@
744744
},
745745
"text/uri-list": {
746746
"schema": {
747-
"$ref": "#/components/schemas/CollectionModelObject"
747+
"type": "string"
748748
}
749749
}
750750
},

springdoc-openapi-data-rest/src/test/resources/results/app23.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@
505505
},
506506
"text/uri-list": {
507507
"schema": {
508-
"$ref": "#/components/schemas/CollectionModelObject"
508+
"type": "string"
509509
}
510510
}
511511
},
@@ -592,7 +592,7 @@
592592
},
593593
"text/uri-list": {
594594
"schema": {
595-
"$ref": "#/components/schemas/CollectionModelObject"
595+
"type": "string"
596596
}
597597
}
598598
},

0 commit comments

Comments
 (0)