Search in sources :

Example 46 with StringProperty

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

Example 47 with StringProperty

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());
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) HibernateBeanValidationsModel(io.swagger.models.HibernateBeanValidationsModel) Model(io.swagger.models.Model) StringProperty(io.swagger.models.properties.StringProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) DoubleProperty(io.swagger.models.properties.DoubleProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 48 with StringProperty

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);
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) HibernateBeanValidationsModel(io.swagger.models.HibernateBeanValidationsModel) Model(io.swagger.models.Model) StringProperty(io.swagger.models.properties.StringProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) DoubleProperty(io.swagger.models.properties.DoubleProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 49 with StringProperty

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);
}
Also used : StringProperty(io.swagger.models.properties.StringProperty) Test(org.testng.annotations.Test)

Example 50 with StringProperty

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);
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) StringProperty(io.swagger.models.properties.StringProperty) Test(org.testng.annotations.Test)

Aggregations

StringProperty (io.swagger.models.properties.StringProperty)62 Test (org.testng.annotations.Test)47 Property (io.swagger.models.properties.Property)30 ArrayProperty (io.swagger.models.properties.ArrayProperty)22 Model (io.swagger.models.Model)21 RefProperty (io.swagger.models.properties.RefProperty)17 IntegerProperty (io.swagger.models.properties.IntegerProperty)16 LongProperty (io.swagger.models.properties.LongProperty)16 DoubleProperty (io.swagger.models.properties.DoubleProperty)14 ModelImpl (io.swagger.models.ModelImpl)10 FloatProperty (io.swagger.models.properties.FloatProperty)9 BodyParameter (io.swagger.models.parameters.BodyParameter)8 BooleanProperty (io.swagger.models.properties.BooleanProperty)8 ApiModelProperty (io.swagger.annotations.ApiModelProperty)7 MapProperty (io.swagger.models.properties.MapProperty)7 Operation (io.swagger.models.Operation)6 Path (io.swagger.models.Path)6 PathParameter (io.swagger.models.parameters.PathParameter)6 ObjectProperty (io.swagger.models.properties.ObjectProperty)6 QueryParameter (io.swagger.models.parameters.QueryParameter)5