Support @Validated validation groups
See original GitHub issueIs your feature request related to a problem? Please describe. Using one request object for PUT and PATCH methods and using @Validated annotation with validation groups should respect the validation of the group.
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("controller")
public class Controller {
@PutMapping
public void put(@RequestBody @Validated(Request.PUT.class) Request request) {}
@PatchMapping
public void patch(@RequestBody @Validated(Request.PATCH.class) Request request) {}
}
import javax.validation.constraints.NotNull;
public class Request {
@NotNull(groups = {PUT.class}) public String name;
interface PUT {}
interface PATCH {}
}
Generates: irrelevant parts omitted for brevity
{
"openapi": "3.0.1",
"info": {},
"servers": [],
"paths": {
"/controller": {
"put": {
"tags": [
"controller"
],
"operationId": "put",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Request"
}
}
}
},
"responses": {}
},
"patch": {
"tags": [
"controller"
],
"operationId": "patch",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Request"
}
}
}
},
"responses": {}
}
}
},
"components": {
"schemas": {
"Request": {
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
Describe the solution you’d like In the above example the patch route should have name as an optional parameter, not required
Something like:
{
"openapi": "3.0.1",
"info": {},
"servers": [],
"paths": {
"/controller": {
"put": {
"tags": [
"controller"
],
"operationId": "put",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Request_PUT"
}
}
}
},
"responses": {}
},
"patch": {
"tags": [
"controller"
],
"operationId": "patch",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Request_PATCH"
}
}
}
},
"responses": {}
}
}
},
"components": {
"schemas": {
"Request_PUT": {
"required": [
"name"
],
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"Request_PATCH": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
Describe alternatives you’ve considered Obvious alternative is different request objects. Though this is annoyingly verbose for the above case.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Grouping Javax Validation Constraints - Baeldung
All javax validation constraints have an attribute named groups. When we add a constraint to an element, we can declare the name of...
Read more >Validation with Spring Boot - the Complete Guide - Reflectoring
Spring Boot's Bean Validation support comes with the validation ... Spring supports validation groups with the @Validated annotation:.
Read more >Will JAX-RS support validation groups? - Stack Overflow
It turns out that it does support validation groups. From the same JSR-339: The presence of @Valid will trigger validation of all the ......
Read more >Chapter 2. Validation step by step - Red Hat on GitHub
Groups allow you to restrict the set of constraints applied during validation. This makes for example wizard like validation possible where in each...
Read more >Are You Using @Valid and @Validated Annotations Wrong?
@Valid annotation comes from Java Bean Validation API mentioned above, ... it is a variant of @Valid with support for validation groups ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can you provide an example on how to achieve this using
@RequestBody
or@Parameter
?plan support it?