Skip to content

Commit 9f8e2f6

Browse files
committed
handle request body
1 parent 4d8f912 commit 9f8e2f6

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/main/groovy/com/github/hauner/openapi/spring/writer/MethodWriter.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.github.hauner.openapi.spring.writer
1818

1919
import com.github.hauner.openapi.spring.model.Endpoint
20+
import com.github.hauner.openapi.spring.model.RequestBody
2021
import com.github.hauner.openapi.spring.model.parameters.Parameter
2122
import com.github.hauner.openapi.support.Identifier
2223

@@ -39,6 +40,11 @@ class MethodWriter {
3940
mapping += "("
4041
mapping += 'path = ' + quote(endpoint.path)
4142

43+
if (!endpoint.requestBodies.empty) {
44+
mapping += ", "
45+
mapping += 'consumes = {' + quote(endpoint.requestBody.contentType) + '}'
46+
}
47+
4248
if (!endpoint.response.empty) {
4349
mapping += ", "
4450
mapping += 'produces = {' + quote(endpoint.response.contentType) + '}'
@@ -68,6 +74,17 @@ class MethodWriter {
6874
param
6975
}
7076

77+
private String createRequestBodyAnnotation (RequestBody requestBody) {
78+
String param = "${requestBody.annotation}"
79+
80+
// required is default, so add required only if the parameter is not required
81+
if (!requestBody.required) {
82+
param += '(required = false)'
83+
}
84+
85+
param
86+
}
87+
7188
private String createMethodName (Endpoint endpoint) {
7289
def tokens = endpoint.path.tokenize ('/')
7390
tokens = tokens.collect { Identifier.fromJson (it).capitalize () }
@@ -86,6 +103,12 @@ class MethodWriter {
86103

87104
}
88105

106+
if (!endpoint.requestBodies.empty) {
107+
def body = endpoint.requestBody
108+
def param = "${createRequestBodyAnnotation(body)} ${body.requestBodyType.name} body"
109+
ps.add (param)
110+
}
111+
89112
ps.join (', ')
90113
}
91114

src/test/groovy/com/github/hauner/openapi/spring/writer/MethodWriterSpec.groovy

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

1919
import com.github.hauner.openapi.spring.model.Endpoint
2020
import com.github.hauner.openapi.spring.model.HttpMethod
21+
import com.github.hauner.openapi.spring.model.RequestBody
2122
import com.github.hauner.openapi.spring.model.Response
2223
import com.github.hauner.openapi.spring.model.datatypes.BooleanDataType
2324
import com.github.hauner.openapi.spring.model.datatypes.CollectionDataType
@@ -322,4 +323,47 @@ class MethodWriterSpec extends Specification {
322323
"""
323324
}
324325
326+
void "writes required request body parameter" () {
327+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.POST, responses: [
328+
new Response (contentType: 'application/json', responseType: new NoneDataType())
329+
], requestBodies: [
330+
new RequestBody(
331+
contentType: 'application/json',
332+
requestBodyType: new ObjectDataType (type: 'FooRequestBody',
333+
properties: ['foo': new StringDataType ()] as LinkedHashMap),
334+
required: true)
335+
])
336+
337+
when:
338+
writer.write (target, endpoint)
339+
340+
then:
341+
target.toString () == """\
342+
@PostMapping(path = "${endpoint.path}", consumes = {"application/json"})
343+
ResponseEntity<void> postFoo(@RequestBody FooRequestBody body);
344+
"""
345+
}
346+
347+
void "writes optional request body parameter" () {
348+
def endpoint = new Endpoint (path: '/foo', method: HttpMethod.POST, responses: [
349+
new Response (contentType: 'application/json', responseType: new NoneDataType())
350+
], requestBodies: [
351+
new RequestBody(
352+
contentType: 'application/json',
353+
requestBodyType: new ObjectDataType (
354+
type: 'FooRequestBody',
355+
properties: ['foo': new StringDataType ()] as LinkedHashMap),
356+
required: false)
357+
])
358+
359+
when:
360+
writer.write (target, endpoint)
361+
362+
then:
363+
target.toString () == """\
364+
@PostMapping(path = "${endpoint.path}", consumes = {"application/json"})
365+
ResponseEntity<void> postFoo(@RequestBody(required = false) FooRequestBody body);
366+
"""
367+
}
368+
325369
}

0 commit comments

Comments
 (0)