Skip to content

Commit 0456333

Browse files
Mattias-Sehlstedtewaostrowska
authored andcommitted
refactor how the size constraint is applied to a parameter
1 parent 3950835 commit 0456333

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ public static Optional<Header> getHeader(io.swagger.v3.oas.annotations.headers.H
15691569
return Optional.of(headerObject);
15701570
}
15711571

1572-
public static void setHeaderExplode (Header header, io.swagger.v3.oas.annotations.headers.Header h) {
1572+
public static void setHeaderExplode(Header header, io.swagger.v3.oas.annotations.headers.Header h) {
15731573
if (isHeaderExplodable(h, header)) {
15741574
if (Explode.TRUE.equals(h.explode())) {
15751575
header.setExplode(Boolean.TRUE);
@@ -1585,14 +1585,22 @@ private static boolean isHeaderExplodable(io.swagger.v3.oas.annotations.headers.
15851585
if (schema != null) {
15861586
Class implementation = schema.implementation();
15871587
if (implementation == Void.class) {
1588-
if (!schema.type().equals("object") && !schema.type().equals("array")) {
1588+
if (!isExplodableOAS30(schema)) {
15891589
explode = false;
15901590
}
15911591
}
15921592
}
15931593
return explode;
15941594
}
15951595

1596+
public static boolean isExplodableOAS30(io.swagger.v3.oas.annotations.media.Schema schema) {
1597+
return schema.type().equals("object") || schema.type().equals("array");
1598+
}
1599+
1600+
public static boolean isExplodableOAS31(io.swagger.v3.oas.annotations.media.Schema schema) {
1601+
return Arrays.asList(schema.types()).contains("object") || Arrays.asList(schema.types()).contains("array");
1602+
}
1603+
15961604
public static void addEncodingToMediaType(MediaType mediaType, io.swagger.v3.oas.annotations.media.Encoding encoding, JsonView jsonViewAnnotation) {
15971605
addEncodingToMediaType(mediaType, encoding, jsonViewAnnotation, false);
15981606
}

modules/swagger-core/src/main/java/io/swagger/v3/core/util/ParameterProcessor.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@
1717
import org.slf4j.Logger;
1818
import org.slf4j.LoggerFactory;
1919

20+
import javax.validation.constraints.Size;
2021
import java.io.IOException;
2122
import java.lang.annotation.Annotation;
2223
import java.lang.reflect.Type;
2324
import java.util.ArrayList;
24-
import java.util.Arrays;
2525
import java.util.LinkedHashMap;
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.Optional;
2929

30+
import static io.swagger.v3.core.util.ValidationAnnotationsUtils.JAVAX_SIZE;
31+
import static io.swagger.v3.core.util.ValidationAnnotationsUtils.applySizeConstraint;
32+
3033
public class ParameterProcessor {
34+
35+
private static final String VALUE_METHOD = "value";
36+
private static final String FORM_PARAMETER = "form";
37+
3138
static Logger LOGGER = LoggerFactory.getLogger(ParameterProcessor.class);
3239

3340
public static Parameter applyAnnotations(Parameter parameter, Type type, List<Annotation> annotations, Components components, String[] classTypes, String[] methodTypes, JsonView jsonViewAnnotation) {
@@ -150,24 +157,24 @@ public static Parameter applyAnnotations(
150157
for (Annotation annotation : annotations) {
151158
if (annotation.annotationType().getName().equals("javax.ws.rs.FormParam")) {
152159
try {
153-
String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
160+
String name = (String) annotation.annotationType().getMethod(VALUE_METHOD).invoke(annotation);
154161
if (StringUtils.isNotBlank(name)) {
155162
parameter.setName(name);
156163
}
157164
} catch (Exception e) {
158165
}
159166
// set temporarily to "form" to inform caller that we need to further process along other form parameters
160-
parameter.setIn("form");
167+
parameter.setIn(FORM_PARAMETER);
161168
} else if (annotation.annotationType().getName().endsWith("FormDataParam")) {
162169
try {
163-
String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
170+
String name = (String) annotation.annotationType().getMethod(VALUE_METHOD).invoke(annotation);
164171
if (StringUtils.isNotBlank(name)) {
165172
parameter.setName(name);
166173
}
167174
} catch (Exception e) {
168175
}
169176
// set temporarily to "form" to inform caller that we need to further process along other form parameters
170-
parameter.setIn("form");
177+
parameter.setIn(FORM_PARAMETER);
171178
}
172179
}
173180

@@ -243,27 +250,21 @@ public static Parameter applyAnnotations(
243250

244251
} else if (annotation.annotationType().getName().equals("javax.ws.rs.PathParam")) {
245252
try {
246-
String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
253+
String name = (String) annotation.annotationType().getMethod(VALUE_METHOD).invoke(annotation);
247254
if (StringUtils.isNotBlank(name)) {
248255
parameter.setName(name);
249256
}
250-
} catch (Exception e) {
257+
} catch (Exception ignored) {
251258
}
252-
} else if (annotation.annotationType().getName().equals("javax.validation.constraints.Size")) {
259+
} else if (annotation.annotationType().getName().equals(JAVAX_SIZE)) {
253260
try {
254261
if (parameter.getSchema() == null) {
255262
parameter.setSchema(new ArraySchema());
256263
}
257264
if (isArraySchema(parameter.getSchema())) {
258265
Schema as = parameter.getSchema();
259-
Integer min = (Integer) annotation.annotationType().getMethod("min").invoke(annotation);
260-
if (min != null) {
261-
as.setMinItems(min);
262-
}
263-
Integer max = (Integer) annotation.annotationType().getMethod("max").invoke(annotation);
264-
if (max != null) {
265-
as.setMaxItems(max);
266-
}
266+
Size size = (Size) annotation;
267+
applySizeConstraint(as, size);
267268
}
268269

269270
} catch (Exception e) {
@@ -296,7 +297,7 @@ public static Parameter applyAnnotations(
296297
}
297298

298299
public static boolean isArraySchema(Schema schema) {
299-
return "array".equals(schema.getType()) || (schema.getTypes() != null && schema.getTypes().contains("array"));
300+
return SchemaTypeUtils.isArraySchema(schema);
300301
}
301302

302303
public static void setParameterExplode(Parameter parameter, io.swagger.v3.oas.annotations.Parameter p) {
@@ -315,11 +316,10 @@ private static boolean isExplodable(io.swagger.v3.oas.annotations.Parameter p, P
315316
if (schema != null) {
316317
Class implementation = schema.implementation();
317318
if (implementation == Void.class) {
318-
if (!schema.type().equals("object") && !schema.type().equals("array") && !schema.type().isEmpty()) {
319+
if (!AnnotationsUtils.isExplodableOAS30(schema) && !schema.type().isEmpty()) {
319320
explode = false;
320321
}
321-
if (schema.types().length != 0 &&
322-
(!Arrays.asList(schema.types()).contains("array") && !Arrays.asList(schema.types()).contains("object"))) {
322+
if (schema.types().length != 0 && !AnnotationsUtils.isExplodableOAS31(schema)) {
323323
explode = false;
324324
}
325325
}
@@ -437,7 +437,7 @@ public AnnotationsHelper(List<Annotation> annotations, Type _type) {
437437
context = true;
438438
} else if ("javax.ws.rs.DefaultValue".equals(item.annotationType().getName())) {
439439
try {
440-
rsDefault = (String) item.annotationType().getMethod("value").invoke(item);
440+
rsDefault = (String) item.annotationType().getMethod(VALUE_METHOD).invoke(item);
441441
} catch (Exception ex) {
442442
LOGGER.error("Invocation of value method failed", ex);
443443
}

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5334,6 +5334,21 @@ public void testTicket4879() {
53345334
" description: default response\n" +
53355335
" content:\n" +
53365336
" '*/*': {}\n" +
5337+
" /test/teststringsize:\n" +
5338+
" get:\n" +
5339+
" operationId: testStringSize\n" +
5340+
" requestBody:\n" +
5341+
" content:\n" +
5342+
" '*/*':\n" +
5343+
" schema:\n" +
5344+
" type: string\n" +
5345+
" maxLength: 50\n" +
5346+
" minLength: 1\n" +
5347+
" responses:\n" +
5348+
" default:\n" +
5349+
" description: default response\n" +
5350+
" content:\n" +
5351+
" '*/*': {}\n" +
53375352
"components:\n" +
53385353
" schemas:\n" +
53395354
" DefaultClass:\n" +

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/resources/Ticket4879Resource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public void testDefault(
2828
@Path("/testsize")
2929
public void testSize(@Size(min = 1, max = 100) List<String> myList) {}
3030

31+
@GET
32+
@Path("/teststringsize")
33+
public void testStringSize(@Size(min = 1, max = 50) String myString) {}
34+
3135
public static class DefaultClass {
3236
@Schema(defaultValue = "true")
3337
public Boolean name;

0 commit comments

Comments
 (0)