use of io.swagger.models.properties.StringProperty 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.StringProperty 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.StringProperty 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);
}
use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.
the class PropertySerializationTest method serializeEnumStringProperty.
@Test(description = "it should serialize a StringProperty with enums")
public void serializeEnumStringProperty() throws IOException {
final StringProperty p = new StringProperty()._enum("a")._enum("b");
final String json = "{\"type\":\"string\",\"enum\":[\"a\",\"b\"]}";
assertEquals(m.writeValueAsString(p), json);
}
use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.
the class PropertySerializationTest method serializeObjectPropertyWithRequiredProperties.
@Test(description = "it should serialize an object property with required set")
public void serializeObjectPropertyWithRequiredProperties() throws IOException {
final ObjectProperty p = new ObjectProperty().property("stringProperty", new StringProperty().required(true));
final String json = "{\"type\":\"object\",\"properties\":{\"stringProperty\":{\"type\":\"string\"}},\"required\":[\"stringProperty\"]}";
assertEquals(m.writeValueAsString(p), json);
}
Aggregations