Skip to content

Support @Validated validation groups #510

Closed

Description

@Briggybros

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.

Activity

  1. bnasslahsen commented on Apr 3, 2020

    @bnasslahsen
    Collaborator

    @Briggybros,

    You can achieve your expected OpenAPI description, by using @RequestBody or @Parameter annotation.
    The feature you are asking will not be supported in short term.
    You can contribute, if you estimate its useful for the community.

  2. omarAbdelhadyok commented on Oct 14, 2020

    @omarAbdelhadyok

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

  3. bensaufley commented on Aug 31, 2021

    @bensaufley

    Always the worst to stumble on a thread that is asking for exactly what you need and the last comment is a question that's a year old 😞. @omarAbdelhadyok did you solve your use case?

  4. Briggybros commented on Sep 1, 2021

    @Briggybros
    Author

    @bensaufley A method like this might help:

    @RestController
    @RequestMapping("controller")
    public class Controller {
        @PutMapping
        public void put(
                @RequestBody
                @io.swagger.v3.oas.annotations.parameters.RequestBody(
                        content = @Content(
                                schema = @Schema(
                                        // See https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#schema
                                        implementation = Request.class,
                                        requiredProperties = {"name"}
                                )
                        )
                )
                @Validated(Request.PUT.class) Request request
        ) {}
    
        @PatchMapping
        public void patch(
                @RequestBody
                @io.swagger.v3.oas.annotations.parameters.RequestBody(
                        content = @Content(
                                schema = @Schema(
                                        // See https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#schema
                                        implementation = Request.class,
                                        requiredProperties = {}
                                )
                        )
                )
                @Validated(Request.PATCH.class) Request request
        ) {}
    }

    As with the original issue, this is still not DRY..

  5. Showleen commented on Dec 13, 2022

    @Showleen

    plan support it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions