Skip to content
Merged
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 @@ -448,11 +448,14 @@ protected void applyBeanValidatorAnnotations(Property property, Annotation[] ann
AbstractNumericProperty ap = (AbstractNumericProperty) property;
ap.setMinimum(new Double(size.min()));
ap.setMaximum(new Double(size.max()));
}
if (property instanceof StringProperty) {
} else if (property instanceof StringProperty) {
StringProperty sp = (StringProperty) property;
sp.minLength(new Integer(size.min()));
sp.maxLength(new Integer(size.max()));
} else if (property instanceof ArrayProperty) {
ArrayProperty sp = (ArrayProperty) property;
sp.setMinItems(size.min());
sp.setMaxItems(size.max());
}
}
if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import io.swagger.models.properties.Property;
import io.swagger.models.properties.PropertyBuilder;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,6 +23,8 @@
import java.util.List;
import java.util.Map;

import javax.validation.constraints.Size;

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

Expand Down Expand Up @@ -56,6 +56,12 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
if (StringUtils.isNotEmpty(param.getDataType())) {
p.setType(param.getDataType());
}
if (helper.getMinItems() != null) {
p.setMinItems(helper.getMinItems());
}
if (helper.getMaxItems() != null) {
p.setMaxItems(helper.getMaxItems());
}

AllowableValues allowableValues = AllowableValuesUtils.create(param.getAllowableValues());

Expand Down Expand Up @@ -184,6 +190,8 @@ private static class AnnotationsHelper {
private boolean context;
private ParamWrapper<?> apiParam = new ApiParamWrapper(DEFAULT_API_PARAM);
private String defaultValue;
private Integer minItems;
private Integer maxItems;

/**
* Constructs an instance.
Expand All @@ -205,6 +213,10 @@ public AnnotationsHelper(List<Annotation> annotations) {
} catch (Exception ex) {
LOGGER.error("Invocation of value method failed", ex);
}
} else if (item instanceof Size) {
final Size size = (Size) item;
minItems = size.min();
maxItems = size.max();
}
}
defaultValue = StringUtils.isNotEmpty(apiParam.getDefaultValue()) ? apiParam.getDefaultValue() : rsDefault;
Expand Down Expand Up @@ -253,6 +265,14 @@ public ParamWrapper<?> getApiParam() {
public String getDefaultValue() {
return defaultValue;
}

public Integer getMinItems() {
return minItems;
}

public Integer getMaxItems() {
return maxItems;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import io.swagger.converter.ModelConverters;
import io.swagger.models.BeanValidationsModel;
import io.swagger.models.Model;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.DoubleProperty;
import io.swagger.models.properties.IntegerProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;

import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -36,5 +38,9 @@ public void readBeanValidatorTest() {

final DoubleProperty maxBalance = (DoubleProperty) properties.get("maxBalance");
Assert.assertTrue(maxBalance.getExclusiveMaximum());

final ArrayProperty items = (ArrayProperty) properties.get("items");
Assert.assertEquals((int) items.getMinItems(), 2);
Assert.assertEquals((int) items.getMaxItems(), 10);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.annotations.ApiParam;
import io.swagger.models.ModelImpl;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.HeaderParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;
import io.swagger.models.parameters.QueryParameter;
Expand All @@ -21,7 +22,9 @@
import java.util.Collections;
import java.util.List;

import javax.validation.constraints.Size;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
Expand Down Expand Up @@ -64,6 +67,10 @@ private void rangedParametrizedMethod(

}

private void arrayParametrizedMethod(@HeaderParam("ids") @Size(min = 5, max = 10) List<Long> ids) {

}

@Test(description = "parse parameters from method")
public void parameterProcessorTest() throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod("parametrizedMethod", String.class, List.class,
Expand Down Expand Up @@ -176,4 +183,17 @@ public void resourceWithParamRangeTest() throws NoSuchMethodException {
Assert.assertTrue(items.getExclusiveMinimum());
Assert.assertTrue(items.getExclusiveMaximum());
}

@Test
public void resourceWithArrayParamTest() throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod("arrayParametrizedMethod", List.class);
final Type[] genericParameterTypes = method.getGenericParameterTypes();
final Annotation[][] paramAnnotations = method.getParameterAnnotations();

final HeaderParameter param = (HeaderParameter) ParameterProcessor.applyAnnotations(null, new HeaderParameter(),
genericParameterTypes[0], Arrays.asList(paramAnnotations[0]));
Assert.assertNotNull(param);
Assert.assertEquals((int) param.getMinItems(), 5);
Assert.assertEquals((int) param.getMaxItems(), 10);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.swagger.models;

import java.util.List;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
Expand Down Expand Up @@ -34,6 +36,9 @@ public class BeanValidationsModel {

protected Integer birthYear;

@Size(min = 2, max = 10)
private List<String> items;

public Long getId() {
return id;
}
Expand Down Expand Up @@ -105,4 +110,12 @@ public Integer getBirthYear() {
public void setBirthYear(Integer birthYear) {
this.birthYear = birthYear;
}
}

public List<String> getItems() {
return items;
}

public void setItems(List<String> items) {
this.items = items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

import java.util.List;

@JsonPropertyOrder({"name", "in", "description", "required", "type", "items", "collectionFormat", "default", "maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum"})
@JsonPropertyOrder({"name", "in", "description", "required", "type", "items", "collectionFormat", "default",
"maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum", "maxItems", "minItems"})
public abstract class AbstractSerializableParameter<T extends AbstractSerializableParameter<T>> extends AbstractParameter implements SerializableParameter {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractSerializableParameter.class);
protected String type;
Expand All @@ -27,6 +28,8 @@ public abstract class AbstractSerializableParameter<T extends AbstractSerializab
protected Double maximum;
protected Boolean exclusiveMinimum;
protected Double minimum;
private Integer maxItems;
private Integer minItems;

@JsonIgnore
protected String defaultValue;
Expand Down Expand Up @@ -198,6 +201,22 @@ public void setMinimum(Double minimum) {
this.minimum = minimum;
}

public Integer getMaxItems() {
return maxItems;
}

public void setMaxItems(Integer maxItems) {
this.maxItems = maxItems;
}

public Integer getMinItems() {
return minItems;
}

public void setMinItems(Integer minItems) {
this.minItems = minItems;
}

@JsonIgnore
private T castThis() {
@SuppressWarnings("unchecked")
Expand All @@ -223,6 +242,8 @@ public int hashCode() {
result = prime * result + ((maximum == null) ? 0 : maximum.hashCode());
result = prime * result + ((minimum == null) ? 0 : minimum.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
result = prime * result + ((maxItems == null) ? 0 : maxItems.hashCode());
result = prime * result + ((minItems == null) ? 0 : minItems.hashCode());
return result;
}

Expand Down Expand Up @@ -308,6 +329,20 @@ public boolean equals(Object obj) {
} else if (!type.equals(other.type)) {
return false;
}
if (maxItems == null) {
if (other.maxItems != null) {
return false;
}
} else if (!maxItems.equals(other.maxItems)) {
return false;
}
if (minItems == null) {
if (other.minItems != null) {
return false;
}
} else if (!minItems.equals(other.minItems)) {
return false;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class ArrayProperty extends AbstractProperty implements Property {
public static final String TYPE = "array";
protected Boolean uniqueItems;
protected Property items;
private Integer maxItems;
private Integer minItems;

public ArrayProperty() {
super.type = TYPE;
Expand Down Expand Up @@ -56,12 +58,30 @@ public void setUniqueItems(Boolean uniqueItems) {
this.uniqueItems = uniqueItems ? true : null;
}

public Integer getMaxItems() {
return maxItems;
}

public void setMaxItems(Integer maxItems) {
this.maxItems = maxItems;
}

public Integer getMinItems() {
return minItems;
}

public void setMinItems(Integer minItems) {
this.minItems = minItems;
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((items == null) ? 0 : items.hashCode());
result = prime * result + ((uniqueItems == null) ? 0 : uniqueItems.hashCode());
result = prime * result + ((maxItems == null) ? 0 : maxItems.hashCode());
result = prime * result + ((minItems == null) ? 0 : minItems.hashCode());
return result;
}

Expand All @@ -88,6 +108,20 @@ public boolean equals(Object obj) {
} else if (!uniqueItems.equals(other.uniqueItems)) {
return false;
}
if (maxItems == null) {
if (other.maxItems != null) {
return false;
}
} else if (!maxItems.equals(other.maxItems)) {
return false;
}
if (minItems == null) {
if (other.minItems != null) {
return false;
}
} else if (!minItems.equals(other.minItems)) {
return false;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,24 @@ public Model toModel(Property property) {
}
return null;
}

@Override
public Property merge(final Property property, final Map<PropertyId, Object> args) {
super.merge(property, args);
if (property instanceof ArrayProperty) {
final ArrayProperty resolved = (ArrayProperty) property;
if (args.containsKey(PropertyId.MIN_ITEMS)) {
final Integer value = PropertyId.MIN_ITEMS.findValue(args);
resolved.setMinItems(value);
}
if (args.containsKey(PropertyId.MAX_ITEMS)) {
final Integer value = PropertyId.MAX_ITEMS.findValue(args);
resolved.setMaxItems(value);
}
}

return property;
}
},
MAP(MapProperty.class) {
@Override
Expand Down