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

Support validating collection of objects on web controller method #16917

Closed
Tracked by #30645
spring-projects-issues opened this issue Oct 7, 2014 · 9 comments
Closed
Tracked by #30645
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 7, 2014

Will May opened SPR-12312 and commented

Add support for being able to validate a list of objects similar to the example below, where Foo is a class which has various JSR 303 annotations on its fields:

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public void insertFoos(@Valid @RequestBody List<Foo> foos) {
  ...
}

I've managed to partially implement the functionality by extending the LocalValidatorFactoryBean and setting the nested path to the current list path ([i]) while calling super.validate in a loop. This almost works apart from the fact that the BeanWrapper cannot retrieve an invalid value if one fails validation and so throws an exception while trying to throw an exception.

So, in summary, the BeanWrapper needs to be able to work directly on lists rather than only being able to work on objects containing lists.


Affects: 4.0.7

Issue Links:

1 votes, 4 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 7, 2014

Will May commented

It's possible that this might be a duplicate or a subtask of #6751 but that appears to be more to do with Map of objects rather than Collection of objects.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Sep 4, 2015

Frédéric Camblor commented

+1 on this (just created a duplicate of the issue before finding this one -> #18007)

@spring-projects-issues
Copy link
Collaborator Author

mathsworld2001 commented

I recently came across this very same issue. Some at stackoverflow claims that this should already work as expected. I still cannot make this work. Can anyone confirm the current status of this ticket?

@spring-projects-issues
Copy link
Collaborator Author

Will May commented

Still doesn't work. See an example at https://github.com/wjam/spr-12312

@SumithraPrasad
Copy link

SumithraPrasad commented Dec 26, 2019

@Validated with MockMVC is not working.

@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class TestController {
	private MockMvc mvc;

	@Mock
	private TestService service;

	@InjectMocks
	private Controller controller;

	private ObjectMapper mapper = new ObjectMapper();

	@Before
	public void setup() { // MockMvc standalone approach mvc =
		mvc = MockMvcBuilders.standaloneSetup(controller).setValidator(validator()).setControllerAdvice(new InvoiceServiceExceptionHandler())
				.build();
	}

	@Test
	public void whenNullValue_thenReturns400() throws JsonProcessingException, Exception {

		TestDTO testDTO = new TestDTO();
		testDTO.setId(null);

		ArrayList<TestDTO> testList = new ArrayList<TestDTO>(Arrays.asList(testDTO));

		String jsonTestList = mapper.writeValueAsString(testList);
		

		MvcResult responseMVC = mvc.perform(post("/v1/test").content(jsonTestList).header(HttpHeaders.CONTENT_TYPE,
				MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()).andReturn();

	}

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jan 2, 2020

@SumithraPrasad, I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference. In particular avoid use of unquoted @ which are mentions on Github.

Also if it isn't supported to begin with, then it can't work with MockMvc.

@jhoeller jhoeller modified the milestones: 5.x Backlog, General Backlog Aug 24, 2020
@exe-atewinkel
Copy link

exe-atewinkel commented Aug 16, 2023

This is a very old issue, but I've just run into this problem. I want to validate a Collection of objects, using validation groups.

Putting @Validated({Default.class, MyValidationGroup.class}) before the collection does not trigger validation of MyValidationGroup. Apparently it should be before the type parameter of the collection (similar to @Valid, which does not support groups), but unlike @Valid, @Validated cannot be applied to a type parameter.

This makes validating a collection of objects with specific validation groups impossible. I would greatly appreciate it if something could be done to fix this.

Specifying groups for an array of objects also does not work.

@rstoyanchev rstoyanchev changed the title Add support for validating a collection of objects [SPR-12312] Add support for validating a collection of objects Aug 23, 2023
@rstoyanchev rstoyanchev self-assigned this Aug 23, 2023
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Aug 23, 2023

The main challenge for bean validation support on a collection of objects is representing the results. You need one BindingResult for each object being validated, while SpringValidatorAdapter is only prepared to validate one object and prepare one BindingResult.

As part of changes to add built-in web support for method validation (see #30645) for 6.1, we already support applying method validation and preparing a MethodValidationResult with separate results for each parameter. This also supports representing results from the validation of Lists and Maps of objects.

So this should already work (with the latest 6.1 milestone 4) when method validation is applied via AOP (i.e. with @Validated at the class level). For the built-in method validation however in spring-web, I expect we need to adjust the logic that decides whether to apply method validation or to validate the parameter individually as we have always done. I'm going to schedule this to experiment for RC1.

@exe-atewinkel, thanks for the additional comment. As part of the work, I'll also experiment with applying groups, and will comment further on that.

@rstoyanchev rstoyanchev changed the title Add support for validating a collection of objects Support validating collection of objects on web controller method Aug 23, 2023
rstoyanchev added a commit that referenced this issue Sep 4, 2023
@rstoyanchev
Copy link
Contributor

The built-in method validation in Spring MVC and WebFlux now works for List validation with some minor changes b068742 as part of the more extensive changes for #30645.

@rstoyanchev rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2023
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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants