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());
}
}
}
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);
}
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);
}
Aggregations