Skip to content

Support @Validated on method parameter #571

New issue

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

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

Already on GitHub? # to your account

Closed
hatotsumuri opened this issue Dec 9, 2022 · 2 comments
Closed

Support @Validated on method parameter #571

hatotsumuri opened this issue Dec 9, 2022 · 2 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@hatotsumuri
Copy link

hatotsumuri commented Dec 9, 2022

Hi.
I'm trying to add validation with GraphQL.

But "@validated" not working with "GraphQlController".
(I tried "GraphQlController × @Valid" and "RestController × @Validated" and they worked.)

Below is the source code.
If there are any missing settings, please kindly advise.

Thanks.

■Query

query ($accountCondition: AccountCondition)  {
	findAccountByCondition(accountCondition: $accountCondition) {
		accountId
		accountName
	}
}

■Query variables

{
	"accountCondition": {
		"accountName": "test"
	}
}

■schema.graphqls

input AccountCondition {
    accountId: Int
    accountName: String
}

type Account {
    accountId: ID!
    accountName: String
 }

type Query {
    findAccountByCondition(accountCondition: AccountCondition): [Account]
}

■AccountCondition

@Data
public class AccountCondition {
	@NotNull
	private Long accountId;
	private String accountName;
}

■GraphQLController × @validated → Validation not works.

import org.springframework.validation.annotation.Validated;
…
@Controller
@RequiredArgsConstructor
public class GraphQlController {
	private final AccountService accountService;

	@QueryMapping
	public List<Account> findAccountByCondition(
			@Argument @Validated AccountCondition accountCondition) {
		List<Account> accountList = accountService.findByCondition(
				accountCondition);
		return accountList;
	}
}

■GraphQLController × @Valid→ Validation works.

import javax.validation.Valid;
…
@Controller
@RequiredArgsConstructor
public class GraphQlController {
	private final AccountService accountService;

	@QueryMapping
	public List<Account> findAccountByCondition(
			@Argument @Valid AccountCondition accountCondition) {
		List<Account> accountList = accountService.findByCondition(
				accountCondition);
		return accountList;
	}
}

■RestController × @validated → Validation works.

import org.springframework.validation.annotation.Validated;
…
@Controller
@RequiredArgsConstructor
public class RestController {
	private final AccountService accountService;

	@PostMapping("/account/search")
	@ResponseBody
	public Result findAccountByCondition(
			@RequestBody @Validated AccountCondition accountCondition) {
		List<Account> accountList = accountService.findByCondition(
				accountCondition);
		return ResultUtil.newSuccess(accountList);
	}
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 9, 2022
@rstoyanchev
Copy link
Contributor

We call standard bean validation to validate all the method parameters, and it does look like that requires @Valid.

Are you trying to specify validation groups? We do check for @Validated on the method, so you can specify groups that way still, but otherwise as it is currently you do need @Valid to trigger cascaded validation.

I think we could enhance our validation support to detect method parameters with @Validated but without @Valid and to apply validation on them, in addition to invoking standard bean validation for other method parameters.

@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 9, 2022
@rstoyanchev rstoyanchev self-assigned this Dec 9, 2022
@rstoyanchev rstoyanchev added this to the 1.2 Backlog milestone Dec 9, 2022
@rstoyanchev rstoyanchev removed their assignment Dec 9, 2022
@rstoyanchev rstoyanchev changed the title @Validated not working with GraphQlController Support @Validated on method parameter Dec 9, 2022
@hatotsumuri
Copy link
Author

hatotsumuri commented Dec 10, 2022

Thank you for your advice.

Are you trying to specify validation groups?

Yes, I would like to use validation groups in graphGL.

We do check for @validated on the method, so you can specify groups that way still.

Thank you for the useful information.
I will try this implementation.

I think we could enhance our validation support to detect method parameters with @validated but without @Valid

It would be great if your can support this.

Thanks.

@rstoyanchev rstoyanchev modified the milestones: 1.2 Backlog, 1.2.0-M1 Jan 12, 2023
@rstoyanchev rstoyanchev self-assigned this Jan 20, 2023
rstoyanchev added a commit that referenced this issue Jan 27, 2023
Perform the check for whether validation for a HandlerMethod is needed
earlier and only once rather than in the constructor of
DataFetcherHandlerMethod.

Make the validation helper passed to DataFetcherHandlerMethod stateful,
so that validation groups are also determined once on startup.

See gh-571
@rstoyanchev rstoyanchev modified the milestones: 1.2.0-M1, 1.1.2 Feb 1, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants