All Downloads are FREE. Search and download functionalities are using the official Maven repository.

springfox.bean.validators.plugins.BeanValidators Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 *
 *  Copyright 2015 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */
package springfox.bean.validators.plugins;

import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.annotations.VisibleForTesting;
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  Optional validatorFromBean(
      ModelPropertyContext context,
      Class annotationType) {

    Optional propertyDefinition = context.getBeanPropertyDefinition();
    Optional notNull = Optional.absent();
    if (propertyDefinition.isPresent()) {
      notNull = annotationFrom(propertyDefinition.get().getGetter(), annotationType)
                .or(annotationFrom(propertyDefinition.get().getField(), annotationType));
    }
    return notNull;
  }

  public static  Optional validatorFromField(
      ModelPropertyContext context,
      Class annotationType) {

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

  @VisibleForTesting
  static  Optional annotationFrom(
      AnnotatedMember nullableMember,
      Class annotationType) {

    Optional member = Optional.fromNullable(nullableMember);
    Optional notNull = Optional.absent();
    if (member.isPresent()) {
      notNull = FluentIterable.from(member.get().annotations())
          .filter(annotationType)
          .first();
    }
    return notNull;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy