Skip to content

Commit

Permalink
Split the plugin into seperate plugins for each of the annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
dilipkrish committed Feb 3, 2016
1 parent aa589c4 commit 8547929
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 282 deletions.
Expand Up @@ -18,12 +18,51 @@
*/
package springfox.bean.validators.plugins;

import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import org.springframework.core.Ordered;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

public class BeanValidators {
public final static int BEAN_VALIDATOR_PLUGIN_ORDER = Ordered.HIGHEST_PRECEDENCE + 500;

private BeanValidators() {
throw new UnsupportedOperationException();
}

public static <T extends Annotation> Optional<T> validatorFromBean(
ModelPropertyContext context,
Class<T> annotationType) {

Optional<BeanPropertyDefinition> propertyDefinition = context.getBeanPropertyDefinition();
Optional<T> notNull = Optional.absent();
if (propertyDefinition.isPresent()) {
notNull = FluentIterable
.from(propertyDefinition.get().getGetter().annotations())
.filter(annotationType)
.first()
.or(FluentIterable
.from(propertyDefinition.get().getField().annotations())
.filter(annotationType)
.first());
}
return notNull;
}

public static <T extends Annotation> Optional<T> validatorFromField(
ModelPropertyContext context,
Class<T> annotationType) {

Optional<AnnotatedElement> annotatedElement = context.getAnnotatedElement();
Optional<T> notNull = Optional.absent();
if (annotatedElement.isPresent()) {
notNull = Optional.fromNullable(annotatedElement.get().getAnnotation(annotationType));
}
return notNull;
}

}
Expand Up @@ -18,14 +18,11 @@
*/
package springfox.bean.validators.plugins;

import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ModelPropertyBuilder;
import springfox.documentation.service.AllowableRangeValues;
import springfox.documentation.service.AllowableValues;
import springfox.documentation.spi.DocumentationType;
Expand All @@ -34,8 +31,8 @@

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import static springfox.bean.validators.plugins.BeanValidators.*;

@Component
@Order(BeanValidators.BEAN_VALIDATOR_PLUGIN_ORDER)
Expand All @@ -51,94 +48,33 @@ public boolean supports(DocumentationType delimiter) {

@Override
public void apply(ModelPropertyContext context) {
ModelPropertyBuilder mybuilder = context.getBuilder();

Optional<BeanPropertyDefinition> beanPropDef = context.getBeanPropertyDefinition();
BeanPropertyDefinition beanDef = beanPropDef.get();
AnnotatedField field = beanDef.getField();

if (field != null) {

// add support for @NotNull
addRequiredForNotNull(mybuilder, field);

AllowableValues myvalues = null;

// add support for @Size
Size mySize = field.getAnnotation(Size.class);

if (mySize != null) {
myvalues = createAllowableValuesFromSizeForStrings(field, mySize);

} else {
// add support for @Min/@Max
Min myMin = field.getAnnotation(Min.class);
Max myMax = field.getAnnotation(Max.class);
if (myMin != null || myMax != null) {
myvalues = createAllowableValuesFromMinMaxForNumbers(field, myMin, myMax);
} else {

}

}

if (myvalues != null) {
mybuilder.allowableValues(myvalues);
}


}
Optional<Min> min = validatorFromBean(context, Min.class)
.or(validatorFromField(context, Min.class));
Optional<Max> max = validatorFromBean(context, Max.class)
.or(validatorFromField(context, Max.class));

// add support for @Min/@Max
context.getBuilder().allowableValues(createAllowableValuesFromMinMaxForNumbers(min, max));

}

private void addRequiredForNotNull(ModelPropertyBuilder mybuilder, AnnotatedField field) {
// add support for @NotNull
NotNull myNotNull = field.getAnnotation(NotNull.class);
if (myNotNull != null) {
LOG.debug("@NotNull detected: adding required to field " + field.getFullName());
mybuilder.required(true);
}
}

private AllowableValues createAllowableValuesFromSizeForStrings(AnnotatedField field, Size mySize) {
AllowableRangeValues myvalues = null;

LOG.debug("@Size detected: adding MinLength/MaxLength to field " + field.getFullName());

if (mySize.min() > 0 && mySize.max() < Integer.MAX_VALUE) {
myvalues = new AllowableRangeValues(Integer.toString(mySize.min()), Integer.toString(mySize.max()));

} else if (mySize.min() > 0) {
LOG.debug("@Size min detected: adding AllowableRangeValues to field " + field.getFullName());
// use Max value until "infinity" works
myvalues = new AllowableRangeValues(Integer.toString(mySize.min()), Integer.toString(Integer.MAX_VALUE));

} else if (mySize.max() < Integer.MAX_VALUE) {
// use Min value until "infinity" works
LOG.debug("@Size max detected: adding AllowableRangeValues to field " + field.getFullName());
myvalues = new AllowableRangeValues(Integer.toString(0), Integer.toString(mySize.max()));
}

return myvalues;
}

private AllowableValues createAllowableValuesFromMinMaxForNumbers(AnnotatedField field, Min myMin, Max myMax) {
private AllowableValues createAllowableValuesFromMinMaxForNumbers(Optional<Min> min, Optional<Max> max) {
AllowableRangeValues myvalues = null;

if (myMin != null && myMax != null) {
LOG.debug("@Min+@Max detected: adding AllowableRangeValues to field " + field.getFullName());
myvalues = new AllowableRangeValues(Double.toString(myMin.value()), Double.toString(myMax.value()));
if (min.isPresent() && max.isPresent()) {
LOG.debug("@Min+@Max detected: adding AllowableRangeValues to field ");
myvalues = new AllowableRangeValues(Double.toString(min.get().value()), Double.toString(max.get().value()));

} else if (myMin != null) {
LOG.debug("@Min detected: adding AllowableRangeValues to field " + field.getFullName());
} else if (min.isPresent()) {
LOG.debug("@Min detected: adding AllowableRangeValues to field ");
// use Max value until "infinity" works
myvalues = new AllowableRangeValues(Double.toString(myMin.value()), Double.toString(Double.MAX_VALUE));
myvalues = new AllowableRangeValues(Double.toString(min.get().value()), Double.toString(Double.MAX_VALUE));

} else if (myMax != null) {
} else if (max.isPresent()) {
// use Min value until "infinity" works
LOG.debug("@Max detected: adding AllowableRangeValues to field " + field.getFullName());
myvalues = new AllowableRangeValues(Double.toString(Double.MIN_VALUE), Double.toString(myMax.value()));
LOG.debug("@Max detected: adding AllowableRangeValues to field ");
myvalues = new AllowableRangeValues(Double.toString(Double.MIN_VALUE), Double.toString(max.get().value()));

}
return myvalues;
Expand Down
Expand Up @@ -18,31 +18,21 @@
*/
package springfox.bean.validators.plugins;

import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ModelPropertyBuilder;
import springfox.documentation.service.AllowableRangeValues;
import springfox.documentation.service.AllowableValues;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import static springfox.bean.validators.plugins.BeanValidators.*;

@Component
@Order(BeanValidators.BEAN_VALIDATOR_PLUGIN_ORDER)
public class NotNullAnnotationPlugin implements ModelPropertyBuilderPlugin {

private static final Logger LOG = LoggerFactory.getLogger(NotNullAnnotationPlugin.class);

@Override
public boolean supports(DocumentationType delimiter) {
// we simply support all documentationTypes!
Expand All @@ -51,98 +41,9 @@ public boolean supports(DocumentationType delimiter) {

@Override
public void apply(ModelPropertyContext context) {
ModelPropertyBuilder mybuilder = context.getBuilder();

Optional<BeanPropertyDefinition> beanPropDef = context.getBeanPropertyDefinition();
BeanPropertyDefinition beanDef = beanPropDef.get();
AnnotatedField field = beanDef.getField();

if (field != null) {

// add support for @NotNull
addRequiredForNotNull(mybuilder, field);

AllowableValues myvalues = null;

// add support for @Size
Size mySize = field.getAnnotation(Size.class);

if (mySize != null) {
myvalues = createAllowableValuesFromSizeForStrings(field, mySize);

} else {
// add support for @Min/@Max
Min myMin = field.getAnnotation(Min.class);
Max myMax = field.getAnnotation(Max.class);
if (myMin != null || myMax != null) {
myvalues = createAllowableValuesFromMinMaxForNumbers(field, myMin, myMax);
} else {

}

}

if (myvalues != null) {
mybuilder.allowableValues(myvalues);
}


}


Optional<NotNull> notNull = validatorFromBean(context, NotNull.class)
.or(validatorFromField(context, NotNull.class));
context.getBuilder().required(notNull.isPresent());
}

private void addRequiredForNotNull(ModelPropertyBuilder mybuilder, AnnotatedField field) {
// add support for @NotNull
NotNull myNotNull = field.getAnnotation(NotNull.class);
if (myNotNull != null) {
LOG.debug("@NotNull detected: adding required to field " + field.getFullName());
mybuilder.required(true);
}
}

private AllowableValues createAllowableValuesFromSizeForStrings(AnnotatedField field, Size mySize) {
AllowableRangeValues myvalues = null;

LOG.debug("@Size detected: adding MinLength/MaxLength to field " + field.getFullName());

if (mySize.min() > 0 && mySize.max() < Integer.MAX_VALUE) {
myvalues = new AllowableRangeValues(Integer.toString(mySize.min()), Integer.toString(mySize.max()));

} else if (mySize.min() > 0) {
LOG.debug("@Size min detected: adding AllowableRangeValues to field " + field.getFullName());
// use Max value until "infinity" works
myvalues = new AllowableRangeValues(Integer.toString(mySize.min()), Integer.toString(Integer.MAX_VALUE));

} else if (mySize.max() < Integer.MAX_VALUE) {
// use Min value until "infinity" works
LOG.debug("@Size max detected: adding AllowableRangeValues to field " + field.getFullName());
myvalues = new AllowableRangeValues(Integer.toString(0), Integer.toString(mySize.max()));
}

return myvalues;
}

private AllowableValues createAllowableValuesFromMinMaxForNumbers(AnnotatedField field, Min myMin, Max myMax) {
AllowableRangeValues myvalues = null;

if (myMin != null && myMax != null) {
LOG.debug("@Min+@Max detected: adding AllowableRangeValues to field " + field.getFullName());
myvalues = new AllowableRangeValues(Double.toString(myMin.value()), Double.toString(myMax.value()));

} else if (myMin != null) {
LOG.debug("@Min detected: adding AllowableRangeValues to field " + field.getFullName());
// use Max value until "infinity" works
myvalues = new AllowableRangeValues(Double.toString(myMin.value()), Double.toString(Double.MAX_VALUE));

} else if (myMax != null) {
// use Min value until "infinity" works
LOG.debug("@Max detected: adding AllowableRangeValues to field " + field.getFullName());
myvalues = new AllowableRangeValues(Double.toString(Double.MIN_VALUE), Double.toString(myMax.value()));

}
return myvalues;
}


}

0 comments on commit 8547929

Please sign in to comment.