Skip to content

Commit 6f3cd50

Browse files
committed
handle request body
1 parent 79e5fce commit 6f3cd50

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.github.hauner.openapi.spring.converter
1818

1919
import com.github.hauner.openapi.spring.model.Api
2020
import com.github.hauner.openapi.spring.model.Endpoint
21+
import com.github.hauner.openapi.spring.model.RequestBody
2122
import com.github.hauner.openapi.spring.model.parameters.CookieParameter
2223
import com.github.hauner.openapi.spring.model.parameters.HeaderParameter
2324
import com.github.hauner.openapi.spring.model.parameters.Parameter as ModelParameter
@@ -88,6 +89,26 @@ class ApiConverter {
8889
ep.parameters.addAll (createParameter(parameter, target, resolver))
8990
}
9091

92+
if (httpOperation.requestBody != null) {
93+
def required = httpOperation.requestBody.required != null ?: false
94+
httpOperation.requestBody.content.each { Map.Entry<String, MediaType> requestBodyEntry ->
95+
def contentType = requestBodyEntry.key
96+
def requestBody = requestBodyEntry.value
97+
98+
def info = new SchemaInfo (requestBody.schema, getInlineTypeName (path))
99+
info.resolver = resolver
100+
101+
DataType dataType = dataTypeConverter.convert (info, target.models)
102+
103+
def body = new RequestBody(
104+
contentType: contentType,
105+
requestBodyType: dataType,
106+
required: required)
107+
108+
ep.requestBodies.add (body)
109+
}
110+
}
111+
91112
httpOperation.responses.each { Map.Entry<String, ApiResponse> responseEntry ->
92113
def httpStatus = responseEntry.key
93114
def httpResponse = responseEntry.value
@@ -136,6 +157,10 @@ class ApiConverter {
136157
}
137158
}
138159

160+
private String getInlineTypeName (String path) {
161+
StringUtil.toCamelCase (path.substring (1)) + 'RequestBody'
162+
}
163+
139164
private String getInlineResponseName (String path, String httpStatus) {
140165
StringUtil.toCamelCase (path.substring (1)) + 'Response' + httpStatus
141166
}
@@ -189,4 +214,5 @@ class ApiConverter {
189214
private boolean hasTags (op) {
190215
op.tags && !op.tags.empty
191216
}
217+
192218
}

src/main/groovy/com/github/hauner/openapi/spring/model/Endpoint.groovy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ class Endpoint {
2727
String path
2828
HttpMethod method
2929

30-
List<Response> responses = []
3130
List<Parameter> parameters = []
31+
List<RequestBody> requestBodies = []
32+
List<Response> responses = []
33+
34+
RequestBody getRequestBody () {
35+
requestBodies.first ()
36+
}
3237

3338
Response getResponse () {
3439
responses.first ()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2019 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.spring.model
18+
19+
import com.github.hauner.openapi.spring.model.datatypes.DataType
20+
21+
/**
22+
* Endpoint request body properties.
23+
*
24+
* @author Martin Hauner
25+
*/
26+
class RequestBody {
27+
28+
String contentType
29+
DataType requestBodyType
30+
boolean required
31+
32+
Set<String> getImports () {
33+
requestBodyType.imports
34+
}
35+
36+
String getAnnotationName () {
37+
"RequestBody"
38+
}
39+
40+
String getAnnotationWithPackage () {
41+
"org.springframework.web.bind.annotation.${annotationName}"
42+
}
43+
44+
String getAnnotation () {
45+
"@${annotationName}"
46+
}
47+
48+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2019 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.spring.converter
18+
19+
import spock.lang.Specification
20+
21+
import static com.github.hauner.openapi.spring.support.OpenApiParser.parse
22+
23+
class ApiConverterRequestBodySpec extends Specification {
24+
25+
void "converts request body parameter"() {
26+
def openApi = parse (
27+
"""\
28+
openapi: 3.0.2
29+
info:
30+
title: test request body parameter
31+
version: 1.0.0
32+
33+
paths:
34+
/endpoint:
35+
36+
get:
37+
tags:
38+
- endpoint
39+
requestBody:
40+
content:
41+
application/json:
42+
schema:
43+
type: object
44+
properties:
45+
foo:
46+
type: string
47+
responses:
48+
'204':
49+
description: empty
50+
""")
51+
52+
when:
53+
def api = new ApiConverter ().convert (openApi)
54+
55+
then:
56+
def itf = api.interfaces.first ()
57+
def ep = itf.endpoints.first ()
58+
def body = ep.requestBodies.first ()
59+
body.contentType == 'application/json'
60+
body.requestBodyType.type == 'EndpointRequestBody'
61+
!body.required
62+
body.annotation == '@RequestBody'
63+
body.annotationWithPackage == 'org.springframework.web.bind.annotation.RequestBody'
64+
}
65+
66+
}

0 commit comments

Comments
 (0)