Search in sources :

Example 1 with AbstractNumericProperty

use of io.swagger.models.properties.AbstractNumericProperty in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method applyBeanValidatorAnnotations.

protected void applyBeanValidatorAnnotations(Property property, Annotation[] annotations) {
    Map<String, Annotation> annos = new HashMap<>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    if (annos.containsKey("javax.validation.constraints.NotNull")) {
        property.setRequired(true);
    }
    if (annos.containsKey("javax.validation.constraints.Min")) {
        if (property instanceof AbstractNumericProperty) {
            Min min = (Min) annos.get("javax.validation.constraints.Min");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new Double(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Max")) {
        if (property instanceof AbstractNumericProperty) {
            Max max = (Max) annos.get("javax.validation.constraints.Max");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new Double(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Size")) {
        Size size = (Size) annos.get("javax.validation.constraints.Size");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new Double(size.min()));
            ap.setMaximum(new Double(size.max()));
        } 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")) {
        DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new Double(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
        DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new Double(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Pattern")) {
        Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
        if (property instanceof StringProperty) {
            StringProperty ap = (StringProperty) property;
            ap.setPattern(pattern.regexp());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) StringProperty(io.swagger.models.properties.StringProperty) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) Min(javax.validation.constraints.Min) DecimalMin(javax.validation.constraints.DecimalMin) DecimalMax(javax.validation.constraints.DecimalMax)

Example 2 with AbstractNumericProperty

use of io.swagger.models.properties.AbstractNumericProperty in project swagger-core by swagger-api.

the class ModelResolver method applyBeanValidatorAnnotations.

protected void applyBeanValidatorAnnotations(Property property, Annotation[] annotations) {
    Map<String, Annotation> annos = new HashMap<String, Annotation>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    if (annos.containsKey("javax.validation.constraints.NotNull")) {
        property.setRequired(true);
    }
    if (annos.containsKey("javax.validation.constraints.Min")) {
        if (property instanceof AbstractNumericProperty) {
            Min min = (Min) annos.get("javax.validation.constraints.Min");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new BigDecimal(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Max")) {
        if (property instanceof AbstractNumericProperty) {
            Max max = (Max) annos.get("javax.validation.constraints.Max");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new BigDecimal(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Size")) {
        Size size = (Size) annos.get("javax.validation.constraints.Size");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new BigDecimal(size.min()));
            ap.setMaximum(new BigDecimal(size.max()));
        } 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")) {
        DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new BigDecimal(min.value()));
            ap.setExclusiveMinimum(!min.inclusive());
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
        DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new BigDecimal(max.value()));
            ap.setExclusiveMaximum(!max.inclusive());
        }
    }
    if (annos.containsKey("javax.validation.constraints.Pattern")) {
        Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
        if (property instanceof StringProperty) {
            StringProperty ap = (StringProperty) property;
            ap.setPattern(pattern.regexp());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) StringProperty(io.swagger.models.properties.StringProperty) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) BigDecimal(java.math.BigDecimal) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) Min(javax.validation.constraints.Min) DecimalMin(javax.validation.constraints.DecimalMin) DecimalMax(javax.validation.constraints.DecimalMax)

Example 3 with AbstractNumericProperty

use of io.swagger.models.properties.AbstractNumericProperty in project swagger-core by swagger-api.

the class BeanValidator method resolveProperty.

@Override
public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> chain) {
    Map<String, Annotation> annos = new HashMap<String, Annotation>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    Property property = null;
    if (chain.hasNext()) {
        property = chain.next().resolveProperty(type, context, annotations, chain);
    }
    if (property != null) {
        if (annos.containsKey("org.hibernate.validator.constraints.NotEmpty")) {
            property.setRequired(true);
            if (property instanceof StringProperty) {
                ((StringProperty) property).minLength(1);
            } else if (property instanceof ArrayProperty) {
                ((ArrayProperty) property).setMinItems(1);
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.NotBlank")) {
            property.setRequired(true);
            if (property instanceof StringProperty) {
                ((StringProperty) property).minLength(1);
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.Range")) {
            if (property instanceof AbstractNumericProperty) {
                Range range = (Range) annos.get("org.hibernate.validator.constraints.Range");
                AbstractNumericProperty ap = (AbstractNumericProperty) property;
                ap.setMinimum(new BigDecimal(range.min()));
                ap.setMaximum(new BigDecimal(range.max()));
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.Length")) {
            if (property instanceof StringProperty) {
                Length length = (Length) annos.get("org.hibernate.validator.constraints.Length");
                StringProperty sp = (StringProperty) property;
                sp.minLength(new Integer(length.min()));
                sp.maxLength(new Integer(length.max()));
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.Email")) {
            if (property instanceof StringProperty) {
                EmailProperty sp = new EmailProperty((StringProperty) property);
                property = sp;
            }
        }
        return property;
    }
    return super.resolveProperty(type, context, annotations, chain);
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) StringProperty(io.swagger.models.properties.StringProperty) Range(org.hibernate.validator.constraints.Range) Annotation(java.lang.annotation.Annotation) BigDecimal(java.math.BigDecimal) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) EmailProperty(io.swagger.models.properties.EmailProperty) Length(org.hibernate.validator.constraints.Length) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) EmailProperty(io.swagger.models.properties.EmailProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) Property(io.swagger.models.properties.Property)

Aggregations

AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)3 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 StringProperty (io.swagger.models.properties.StringProperty)3 Annotation (java.lang.annotation.Annotation)3 HashMap (java.util.HashMap)3 BigDecimal (java.math.BigDecimal)2 LinkedHashMap (java.util.LinkedHashMap)2 DecimalMax (javax.validation.constraints.DecimalMax)2 DecimalMin (javax.validation.constraints.DecimalMin)2 Max (javax.validation.constraints.Max)2 Min (javax.validation.constraints.Min)2 Pattern (javax.validation.constraints.Pattern)2 Size (javax.validation.constraints.Size)2 EmailProperty (io.swagger.models.properties.EmailProperty)1 Property (io.swagger.models.properties.Property)1 Length (org.hibernate.validator.constraints.Length)1 Range (org.hibernate.validator.constraints.Range)1