Skip to content

Commit 10bd991

Browse files
authored
Merge pull request #2201 from uc4w6c/support_requiredMode
Support required mode
2 parents 534ad5e + 823cac2 commit 10bd991

File tree

6 files changed

+259
-3
lines changed

6 files changed

+259
-3
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import io.swagger.v3.oas.annotations.enums.ParameterStyle;
4747
import io.swagger.v3.oas.annotations.extensions.Extension;
4848
import io.swagger.v3.oas.annotations.media.ExampleObject;
49+
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
4950
import io.swagger.v3.oas.models.Components;
5051
import io.swagger.v3.oas.models.examples.Example;
5152
import io.swagger.v3.oas.models.media.ArraySchema;
@@ -326,7 +327,7 @@ private void setSchema(io.swagger.v3.oas.annotations.Parameter parameterDoc, Com
326327
catch (Exception e) {
327328
LOGGER.warn(Constants.GRACEFUL_EXCEPTION_OCCURRED, e);
328329
}
329-
if (schema == null) {
330+
if (schema == null && parameterDoc.array() != null) {
330331
schema = AnnotationsUtils.getSchema(parameterDoc.schema(), parameterDoc.array(), true, parameterDoc.array().schema().implementation(), components, jsonView).orElse(null);
331332
// default value not set by swagger-core for array !
332333
if (schema != null) {
@@ -358,7 +359,7 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
358359
WebConversionServiceProvider webConversionServiceProvider = optionalWebConversionServiceProvider.get();
359360
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && methodParameter.getParameterType().getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class) == null) {
360361
Class<?> springConvertedType = webConversionServiceProvider.getSpringConvertedType(methodParameter.getParameterType());
361-
if (!(String.class.equals(springConvertedType) && ((Class<?>) type).isEnum()) && requestBodyInfo==null)
362+
if (!(String.class.equals(springConvertedType) && ((Class<?>) type).isEnum()) && requestBodyInfo == null)
362363
type = springConvertedType;
363364
}
364365
}
@@ -619,7 +620,9 @@ public String description() {
619620

620621
@Override
621622
public boolean required() {
622-
return schema.required();
623+
return schema.requiredMode().equals(RequiredMode.AUTO) ?
624+
schema.required() :
625+
schema.requiredMode().equals(RequiredMode.REQUIRED);
623626
}
624627

625628
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package test.org.springdoc.api.v30.app206;
2+
3+
import java.util.Optional;
4+
5+
import javax.validation.constraints.Email;
6+
import javax.validation.constraints.Max;
7+
import javax.validation.constraints.Min;
8+
import javax.validation.constraints.NotBlank;
9+
import javax.validation.constraints.Pattern;
10+
import javax.validation.constraints.Size;
11+
12+
import io.swagger.v3.oas.annotations.media.Schema;
13+
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
14+
15+
public class PersonRequest {
16+
@Schema(requiredMode = RequiredMode.REQUIRED)
17+
private long id;
18+
19+
@Schema(requiredMode = RequiredMode.NOT_REQUIRED)
20+
private String firstName;
21+
22+
@NotBlank
23+
@Size(max = 10)
24+
private String lastName;
25+
26+
@Schema(requiredMode = RequiredMode.AUTO)
27+
private Optional<String> email;
28+
29+
@Schema(required = true)
30+
private int age;
31+
32+
public PersonRequest(long id, String firstName, String lastName, Optional<String> email, int age) {
33+
this.id = id;
34+
this.firstName = firstName;
35+
this.lastName = lastName;
36+
this.email = email;
37+
this.age = age;
38+
}
39+
40+
public long getId() {
41+
return id;
42+
}
43+
44+
public String getFirstName() {
45+
return firstName;
46+
}
47+
48+
public String getLastName() {
49+
return lastName;
50+
}
51+
52+
public Optional<String> getEmail() {
53+
return email;
54+
}
55+
56+
public int getAge() {
57+
return age;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package test.org.springdoc.api.v30.app206;
2+
3+
import java.util.Optional;
4+
5+
import javax.validation.constraints.NotBlank;
6+
import javax.validation.constraints.Size;
7+
8+
import io.swagger.v3.oas.annotations.media.Schema;
9+
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
10+
11+
public class PersonResponse {
12+
@Schema
13+
private long id;
14+
15+
@NotBlank
16+
@Size(max = 10)
17+
private String firstName;
18+
19+
@Schema(requiredMode = RequiredMode.REQUIRED)
20+
private String lastName;
21+
22+
@Schema(requiredMode = RequiredMode.REQUIRED)
23+
private Optional<String> email;
24+
25+
@Schema(required = true)
26+
private int age;
27+
28+
public PersonResponse(long id, String firstName, String lastName, Optional<String> email, int age) {
29+
this.id = id;
30+
this.firstName = firstName;
31+
this.lastName = lastName;
32+
this.email = email;
33+
this.age = age;
34+
}
35+
36+
public long getId() {
37+
return id;
38+
}
39+
40+
public String getFirstName() {
41+
return firstName;
42+
}
43+
44+
public String getLastName() {
45+
return lastName;
46+
}
47+
48+
public Optional<String> getEmail() {
49+
return email;
50+
}
51+
52+
public int getAge() {
53+
return age;
54+
}
55+
}
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.api.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)