question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support @Validated validation groups

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
omarAbdelhadyokcommented, Oct 14, 2020

Can you provide an example on how to achieve this using @RequestBody or @Parameter?

0reactions
Showleencommented, Dec 13, 2022

plan support it?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found