Search in sources :

Example 91 with Property

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

Example 92 with Property

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

Example 93 with Property

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);
}
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 94 with Property

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());
}
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 95 with Property

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

Aggregations

Property (io.swagger.models.properties.Property)145 Test (org.testng.annotations.Test)96 ArrayProperty (io.swagger.models.properties.ArrayProperty)86 StringProperty (io.swagger.models.properties.StringProperty)75 RefProperty (io.swagger.models.properties.RefProperty)65 MapProperty (io.swagger.models.properties.MapProperty)59 Model (io.swagger.models.Model)52 IntegerProperty (io.swagger.models.properties.IntegerProperty)48 LongProperty (io.swagger.models.properties.LongProperty)35 DoubleProperty (io.swagger.models.properties.DoubleProperty)32 FloatProperty (io.swagger.models.properties.FloatProperty)27 BooleanProperty (io.swagger.models.properties.BooleanProperty)25 ObjectProperty (io.swagger.models.properties.ObjectProperty)25 Operation (io.swagger.models.Operation)24 DateTimeProperty (io.swagger.models.properties.DateTimeProperty)23 DateProperty (io.swagger.models.properties.DateProperty)22 ApiModelProperty (io.swagger.annotations.ApiModelProperty)20 FileProperty (io.swagger.models.properties.FileProperty)18 ModelImpl (io.swagger.models.ModelImpl)17 Response (io.swagger.models.Response)16