Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable built-in controller method validation per controller #32292

Closed
mlichtblau opened this issue Feb 19, 2024 · 4 comments
Closed

Disable built-in controller method validation per controller #32292

mlichtblau opened this issue Feb 19, 2024 · 4 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)

Comments

@mlichtblau
Copy link

Following up on this discussion about disabling the built-in controller method validation here with @rstoyanchev.

The bean validation spec rule seems reasonable for polymorphic use, but controllers are invoked directly by the web framework, and I'm not sure there is a practical difference there.

Sorry, I don't think I fully understand your point here. Do you think it would be reasonable that if the validation annotation is present only on the interface, but not on the controller, that no validation is performed? This would work well for my problem as I could place the annotations only on the controller that should actually perform validation and not on the one in the proxy (this is in line with the spec I think). Currently validation is performed even if the annotation is only in the interface.

I'm not sure it would be ideal to have annotations specifically to turn off method validation.

I see your point, maybe there is another solution that does not require a new validation annotation? Would it be possible to specify the Validator implementation per controller? I could use a no-op Validator for some controllers that don't need validation and the Spring Validator for the other controllers.

If your proxy does not need validation at all

Unfortunately this doesn't work as there are some controllers that do use validation.

Thank you!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 19, 2024
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Feb 19, 2024
@sbrannen sbrannen changed the title Disable built-in controller method annotation per controller Disable built-in controller method validation per controller Feb 19, 2024
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Mar 8, 2024

Sorry, I don't think I fully understand your point here. Do you think it would be reasonable that if the validation annotation is present only on the interface, but not on the controller, that no validation is performed?

I think you mean the opposite, if validation annotations are present only on the controller, but not on the interface?

Generally, in an object hierarchy, it makes sense for preconditions to be in the contract so that code using the contract works just the same with any implementation. Controllers are different in that they are never used hidden behind a contract. They are just entry points for web handling, only ever invoked by Spring MVC, which is fully aware of both contract and concrete implementation.

This would work well for my problem as I could place the annotations only on the controller that should actually perform validation and not on the one in the proxy (this is in line with the spec I think). Currently validation is performed even if the annotation is only in the interface.

That would be my suggestion, and it makes sense given that validation should be performed for one set of controllers only.

@rstoyanchev rstoyanchev added the status: waiting-for-feedback We need additional information before we can continue label Mar 8, 2024
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Mar 15, 2024
@spring-projects-issues
Copy link
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2024
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 22, 2024
@mlichtblau
Copy link
Author

Sorry for the late reply.

Generally, in an object hierarchy, it makes sense for preconditions to be in the contract so that code using the contract works just the same with any implementation. Controllers are different in that they are never used hidden behind a contract. They are just entry points for web handling, only ever invoked by Spring MVC, which is fully aware of both contract and concrete implementation.

I fully agree that this is a special corner case relevant only to controllers. Sorry if my problem description was unclear.

The issue is that it is not possible to put the constraint annotation only on the controller when it inherits from an interface that does not have the annotation. In this case a runtime exception will be thrown, indicating that the 4.5.5 rule of the bean validation spec is violated. This means I have to put the validation also on the interface.

Example from docs

Example 4.16. Illegally declared parameter constraints on interface implementation

public interface OrderService {

  void placeOrder(String customerCode, Item item, int quantity);
}

public class SimpleOrderService implements OrderService {

  @Override
  public void placeOrder(
      @NotNull @Size(min=3, max=20) String customerCode,
      @NotNull Item item,
      @Min(1) int quantity) {
      [...]
  }
}

The constraints in SimpleOrderService are illegal, as they strengthen the preconditions of the placeOrder() method as constituted by the interface OrderService.

This will throw a runtime exception.

My proxy controller also inherits from this interface, but even though the annotation is not present on the controller, from what I can tell validation is still performed because of the annotation in the interface. This is also mentioned in the linked docs.

This means there is no possibility for me to place annotations somewhere to achieve me desired outcome while keeping the current inheritance structure of one interface and both controllers inheriting from that interface.

This is why I would like to disable all validation for one of these controllers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)
Projects
None yet
Development

No branches or pull requests

4 participants