restx/restx — pull request #137 by fcamblor (2014-12-29 09:58:28+00:00)
Title: @ValidatedFor annotation support for validation groups

Proposal to support bean validation groups during validation.

I'm not really satisfied by the current implementation and would like to open the discussion about it.

At first, I was planning to implement usage below :
```
@PermitAll
@POST("/valid/pojos")
public void createPOJOWithoutAnnotation(POJO myPojo) { // No @ValidatedFor annotation => Default validation group will be implicitly used here, keeping backward compat behaviour
LOG.info("Pojo {} {} created !", myPojo.getName(), myPojo.getSubPOJO().getLabel());
}

@PermitAll
@POST("/valid/pojos2")
public void createPOJOWithAnnotation(@ValidatedFor(FormValidations.Create.class) POJO myPojo) { // Here, defining we want to activate the Create validation group mode on validation
LOG.info("Pojo {} {} created !", myPojo.getName(), myPojo.getSubPOJO().getLabel());
}

@PermitAll
@PUT("/valid/pojos/{id}")
public void createPOJOWithoutAnnotation(Long id, @ValidatedFor({MyCustomValidationGroup.class, FormValidations.Update.class}) POJO myPojo) { // Multiple validation groups can be provided, following bean validation's Validator API
LOG.info("Pojo {} {} updated !", myPojo.getName(), myPojo.getSubPOJO().getLabel());
}
```
with group declaration :
```
public interface FormValidations {
public static interface Create extends Default{}
public static interface Update extends Default{}
}

public class ValidationResource {
// ...
public static interface MyCustomValidationGroup{}
// ...
}
```

But I faced some issues at annotation processing time when retrieving `@ValidatedFor.value()` classes : didn't completely understood why, but some classes (either in `restx-core` or in `samplest`) were not accessible.
You can see some testing I made in commit 7f88a79573d00d5d28c796d238aaf0dfdde1fdcf (from branch [validations-validatedfor-with-class](https://github.com/fcamblor/restx/commits/validations-validatedfor-with-class), some github comments describe what's going weird)

Thus, I had to fallback from `Class[]` to `String[]` in order to directly provide validation group FQN used for source generation.
Which makes declaration look like :
```
@PermitAll
@POST("/valid/pojos")
public void createPOJOWithoutAnnotation(POJO myPojo) {
LOG.info("Pojo {} {} created !", myPojo.getName(), myPojo.getSubPOJO().getLabel());
}

@PermitAll
@POST("/valid/pojos2")
public void createPOJOWithAnnotation(@ValidatedFor(FormValidations.CreateFQN) POJO myPojo) {
LOG.info("Pojo {} {} created !", myPojo.getName(), myPojo.getSubPOJO().getLabel());
}

@PermitAll
@PUT("/valid/pojos/{id}")
public void createPOJOWithoutAnnotation(Long id, @ValidatedFor({MyCustomValidationGroupFQN, FormValidations.UpdateFQN}) POJO myPojo) {
LOG.info("Pojo {} {} updated !", myPojo.getName(), myPojo.getSubPOJO().getLabel());
}
```
and group declaration :
```
public interface FormValidations {
public static final String DefaultFQN = "javax.validation.groups.Default";
public static final String CreateFQN = "restx.validation.stereotypes.FormValidations.Create";
public static interface Create extends Default{}
public static final String UpdateFQN = "restx.validation.stereotypes.FormValidations.Update";
public static interface Update extends Default{}
}

public class ValidationResource {
// ...
public static interface MyCustomValidationGroup{}
public static final String MyCustomValidationGroupFQN = "samplest.validation.ValidationResource.MyCustomValidationGroup";
// ...
}
```

If you see any workaround for this issue, I'm opened to discuss it.

Another way would be to rely on reflection but I know this is something we should avoid, especially for such trivial things.