Skip to content

Commit

Permalink
Swap put and post methods
Browse files Browse the repository at this point in the history
(cherry picked from commit 07e748d)
  • Loading branch information
isaul32 authored and geoand committed Sep 28, 2021
1 parent 921485d commit 5d9c68d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
32 changes: 16 additions & 16 deletions docs/src/main/asciidoc/validation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -369,28 +369,28 @@ quarkus.locales=en-US,es-ES,fr-FR
It's sometimes necessary to enable different validation constraints
for the same class when it's passed to a different method.

For example, a `Book` may need to have a `null` identifier when passed to the `put` method
For example, a `Book` may need to have a `null` identifier when passed to the `post` method
(because the identifier will be generated),
but a non-`null` identifier when passed to the `post` method
but a non-`null` identifier when passed to the `put` method
(because the method needs the identifier to know what to update).

To address this, you can take advantage of validation groups.
Validation groups are markers that you put on your constraints in order to enable or disable them at will.

First, define the `Put` and `Post` groups, which are just Java interfaces.
First, define the `Post` and `Put` groups, which are just Java interfaces.

[source, java]
----
public interface ValidationGroups {
interface Put extends Default { // <1>
}
interface Post extends Default { // <1>
}
interface Put extends Default { // <1>
}
}
----
<1> Make the custom groups extend the `Default` group.
This means that whenever these groups are enabled, the `Default` group is also enabled.
This is useful if you have constraints that you want validated in both the `Put` and `Post` method:
This is useful if you have constraints that you want validated in both the `Post` and `Put` method:
you can simply use the default group on those constraints, like on the `title` property below.

Then add the relevant constraints to `Book`, assigning the right group to each constraint:
Expand All @@ -399,8 +399,8 @@ Then add the relevant constraints to `Book`, assigning the right group to each c
----
public class Book {
@Null(groups = ValidationGroups.Put.class)
@NotNull(groups = ValidationGroups.Post.class)
@Null(groups = ValidationGroups.Post.class)
@NotNull(groups = ValidationGroups.Put.class)
public Long id;
@NotBlank
Expand All @@ -414,24 +414,24 @@ Finally, add a `@ConvertGroup` annotation next to your `@Valid` annotation in yo
[source, java]
----
@Path("/")
@PUT
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void put(@Valid @ConvertGroup(to = ValidationGroups.Put.class) Book book) { // <1>
public void post(@Valid @ConvertGroup(to = ValidationGroups.Post.class) Book book) { // <1>
// ...
}
@Path("/")
@POST
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void post(@Valid @ConvertGroup(to = ValidationGroups.Post.class) Book book) { // <2>
public void put(@Valid @ConvertGroup(to = ValidationGroups.Put.class) Book book) { // <2>
// ...
}
----
<1> Enable the `Put` group, meaning only constraints assigned to the `Put` (and `Default`) groups
will be validated for the `book` parameter of the `put` method.
In this case, it means `Book.id` must be `null` and `Book.title` must not be blank.
<2> Enable the `Post` group, meaning only constraints assigned to the `Post` (and `Default`) groups
<1> Enable the `Post` group, meaning only constraints assigned to the `Post` (and `Default`) groups
will be validated for the `book` parameter of the `post` method.
In this case, it means `Book.id` must be `null` and `Book.title` must not be blank.
<2> Enable the `Put` group, meaning only constraints assigned to the `Put` (and `Default`) groups
will be validated for the `book` parameter of the `put` method.
In this case, it means `Book.id` must not be `null` and `Book.title` must not be blank.

[[configuration-reference]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,21 @@ public String testHibernateOrmIntegration() {
return "FAILED";
}

@PUT
@POST
@Path("/rest-end-point-validation-groups/")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String testRestEndPointValidationGroups_Put(
@Valid @ConvertGroup(to = ValidationGroups.Put.class) MyBeanWithGroups bean) {
public String testRestEndPointValidationGroups_Post(
@Valid @ConvertGroup(to = ValidationGroups.Post.class) MyBeanWithGroups bean) {
return "passed";
}

@POST
@PUT
@Path("/rest-end-point-validation-groups/")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String testRestEndPointValidationGroups_Post(
@Valid @ConvertGroup(to = ValidationGroups.Post.class) MyBeanWithGroups bean) {
public String testRestEndPointValidationGroups_Put(
@Valid @ConvertGroup(to = ValidationGroups.Put.class) MyBeanWithGroups bean) {
return "passed";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

public class MyBeanWithGroups {

@Null(groups = ValidationGroups.Put.class)
@NotNull(groups = { ValidationGroups.Post.class, ValidationGroups.Get.class, ValidationGroups.Delete.class })
@Null(groups = ValidationGroups.Post.class)
@NotNull(groups = { ValidationGroups.Put.class, ValidationGroups.Get.class, ValidationGroups.Delete.class })
private Long id;

@NotNull
private String name;

@AssertFalse(groups = { ValidationGroups.Put.class, ValidationGroups.Post.class, ValidationGroups.Get.class })
@AssertFalse(groups = { ValidationGroups.Post.class, ValidationGroups.Put.class, ValidationGroups.Get.class })
@AssertTrue(groups = ValidationGroups.Delete.class)
private boolean deleted;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import javax.validation.groups.Default;

public interface ValidationGroups {
interface Put extends Default {
interface Post extends Default {
}

interface Post extends Default {
interface Put extends Default {
}

interface Get extends Default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,38 +353,38 @@ public void testInheritance() {

@Test
public void testRestEndPointValidationGroups_parameters() {
// PUT: input id must be null
// POST: input id must be null
RestAssured.given()
.contentType(ContentType.JSON)
.body("{\"id\": 1, \"name\": \"b\"}")
.when()
.put("/hibernate-validator/test/rest-end-point-validation-groups/")
.post("/hibernate-validator/test/rest-end-point-validation-groups/")
.then()
.statusCode(400)
.body(containsString("must be null"));
RestAssured.given()
.contentType(ContentType.JSON)
.body("{\"name\": \"b\"}")
.when()
.put("/hibernate-validator/test/rest-end-point-validation-groups/")
.post("/hibernate-validator/test/rest-end-point-validation-groups/")
.then()
.statusCode(200)
.body(containsString("passed"));

// POST: input id must not be null
// PUT: input id must not be null
RestAssured.given()
.contentType(ContentType.JSON)
.body("{\"name\": \"b\"}")
.when()
.post("/hibernate-validator/test/rest-end-point-validation-groups/")
.put("/hibernate-validator/test/rest-end-point-validation-groups/")
.then()
.statusCode(400)
.body(containsString("must not be null"));
RestAssured.given()
.contentType(ContentType.JSON)
.body("{\"id\": 1, \"name\": \"b\"}")
.when()
.post("/hibernate-validator/test/rest-end-point-validation-groups/")
.put("/hibernate-validator/test/rest-end-point-validation-groups/")
.then()
.statusCode(200)
.body(containsString("passed"));
Expand All @@ -394,15 +394,15 @@ public void testRestEndPointValidationGroups_parameters() {
.contentType(ContentType.JSON)
.body("{}")
.when()
.put("/hibernate-validator/test/rest-end-point-validation-groups/")
.post("/hibernate-validator/test/rest-end-point-validation-groups/")
.then()
.statusCode(400)
.body(containsString("must not be null"));
RestAssured.given()
.contentType(ContentType.JSON)
.body("{\"id\":1}")
.when()
.post("/hibernate-validator/test/rest-end-point-validation-groups/")
.put("/hibernate-validator/test/rest-end-point-validation-groups/")
.then()
.statusCode(400)
.body(containsString("must not be null"));
Expand Down

0 comments on commit 5d9c68d

Please sign in to comment.