Skip to content

Commit e78c9f6

Browse files
committed
Use correct annotation for optional request body
This adds `required=false` to the `@RequestBody` annotation when the OpenAPI definition has the body marked as not required.
1 parent 6c714dd commit e78c9f6

File tree

17 files changed

+59
-59
lines changed

17 files changed

+59
-59
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isBodyParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue={{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{/defaultValue}}{{/isContainer}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody {{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isListContainer}}Mono{{/isListContainer}}{{#isListContainer}}Flux{{/isListContainer}}<{{{baseType}}}>{{/reactive}} {{paramName}}{{/isBodyParam}}
1+
{{#isBodyParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue={{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{{defaultValue}}}{{^isString}}"{{/isString}}{{#isString}}{{#isEnum}}"{{/isEnum}}{{/isString}}{{/defaultValue}}{{/isContainer}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody{{^required}}(required=false){{/required}} {{^reactive}}{{{dataType}}}{{/reactive}}{{#reactive}}{{^isListContainer}}Mono{{/isListContainer}}{{#isListContainer}}Flux{{/isListContainer}}<{{{baseType}}}>{{/reactive}} {{paramName}}{{/isBodyParam}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isBodyParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody {{{dataType}}} {{paramName}}{{/isBodyParam}}
1+
{{#isBodyParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody{{^required}}(required=false){{/required}} {{{dataType}}} {{paramName}}{{/isBodyParam}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#isBodyParam}}{{#swaggerAnnotations}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{/swaggerAnnotations}} {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody {{paramName}}: {{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isListContainer}}{{>optionalDataType}}{{/isListContainer}}{{#isListContainer}}Flow<{{{baseType}}}>{{/isListContainer}}{{/reactive}}{{/isBodyParam}}
1+
{{#isBodyParam}}{{#swaggerAnnotations}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{/swaggerAnnotations}} {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody{{^required}}(required=false){{/required}} {{paramName}}: {{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isListContainer}}{{>optionalDataType}}{{/isListContainer}}{{#isListContainer}}Flow<{{{baseType}}}>{{/isListContainer}}{{/reactive}}{{/isBodyParam}}

samples/server/petstore/spring-mvc-j8-async/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ default CompletableFuture<ResponseEntity<Void>> createXmlItem(@ApiParam(value =
6464
@RequestMapping(value = "/fake/outer/boolean",
6565
produces = { "*/*" },
6666
method = RequestMethod.POST)
67-
default CompletableFuture<ResponseEntity<Boolean>> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody Boolean body) {
67+
default CompletableFuture<ResponseEntity<Boolean>> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody(required=false) Boolean body) {
6868
return CompletableFuture.completedFuture(new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED));
6969

7070
}
@@ -76,7 +76,7 @@ default CompletableFuture<ResponseEntity<Boolean>> fakeOuterBooleanSerialize(@Ap
7676
@RequestMapping(value = "/fake/outer/composite",
7777
produces = { "*/*" },
7878
method = RequestMethod.POST)
79-
default CompletableFuture<ResponseEntity<OuterComposite>> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody OuterComposite body) {
79+
default CompletableFuture<ResponseEntity<OuterComposite>> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody(required=false) OuterComposite body) {
8080
return CompletableFuture.supplyAsync(()-> {
8181
getRequest().ifPresent(request -> {
8282
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
@@ -99,7 +99,7 @@ default CompletableFuture<ResponseEntity<OuterComposite>> fakeOuterCompositeSeri
9999
@RequestMapping(value = "/fake/outer/number",
100100
produces = { "*/*" },
101101
method = RequestMethod.POST)
102-
default CompletableFuture<ResponseEntity<BigDecimal>> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody BigDecimal body) {
102+
default CompletableFuture<ResponseEntity<BigDecimal>> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody(required=false) BigDecimal body) {
103103
return CompletableFuture.completedFuture(new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED));
104104

105105
}
@@ -111,7 +111,7 @@ default CompletableFuture<ResponseEntity<BigDecimal>> fakeOuterNumberSerialize(@
111111
@RequestMapping(value = "/fake/outer/string",
112112
produces = { "*/*" },
113113
method = RequestMethod.POST)
114-
default CompletableFuture<ResponseEntity<String>> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody String body) {
114+
default CompletableFuture<ResponseEntity<String>> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody(required=false) String body) {
115115
return CompletableFuture.completedFuture(new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED));
116116

117117
}

samples/server/petstore/spring-mvc-j8-localdatetime/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ default ResponseEntity<Void> createXmlItem(@ApiParam(value = "XmlItem Body" ,req
6363
@RequestMapping(value = "/fake/outer/boolean",
6464
produces = { "*/*" },
6565
method = RequestMethod.POST)
66-
default ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody Boolean body) {
66+
default ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody(required=false) Boolean body) {
6767
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
6868

6969
}
@@ -75,7 +75,7 @@ default ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Inp
7575
@RequestMapping(value = "/fake/outer/composite",
7676
produces = { "*/*" },
7777
method = RequestMethod.POST)
78-
default ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody OuterComposite body) {
78+
default ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody(required=false) OuterComposite body) {
7979
getRequest().ifPresent(request -> {
8080
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
8181
if (mediaType.isCompatibleWith(MediaType.valueOf("*/*"))) {
@@ -96,7 +96,7 @@ default ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(val
9696
@RequestMapping(value = "/fake/outer/number",
9797
produces = { "*/*" },
9898
method = RequestMethod.POST)
99-
default ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody BigDecimal body) {
99+
default ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody(required=false) BigDecimal body) {
100100
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
101101

102102
}
@@ -108,7 +108,7 @@ default ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "I
108108
@RequestMapping(value = "/fake/outer/string",
109109
produces = { "*/*" },
110110
method = RequestMethod.POST)
111-
default ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody String body) {
111+
default ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody(required=false) String body) {
112112
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
113113

114114
}

samples/server/petstore/spring-mvc/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public interface FakeApi {
5252
@RequestMapping(value = "/fake/outer/boolean",
5353
produces = { "*/*" },
5454
method = RequestMethod.POST)
55-
ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody Boolean body);
55+
ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody(required=false) Boolean body);
5656

5757

5858
@ApiOperation(value = "", nickname = "fakeOuterCompositeSerialize", notes = "Test serialization of object with outer number type", response = OuterComposite.class, tags={ "fake", })
@@ -61,7 +61,7 @@ public interface FakeApi {
6161
@RequestMapping(value = "/fake/outer/composite",
6262
produces = { "*/*" },
6363
method = RequestMethod.POST)
64-
ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody OuterComposite body);
64+
ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody(required=false) OuterComposite body);
6565

6666

6767
@ApiOperation(value = "", nickname = "fakeOuterNumberSerialize", notes = "Test serialization of outer number types", response = BigDecimal.class, tags={ "fake", })
@@ -70,7 +70,7 @@ public interface FakeApi {
7070
@RequestMapping(value = "/fake/outer/number",
7171
produces = { "*/*" },
7272
method = RequestMethod.POST)
73-
ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody BigDecimal body);
73+
ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody(required=false) BigDecimal body);
7474

7575

7676
@ApiOperation(value = "", nickname = "fakeOuterStringSerialize", notes = "Test serialization of outer string types", response = String.class, tags={ "fake", })
@@ -79,7 +79,7 @@ public interface FakeApi {
7979
@RequestMapping(value = "/fake/outer/string",
8080
produces = { "*/*" },
8181
method = RequestMethod.POST)
82-
ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody String body);
82+
ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody(required=false) String body);
8383

8484

8585
@ApiOperation(value = "", nickname = "testBodyWithFileSchema", notes = "For this test, the body for this request much reference a schema named `File`.", tags={ "fake", })

samples/server/petstore/spring-mvc/src/main/java/org/openapitools/api/FakeApiController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public ResponseEntity<Void> createXmlItem(@ApiParam(value = "XmlItem Body" ,requ
4646

4747
}
4848

49-
public ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody Boolean body) {
49+
public ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody(required=false) Boolean body) {
5050
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
5151

5252
}
5353

54-
public ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody OuterComposite body) {
54+
public ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody(required=false) OuterComposite body) {
5555
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
5656
if (mediaType.isCompatibleWith(MediaType.valueOf("*/*"))) {
5757
String exampleString = "{ \"my_string\" : \"my_string\", \"my_number\" : 0.8008281904610115, \"my_boolean\" : true }";
@@ -63,12 +63,12 @@ public ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(valu
6363

6464
}
6565

66-
public ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody BigDecimal body) {
66+
public ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody(required=false) BigDecimal body) {
6767
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
6868

6969
}
7070

71-
public ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody String body) {
71+
public ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody(required=false) String body) {
7272
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
7373

7474
}

samples/server/petstore/springboot-beanvalidation/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public interface FakeApi {
5252
@RequestMapping(value = "/fake/outer/boolean",
5353
produces = { "*/*" },
5454
method = RequestMethod.POST)
55-
ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody Boolean body);
55+
ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody(required=false) Boolean body);
5656

5757

5858
@ApiOperation(value = "", nickname = "fakeOuterCompositeSerialize", notes = "Test serialization of object with outer number type", response = OuterComposite.class, tags={ "fake", })
@@ -61,7 +61,7 @@ public interface FakeApi {
6161
@RequestMapping(value = "/fake/outer/composite",
6262
produces = { "*/*" },
6363
method = RequestMethod.POST)
64-
ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody OuterComposite body);
64+
ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody(required=false) OuterComposite body);
6565

6666

6767
@ApiOperation(value = "", nickname = "fakeOuterNumberSerialize", notes = "Test serialization of outer number types", response = BigDecimal.class, tags={ "fake", })
@@ -70,7 +70,7 @@ public interface FakeApi {
7070
@RequestMapping(value = "/fake/outer/number",
7171
produces = { "*/*" },
7272
method = RequestMethod.POST)
73-
ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody BigDecimal body);
73+
ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody(required=false) BigDecimal body);
7474

7575

7676
@ApiOperation(value = "", nickname = "fakeOuterStringSerialize", notes = "Test serialization of outer string types", response = String.class, tags={ "fake", })
@@ -79,7 +79,7 @@ public interface FakeApi {
7979
@RequestMapping(value = "/fake/outer/string",
8080
produces = { "*/*" },
8181
method = RequestMethod.POST)
82-
ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody String body);
82+
ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody(required=false) String body);
8383

8484

8585
@ApiOperation(value = "", nickname = "testBodyWithFileSchema", notes = "For this test, the body for this request much reference a schema named `File`.", tags={ "fake", })

samples/server/petstore/springboot-beanvalidation/src/main/java/org/openapitools/api/FakeApiController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public ResponseEntity<Void> createXmlItem(@ApiParam(value = "XmlItem Body" ,requ
4646

4747
}
4848

49-
public ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody Boolean body) {
49+
public ResponseEntity<Boolean> fakeOuterBooleanSerialize(@ApiParam(value = "Input boolean as post body" ) @Valid @RequestBody(required=false) Boolean body) {
5050
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
5151

5252
}
5353

54-
public ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody OuterComposite body) {
54+
public ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(value = "Input composite as post body" ) @Valid @RequestBody(required=false) OuterComposite body) {
5555
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
5656
if (mediaType.isCompatibleWith(MediaType.valueOf("*/*"))) {
5757
String exampleString = "{ \"my_string\" : \"my_string\", \"my_number\" : 0.8008281904610115, \"my_boolean\" : true }";
@@ -63,12 +63,12 @@ public ResponseEntity<OuterComposite> fakeOuterCompositeSerialize(@ApiParam(valu
6363

6464
}
6565

66-
public ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody BigDecimal body) {
66+
public ResponseEntity<BigDecimal> fakeOuterNumberSerialize(@ApiParam(value = "Input number as post body" ) @Valid @RequestBody(required=false) BigDecimal body) {
6767
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
6868

6969
}
7070

71-
public ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody String body) {
71+
public ResponseEntity<String> fakeOuterStringSerialize(@ApiParam(value = "Input string as post body" ) @Valid @RequestBody(required=false) String body) {
7272
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
7373

7474
}

0 commit comments

Comments
 (0)