Skip to content

Commit 6e554e7

Browse files
committed
Support schema.requiredMode() on ParameterObject. springdoc#2200
1 parent e792cea commit 6e554e7

File tree

6 files changed

+251
-1
lines changed

6 files changed

+251
-1
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import io.swagger.v3.oas.annotations.enums.ParameterStyle;
4949
import io.swagger.v3.oas.annotations.extensions.Extension;
5050
import io.swagger.v3.oas.annotations.media.ExampleObject;
51+
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
5152
import io.swagger.v3.oas.models.Components;
5253
import io.swagger.v3.oas.models.examples.Example;
5354
import io.swagger.v3.oas.models.media.ArraySchema;
@@ -626,7 +627,9 @@ public String description() {
626627

627628
@Override
628629
public boolean required() {
629-
return schema.required();
630+
return schema.requiredMode().equals(RequiredMode.AUTO) ?
631+
schema.required() :
632+
schema.requiredMode().equals(RequiredMode.REQUIRED);
630633
}
631634

632635
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package test.org.springdoc.api.v30.app206;
2+
3+
import java.util.Optional;
4+
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
7+
import jakarta.validation.constraints.NotBlank;
8+
import jakarta.validation.constraints.Size;
9+
10+
public class PersonRequest {
11+
@Schema(requiredMode = RequiredMode.REQUIRED)
12+
private long id;
13+
14+
@Schema(requiredMode = RequiredMode.NOT_REQUIRED)
15+
private String firstName;
16+
17+
@NotBlank
18+
@Size(max = 10)
19+
private String lastName;
20+
21+
@Schema(requiredMode = RequiredMode.AUTO)
22+
private Optional<String> email;
23+
24+
@Schema(required = true)
25+
private int age;
26+
27+
public PersonRequest(long id, String firstName, String lastName, Optional<String> email, int age) {
28+
this.id = id;
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
this.email = email;
32+
this.age = age;
33+
}
34+
35+
public long getId() {
36+
return id;
37+
}
38+
39+
public String getFirstName() {
40+
return firstName;
41+
}
42+
43+
public String getLastName() {
44+
return lastName;
45+
}
46+
47+
public Optional<String> getEmail() {
48+
return email;
49+
}
50+
51+
public int getAge() {
52+
return age;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package test.org.springdoc.api.v30.app206;
2+
3+
import java.util.Optional;
4+
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
7+
import jakarta.validation.constraints.NotBlank;
8+
import jakarta.validation.constraints.Size;
9+
10+
public class PersonResponse {
11+
@Schema
12+
private long id;
13+
14+
@NotBlank
15+
@Size(max = 10)
16+
private String firstName;
17+
18+
@Schema(requiredMode = RequiredMode.REQUIRED)
19+
private String lastName;
20+
21+
@Schema(requiredMode = RequiredMode.REQUIRED)
22+
private Optional<String> email;
23+
24+
@Schema(required = true)
25+
private int age;
26+
27+
public PersonResponse(long id, String firstName, String lastName, Optional<String> email, int age) {
28+
this.id = id;
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
this.email = email;
32+
this.age = age;
33+
}
34+
35+
public long getId() {
36+
return id;
37+
}
38+
39+
public String getFirstName() {
40+
return firstName;
41+
}
42+
43+
public String getLastName() {
44+
return lastName;
45+
}
46+
47+
public Optional<String> getEmail() {
48+
return email;
49+
}
50+
51+
public int getAge() {
52+
return age;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.v30.app206;
2+
3+
import org.springdoc.core.annotations.ParameterObject;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class RequiredModeController {
10+
@GetMapping
11+
public PersonResponse index(@ParameterObject PersonRequest request) {
12+
return null;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.v30.app206;
2+
3+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
public class SpringdocApp206Test extends AbstractSpringDocV30Test {
8+
9+
@SpringBootApplication
10+
static class SpringDocTestApp {}
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/": {
15+
"get": {
16+
"tags": [
17+
"required-mode-controller"
18+
],
19+
"operationId": "index",
20+
"parameters": [
21+
{
22+
"name": "id",
23+
"in": "query",
24+
"required": true,
25+
"schema": {
26+
"type": "string"
27+
}
28+
},
29+
{
30+
"name": "firstName",
31+
"in": "query",
32+
"required": false,
33+
"schema": {
34+
"type": "string"
35+
}
36+
},
37+
{
38+
"name": "lastName",
39+
"in": "query",
40+
"required": true,
41+
"schema": {
42+
"maxLength": 10,
43+
"minLength": 0,
44+
"type": "string"
45+
}
46+
},
47+
{
48+
"name": "email",
49+
"in": "query",
50+
"required": false,
51+
"schema": {
52+
"type": "string"
53+
}
54+
},
55+
{
56+
"name": "age",
57+
"in": "query",
58+
"required": true,
59+
"schema": {
60+
"type": "string"
61+
}
62+
}
63+
],
64+
"responses": {
65+
"200": {
66+
"description": "OK",
67+
"content": {
68+
"*/*": {
69+
"schema": {
70+
"$ref": "#/components/schemas/PersonResponse"
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
},
79+
"components": {
80+
"schemas": {
81+
"PersonResponse": {
82+
"required": [
83+
"age",
84+
"email",
85+
"firstName",
86+
"lastName"
87+
],
88+
"type": "object",
89+
"properties": {
90+
"id": {
91+
"type": "integer",
92+
"format": "int64"
93+
},
94+
"firstName": {
95+
"maxLength": 10,
96+
"minLength": 0,
97+
"type": "string"
98+
},
99+
"lastName": {
100+
"type": "string"
101+
},
102+
"email": {
103+
"type": "string"
104+
},
105+
"age": {
106+
"type": "integer",
107+
"format": "int32"
108+
}
109+
}
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)