Skip to content

Commit 59f9a23

Browse files
authored
Merge pull request #627 from Azquelt/requestbody-default-required
Make request body required by default
2 parents d927a38 + 8f3ec87 commit 59f9a23

File tree

8 files changed

+46
-12
lines changed

8 files changed

+46
-12
lines changed

api/src/main/java/org/eclipse/microprofile/openapi/annotations/parameters/RequestBody.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@
5656

5757
/**
5858
* Determines if the request body is required in the request.
59+
* <p>
60+
* Note that the default value of this property is {@code true}, while the default value of the {@code required}
61+
* property in the OpenAPI specification is {@code false}, because Jakarta REST resource methods which accept a
62+
* request body generally require it.
5963
*
6064
* @return whether or not this requestBody is required
6165
**/
62-
boolean required() default false;
66+
boolean required() default true;
6367

6468
/**
6569
* The unique name to identify this request body. Unless this annotation is used on the actual request body

api/src/main/java/org/eclipse/microprofile/openapi/annotations/parameters/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
* </pre>
3333
*/
3434

35-
@org.osgi.annotation.versioning.Version("1.2")
35+
@org.osgi.annotation.versioning.Version("1.2.1")
3636
@org.osgi.annotation.versioning.ProviderType
3737
package org.eclipse.microprofile.openapi.annotations.parameters;

tck/src/main/java/org/eclipse/microprofile/openapi/apps/airlines/JAXRSApp.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,17 @@
172172
@RequestBody(name = "review",
173173
content = @Content(mediaType = MediaType.APPLICATION_JSON,
174174
schema = @Schema(implementation = Review.class)),
175-
required = true,
176175
description = "example review to add"),
176+
@RequestBody(name = "nonRequiredReview",
177+
content = @Content(mediaType = MediaType.APPLICATION_JSON,
178+
schema = @Schema(implementation = Review.class)),
179+
required = false,
180+
description = "example non-required review"),
181+
@RequestBody(name = "requiredReview",
182+
content = @Content(mediaType = MediaType.APPLICATION_JSON,
183+
schema = @Schema(implementation = Review.class)),
184+
required = true,
185+
description = "example required review"),
177186
@RequestBody(name = "reviewARef",
178187
ref = "#/components/requestBodies/review",
179188
description = "Review reference")

tck/src/main/java/org/eclipse/microprofile/openapi/apps/airlines/resources/UserResource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public Response createUser(
148148
/* tags = {"user"}, //this operation intentionally doesn't have tags attribute, since above Tag ref should apply */
149149
)
150150
public Response createUsersWithArrayInput(
151-
@RequestBody(description = "Array of user object", required = true,
151+
@RequestBody(description = "Array of user object",
152152
content = @Content(mediaType = "application/json",
153153
schema = @Schema(type = SchemaType.ARRAY, implementation = User.class,
154154
nullable = true, writeOnly = true, minItems = 2,
@@ -168,7 +168,7 @@ public Response createUsersWithArrayInput(
168168
@Operation(summary = "Creates list of users with given input list", // List of User objects
169169
operationId = "createUsersFromList")
170170
public Response createUsersWithListInput(
171-
@RequestBody(description = "List of user object", required = true) java.util.List<User> users) {
171+
@RequestBody(description = "List of user object") java.util.List<User> users) {
172172
for (User user : users) {
173173
userData.addUser(user);
174174
}
@@ -177,7 +177,7 @@ public Response createUsersWithListInput(
177177

178178
@Path("/username/{username}")
179179
@PUT
180-
@RequestBody(name = "user", description = "Record of a new user to be created in the system.",
180+
@RequestBody(name = "user", description = "Record of a new user to be created in the system.", required = false,
181181
content = @Content(mediaType = "application/json", schema = @Schema(implementation = User.class),
182182
examples = @ExampleObject(name = "user",
183183
summary = "Example user properties to update",

tck/src/main/java/org/eclipse/microprofile/openapi/apps/petstore/resource/PetResource.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public Response deletePet(
172172
@RequestBody(name = "pet",
173173
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Pet.class),
174174
examples = @ExampleObject(ref = "http://example.org/petapi-examples/openapi.json#/components/examples/pet-example")),
175-
required = true, description = "example of a new pet to add")
175+
description = "example of a new pet to add")
176176
@Operation(summary = "Add pet to store", description = "Add a new pet to the store")
177177
public Response addPet(Pet pet) {
178178
Pet updatedPet = petData.addPet(pet);
@@ -193,7 +193,6 @@ public Response addPet(Pet pet) {
193193
@Operation(summary = "Update an existing pet", description = "Update an existing pet with the given new attributes")
194194
public Response updatePet(
195195
@RequestBody(description = "Attribute to update existing pet record",
196-
required = true,
197196
content = @Content(schema = @Schema(implementation = Pet.class))) Pet pet) {
198197
Pet updatedPet = petData.addPet(pet);
199198
return Response.ok().entity(updatedPet).build();

tck/src/main/java/org/eclipse/microprofile/openapi/apps/petstore/resource/PetStoreResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public Response getOrderById(
115115
@APIResponse(responseCode = "200", description = "successful operation")
116116
@APIResponse(responseCode = "400", description = "Invalid Order")
117117
public Order placeOrder(
118-
@RequestBody(description = "order placed for purchasing the pet", required = true) Order order) {
118+
@RequestBody(description = "order placed for purchasing the pet") Order order) {
119119
storeData.placeOrder(order);
120120
return storeData.placeOrder(order);
121121
}

tck/src/main/java/org/eclipse/microprofile/openapi/apps/petstore/resource/UserResource.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public class UserResource {
6969
})
7070
public Response createUser(
7171
@RequestBody(description = "Created user object",
72-
content = @Content(schema = @Schema(ref = "#/components/schemas/User")),
73-
required = true) User user) {
72+
content = @Content(schema = @Schema(ref = "#/components/schemas/User"))) User user) {
7473
userData.addUser(user);
7574
return Response.ok().entity("").build();
7675
}
@@ -112,7 +111,7 @@ public Response updateUser(
112111
@Parameter(name = "username", description = "name that need to be deleted",
113112
schema = @Schema(type = SchemaType.STRING),
114113
required = true) @PathParam("username") String username,
115-
@RequestBody(description = "Updated user object", required = true) User user) {
114+
@RequestBody(description = "Updated user object") User user) {
116115
userData.addUser(user);
117116
return Response.ok().entity("").build();
118117
}

tck/src/main/java/org/eclipse/microprofile/openapi/tck/AirlinesAppTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,16 @@ public void testRequestBodyAnnotations(String type) {
502502
vr.body(endpoint + ".description", equalTo("Create a new booking with the provided information."));
503503
vr.body(endpoint + ".content", notNullValue());
504504
vr.body(endpoint + ".x-request-body", equalTo("test-request-body"));
505+
vr.body(endpoint + ".required", equalTo(true));
505506

507+
// PUT method with entity parameter but no @RequestBody annotation
506508
endpoint = "paths.'/bookings/{id}'.put.requestBody";
507509
vr.body(endpoint + ".content", notNullValue());
510+
vr.body(endpoint + ".required", equalTo(true));
511+
512+
// GET method without @RequestBody annotation
513+
endpoint = "paths.'/bookings/{id}'.get.requestBody";
514+
vr.body(endpoint, nullValue());
508515

509516
endpoint = "paths.'/user'.post.requestBody";
510517
vr.body(endpoint + ".description", equalTo("Record of a new user to be created in the system."));
@@ -514,6 +521,7 @@ public void testRequestBodyAnnotations(String type) {
514521
endpoint = "paths.'/user/username/{username}'.put.requestBody";
515522
vr.body(endpoint + ".description", equalTo("Record of a new user to be created in the system."));
516523
vr.body(endpoint + ".content", notNullValue());
524+
vr.body(endpoint + ".required", either(nullValue()).or(equalTo(false)));
517525

518526
endpoint = "paths.'/user/createWithArray'.post.requestBody";
519527
vr.body(endpoint + ".description", equalTo("Array of user object"));
@@ -524,6 +532,21 @@ public void testRequestBodyAnnotations(String type) {
524532
vr.body(endpoint + ".description", equalTo("List of user object"));
525533
vr.body(endpoint + ".content", notNullValue());
526534
vr.body(endpoint + ".required", equalTo(true));
535+
536+
endpoint = "components.requestBodies.review";
537+
vr.body(endpoint + ".description", equalTo("example review to add"));
538+
vr.body(endpoint + ".content", notNullValue());
539+
vr.body(endpoint + ".required", equalTo(true));
540+
541+
endpoint = "components.requestBodies.nonRequiredReview";
542+
vr.body(endpoint + ".description", equalTo("example non-required review"));
543+
vr.body(endpoint + ".content", notNullValue());
544+
vr.body(endpoint + ".required", either(nullValue()).or(equalTo(false)));
545+
546+
endpoint = "components.requestBodies.requiredReview";
547+
vr.body(endpoint + ".description", equalTo("example required review"));
548+
vr.body(endpoint + ".content", notNullValue());
549+
vr.body(endpoint + ".required", equalTo(true));
527550
}
528551

529552
@Test(dataProvider = "formatProvider")

0 commit comments

Comments
 (0)