@@ -18,6 +18,7 @@ package com.github.hauner.openapi.spring.writer
1818
1919import com.github.hauner.openapi.spring.model.Endpoint
2020import com.github.hauner.openapi.spring.model.HttpMethod
21+ import com.github.hauner.openapi.spring.model.RequestBody
2122import com.github.hauner.openapi.spring.model.Response
2223import com.github.hauner.openapi.spring.model.datatypes.BooleanDataType
2324import 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