use of io.swagger.models.properties.Property in project swagger-core by swagger-api.
the class JsonDeserializationTest method shouldDeserializeArrayPropertyUniqueItems.
@Test
public void shouldDeserializeArrayPropertyUniqueItems() throws Exception {
String path = "json-schema-validation/array.json";
ArrayProperty property = (ArrayProperty) TestUtils.deserializeJsonFileFromClasspath(path, Property.class);
assertNotNull(property.getUniqueItems());
assertTrue(property.getUniqueItems());
}
use of io.swagger.models.properties.Property in project swagger-core by swagger-api.
the class JsonDeserializationTest method testDeserializeConstrainedArrayProperties.
@Test(description = "should deserialize an array property with constraints")
public void testDeserializeConstrainedArrayProperties() throws Exception {
Swagger swagger = TestUtils.deserializeJsonFileFromClasspath("specFiles/propertiesWithConstraints.json", Swagger.class);
Map<String, Property> properties = swagger.getDefinitions().get("Health").getProperties();
ArrayProperty withMin = (ArrayProperty) properties.get("array_with_min");
assertEquals(withMin.getMinItems(), Integer.valueOf(5));
assertNull(withMin.getMaxItems());
assertNull(withMin.getUniqueItems());
ArrayProperty withMax = (ArrayProperty) properties.get("array_with_max");
assertNull(withMax.getMinItems());
assertEquals(withMax.getMaxItems(), Integer.valueOf(10));
assertNull(withMax.getUniqueItems());
ArrayProperty withUnique = (ArrayProperty) properties.get("array_with_unique");
assertNull(withUnique.getMinItems());
assertNull(withUnique.getMaxItems());
assertEquals(withUnique.getUniqueItems(), Boolean.TRUE);
ArrayProperty withAll = (ArrayProperty) properties.get("array_with_all");
assertEquals(withAll.getMinItems(), Integer.valueOf(1));
assertEquals(withAll.getMaxItems(), Integer.valueOf(10));
assertEquals(withAll.getUniqueItems(), Boolean.TRUE);
}
use of io.swagger.models.properties.Property 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);
}
use of io.swagger.models.properties.Property in project swagger-core by swagger-api.
the class HibernateBeanValidationsTest method readHibernateValidations.
@Test(description = "it should read hibernate validations")
public void readHibernateValidations() {
final Map<String, Model> schemas = ModelConverters.getInstance().readAll(HibernateBeanValidationsModel.class);
final Map<String, Property> properties = schemas.get("HibernateBeanValidationsModel").getProperties();
final IntegerProperty age = (IntegerProperty) properties.get("age");
assertEquals(age.getMinimum().doubleValue(), 13.0, 0.01);
assertEquals(age.getMaximum().doubleValue(), 99.0, 0.01);
final StringProperty password = (StringProperty) properties.get("password");
assertEquals((int) password.getMinLength(), 6);
assertEquals((int) password.getMaxLength(), 20);
assertTrue(((DoubleProperty) properties.get("minBalance")).getExclusiveMinimum());
assertTrue(((DoubleProperty) properties.get("maxBalance")).getExclusiveMaximum());
}
use of io.swagger.models.properties.Property in project swagger-core by swagger-api.
the class HibernateBeanValidationsTest method shouldUnderstandNotEmpty.
@Test
public void shouldUnderstandNotEmpty() {
final Map<String, Model> schemas = ModelConverters.getInstance().readAll(HibernateBeanValidationsModel.class);
final Map<String, Property> properties = schemas.get("HibernateBeanValidationsModel").getProperties();
final StringProperty notEmptyString = (StringProperty) properties.get("notEmptyString");
assertEquals((int) notEmptyString.getMinLength(), 1);
final ArrayProperty notEmptyArray = (ArrayProperty) properties.get("notEmptyArray");
assertEquals((int) notEmptyArray.getMinItems(), 1);
}
Aggregations