Repository navigation
Closed
Description
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.
Reacted by devopsix, Pascal Gehl, Charly, Sebastian Kirchner, Tuan Tran, Ben Saufley, Jan Gassen, tapioko, Dominique Tipton, Tomasz Gawel and 5 more
Reacted by Tomasz Gawel
Reacted by Tomasz Gawel
Reacted by Tomasz Gawel
Metadata
Metadata
Assignees
Labels
No labels
Activity
bnasslahsen commentedon Apr 3, 2020
@Briggybros,
You can achieve your expected OpenAPI description, by using
@RequestBodyor@Parameterannotation.The feature you are asking will not be supported in short term.
You can contribute, if you estimate its useful for the community.
omarAbdelhadyok commentedon Oct 14, 2020
Can you provide an example on how to achieve this using
@RequestBodyor@Parameter?bensaufley commentedon Aug 31, 2021
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?
Briggybros commentedon Sep 1, 2021
@bensaufley A method like this might help:
As with the original issue, this is still not DRY..
Showleen commentedon Dec 13, 2022
plan support it?