Search in sources :

Example 6 with NumberSchema

use of io.swagger.v3.oas.models.media.NumberSchema in project swagger-core by swagger-api.

the class ModelResolver method applyBeanValidatorAnnotations.

protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annotations, Schema parent) {
    Map<String, Annotation> annos = new HashMap<>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    if (parent != null && annotations != null) {
        boolean requiredItem = Arrays.stream(annotations).anyMatch(annotation -> NOT_NULL_ANNOTATIONS.contains(annotation.annotationType().getSimpleName()));
        if (requiredItem) {
            addRequiredItem(parent, property.getName());
        }
    }
    if (annos.containsKey("javax.validation.constraints.Min")) {
        if ("integer".equals(property.getType()) || "number".equals(property.getType())) {
            Min min = (Min) annos.get("javax.validation.constraints.Min");
            property.setMinimum(new BigDecimal(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Max")) {
        if ("integer".equals(property.getType()) || "number".equals(property.getType())) {
            Max max = (Max) annos.get("javax.validation.constraints.Max");
            property.setMaximum(new BigDecimal(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Size")) {
        Size size = (Size) annos.get("javax.validation.constraints.Size");
        if ("integer".equals(property.getType()) || "number".equals(property.getType())) {
            property.setMinimum(new BigDecimal(size.min()));
            property.setMaximum(new BigDecimal(size.max()));
        } else if (property instanceof StringSchema) {
            StringSchema sp = (StringSchema) property;
            sp.minLength(Integer.valueOf(size.min()));
            sp.maxLength(Integer.valueOf(size.max()));
        } else if (property instanceof ArraySchema) {
            ArraySchema sp = (ArraySchema) 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 NumberSchema) {
            NumberSchema ap = (NumberSchema) 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 NumberSchema) {
            NumberSchema ap = (NumberSchema) 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 StringSchema) {
            property.setPattern(pattern.regexp());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) BigDecimal(java.math.BigDecimal) Min(javax.validation.constraints.Min) DecimalMin(javax.validation.constraints.DecimalMin) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) DecimalMax(javax.validation.constraints.DecimalMax)

Example 7 with NumberSchema

use of io.swagger.v3.oas.models.media.NumberSchema in project swagger-core by swagger-api.

the class ParameterSerializationTest method testDoubleValue.

@Test(description = "should serialize double value")
public void testDoubleValue() {
    final QueryParameter param = new QueryParameter();
    param.setSchema(new NumberSchema()._default(new BigDecimal("12.34")).format("double"));
    final String json = "{\"in\":\"query\",\"schema\":{\"type\":\"number\",\"format\":\"double\",\"default\":12.34}}";
    SerializationMatchers.assertEqualsToJson(param, json);
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 8 with NumberSchema

use of io.swagger.v3.oas.models.media.NumberSchema in project swagger-core by swagger-api.

the class PropertySerializationTest method serializeFloatProperty.

@Test(description = "it should serialize a FloatProperty")
public void serializeFloatProperty() throws IOException {
    final NumberSchema p = new NumberSchema()._default(new BigDecimal("1.2"));
    p.format("float");
    final String json = "{\"type\":\"number\",\"format\":\"float\",\"default\":1.2}";
    assertEquals(m.writeValueAsString(p), json);
}
Also used : NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)8 BigDecimal (java.math.BigDecimal)7 Test (org.testng.annotations.Test)7 StringSchema (io.swagger.v3.oas.models.media.StringSchema)4 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)3 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)3 Schema (io.swagger.v3.oas.models.media.Schema)3 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)3 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 DecimalMax (javax.validation.constraints.DecimalMax)1 DecimalMin (javax.validation.constraints.DecimalMin)1 Max (javax.validation.constraints.Max)1 Min (javax.validation.constraints.Min)1 Pattern (javax.validation.constraints.Pattern)1 Size (javax.validation.constraints.Size)1