Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ public static Optional<Header> getHeader(io.swagger.v3.oas.annotations.headers.H
return Optional.of(headerObject);
}

public static void setHeaderExplode (Header header, io.swagger.v3.oas.annotations.headers.Header h) {
public static void setHeaderExplode(Header header, io.swagger.v3.oas.annotations.headers.Header h) {
if (isHeaderExplodable(h, header)) {
if (Explode.TRUE.equals(h.explode())) {
header.setExplode(Boolean.TRUE);
Expand All @@ -1560,14 +1560,22 @@ private static boolean isHeaderExplodable(io.swagger.v3.oas.annotations.headers.
if (schema != null) {
Class implementation = schema.implementation();
if (implementation == Void.class) {
if (!schema.type().equals("object") && !schema.type().equals("array")) {
if (!isExplodableOAS30(schema)) {
explode = false;
}
}
}
return explode;
}

public static boolean isExplodableOAS30(io.swagger.v3.oas.annotations.media.Schema schema) {
return schema.type().equals("object") || schema.type().equals("array");
}

public static boolean isExplodableOAS31(io.swagger.v3.oas.annotations.media.Schema schema) {
return Arrays.asList(schema.types()).contains("object") || Arrays.asList(schema.types()).contains("array");
}

public static void addEncodingToMediaType(MediaType mediaType, io.swagger.v3.oas.annotations.media.Encoding encoding, JsonView jsonViewAnnotation) {
addEncodingToMediaType(mediaType, encoding, jsonViewAnnotation, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.Size;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.swagger.v3.core.util.ValidationAnnotationsUtils.JAVAX_SIZE;
import static io.swagger.v3.core.util.ValidationAnnotationsUtils.applySizeConstraint;

public class ParameterProcessor {

private static final String VALUE_METHOD = "value";
private static final String FORM_PARAMETER = "form";

static Logger LOGGER = LoggerFactory.getLogger(ParameterProcessor.class);

public static Parameter applyAnnotations(Parameter parameter, Type type, List<Annotation> annotations, Components components, String[] classTypes, String[] methodTypes, JsonView jsonViewAnnotation) {
Expand Down Expand Up @@ -150,24 +157,24 @@ public static Parameter applyAnnotations(
for (Annotation annotation : annotations) {
if (annotation.annotationType().getName().equals("javax.ws.rs.FormParam")) {
try {
String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
String name = (String) annotation.annotationType().getMethod(VALUE_METHOD).invoke(annotation);
if (StringUtils.isNotBlank(name)) {
parameter.setName(name);
}
} catch (Exception e) {
}
// set temporarily to "form" to inform caller that we need to further process along other form parameters
parameter.setIn("form");
parameter.setIn(FORM_PARAMETER);
} else if (annotation.annotationType().getName().endsWith("FormDataParam")) {
try {
String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
String name = (String) annotation.annotationType().getMethod(VALUE_METHOD).invoke(annotation);
if (StringUtils.isNotBlank(name)) {
parameter.setName(name);
}
} catch (Exception e) {
}
// set temporarily to "form" to inform caller that we need to further process along other form parameters
parameter.setIn("form");
parameter.setIn(FORM_PARAMETER);
}
}

Expand Down Expand Up @@ -243,27 +250,21 @@ public static Parameter applyAnnotations(

} else if (annotation.annotationType().getName().equals("javax.ws.rs.PathParam")) {
try {
String name = (String) annotation.annotationType().getMethod("value").invoke(annotation);
String name = (String) annotation.annotationType().getMethod(VALUE_METHOD).invoke(annotation);
if (StringUtils.isNotBlank(name)) {
parameter.setName(name);
}
} catch (Exception e) {
} catch (Exception ignored) {
}
} else if (annotation.annotationType().getName().equals("javax.validation.constraints.Size")) {
} else if (annotation.annotationType().getName().equals(JAVAX_SIZE)) {
try {
if (parameter.getSchema() == null) {
parameter.setSchema(new ArraySchema());
}
if (isArraySchema(parameter.getSchema())) {
Schema as = parameter.getSchema();
Integer min = (Integer) annotation.annotationType().getMethod("min").invoke(annotation);
if (min != null) {
as.setMinItems(min);
}
Integer max = (Integer) annotation.annotationType().getMethod("max").invoke(annotation);
if (max != null) {
as.setMaxItems(max);
}
Size size = (Size) annotation;
applySizeConstraint(as, size);
}

} catch (Exception e) {
Expand Down Expand Up @@ -296,7 +297,7 @@ public static Parameter applyAnnotations(
}

public static boolean isArraySchema(Schema schema) {
return "array".equals(schema.getType()) || (schema.getTypes() != null && schema.getTypes().contains("array"));
return SchemaTypeUtils.isArraySchema(schema);
}

public static void setParameterExplode(Parameter parameter, io.swagger.v3.oas.annotations.Parameter p) {
Expand All @@ -315,11 +316,10 @@ private static boolean isExplodable(io.swagger.v3.oas.annotations.Parameter p, P
if (schema != null) {
Class implementation = schema.implementation();
if (implementation == Void.class) {
if (!schema.type().equals("object") && !schema.type().equals("array") && !schema.type().isEmpty()) {
if (!AnnotationsUtils.isExplodableOAS30(schema) && !schema.type().isEmpty()) {
explode = false;
}
if (schema.types().length != 0 &&
(!Arrays.asList(schema.types()).contains("array") && !Arrays.asList(schema.types()).contains("object"))) {
if (schema.types().length != 0 && !AnnotationsUtils.isExplodableOAS31(schema)) {
explode = false;
}
}
Expand Down Expand Up @@ -437,7 +437,7 @@ public AnnotationsHelper(List<Annotation> annotations, Type _type) {
context = true;
} else if ("javax.ws.rs.DefaultValue".equals(item.annotationType().getName())) {
try {
rsDefault = (String) item.annotationType().getMethod("value").invoke(item);
rsDefault = (String) item.annotationType().getMethod(VALUE_METHOD).invoke(item);
} catch (Exception ex) {
LOGGER.error("Invocation of value method failed", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5334,6 +5334,21 @@ public void testTicket4879() {
" description: default response\n" +
" content:\n" +
" '*/*': {}\n" +
" /test/teststringsize:\n" +
" get:\n" +
" operationId: testStringSize\n" +
" requestBody:\n" +
" content:\n" +
" '*/*':\n" +
" schema:\n" +
" type: string\n" +
" maxLength: 50\n" +
" minLength: 1\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" '*/*': {}\n" +
"components:\n" +
" schemas:\n" +
" DefaultClass:\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public void testDefault(
@Path("/testsize")
public void testSize(@Size(min = 1, max = 100) List<String> myList) {}

@GET
@Path("/teststringsize")
public void testStringSize(@Size(min = 1, max = 50) String myString) {}

public static class DefaultClass {
@Schema(defaultValue = "true")
public Boolean name;
Expand Down