-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Support validating collection of objects on web controller method #16917
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Comments
Frédéric Camblor commented +1 on this (just created a duplicate of the issue before finding this one -> #18007) |
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? |
Will May commented Still doesn't work. See an example at https://github.com/wjam/spr-12312 |
@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();
} |
@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 Also if it isn't supported to begin with, then it can't work with MockMvc. |
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 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. |
The main challenge for bean validation support on a collection of objects is representing the results. You need one 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 So this should already work (with the latest 6.1 milestone 4) when method validation is applied via AOP (i.e. with @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. |
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:
I've managed to partially implement the functionality by extending the
LocalValidatorFactoryBean
and setting the nested path to the current list path ([i]
) while callingsuper.validate
in a loop. This almost works apart from the fact that theBeanWrapper
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:
@Validated
support on Iterables (and implementors)1 votes, 4 watchers
The text was updated successfully, but these errors were encountered: