Search in sources :

Example 51 with ArrayProperty

use of io.swagger.models.properties.ArrayProperty 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 52 with ArrayProperty

use of io.swagger.models.properties.ArrayProperty 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 53 with ArrayProperty

use of io.swagger.models.properties.ArrayProperty in project swagger-core by swagger-api.

the class XMLInfoTest method testSimple.

@Test
public void testSimple() throws Exception {
    final ModelConverter mr = modelResolver();
    final Model model = mr.resolve(XmlDecoratedBean.class, new ModelConverterContextImpl(mr), null);
    assertTrue(model instanceof ModelImpl);
    final ModelImpl impl = (ModelImpl) model;
    final Xml xml = impl.getXml();
    assertNotNull(xml);
    assertEquals(xml.getName(), "xmlDecoratedBean");
    // Cast it to an array property
    final ArrayProperty property = (ArrayProperty) impl.getProperties().get("elements");
    assertNotNull(property);
    final Xml propertyXml = property.getXml();
    assertNotNull(propertyXml);
    assertNull(propertyXml.getName());
    assertTrue(propertyXml.getWrapped());
    // Get the xml for items for the array property
    final Xml itemsXml = property.getItems().getXml();
    assertNotNull(itemsXml);
    // Check the name of item name
    assertEquals(itemsXml.getName(), "element");
    assertNotNull(impl.getProperties().get("elementC"));
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) Xml(io.swagger.models.Xml) Model(io.swagger.models.Model) ApiModel(io.swagger.annotations.ApiModel) ModelImpl(io.swagger.models.ModelImpl) ModelConverter(io.swagger.converter.ModelConverter) ModelConverterContextImpl(io.swagger.converter.ModelConverterContextImpl) Test(org.testng.annotations.Test)

Example 54 with ArrayProperty

use of io.swagger.models.properties.ArrayProperty in project swagger-core by swagger-api.

the class ArrayPropertyDeserializerTest method testArrayDeserialization.

@Test(description = "it should includes the example in the arrayproperty")
public void testArrayDeserialization() throws Exception {
    Operation operation = Yaml.mapper().readValue(yaml, Operation.class);
    Response response = operation.getResponses().get("200");
    assertNotNull(response);
    Property responseSchema = response.getSchema();
    assertNotNull(responseSchema);
    assertTrue(responseSchema instanceof ArrayProperty);
    ArrayProperty mp = (ArrayProperty) responseSchema;
    assertNotNull(mp.getExample());
    assertEquals(mp.getMinItems(), new Integer(3));
    assertEquals(mp.getMaxItems(), new Integer(100));
}
Also used : Response(io.swagger.models.Response) ArrayProperty(io.swagger.models.properties.ArrayProperty) Operation(io.swagger.models.Operation) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 55 with ArrayProperty

use of io.swagger.models.properties.ArrayProperty in project swagger-core by swagger-api.

the class SimpleReaderTest method checkResponseModelsProcessing.

@Test(description = "check response models processing")
public void checkResponseModelsProcessing() {
    Swagger swagger = getSwagger(ResourceWithTypedResponses.class);
    assertEquals(swagger.getDefinitions().keySet(), Arrays.asList("Tag"));
    for (Map.Entry<String, Path> entry : swagger.getPaths().entrySet()) {
        String name = entry.getKey().substring(entry.getKey().lastIndexOf("/") + 1);
        if ("testPrimitiveResponses".equals(name)) {
            Map<String, String[]> expected = ImmutableMap.of("400", new String[] { "string", "uri" }, "401", new String[] { "string", "url" }, "402", new String[] { "string", "uuid" }, "403", new String[] { "integer", "int64" }, "404", new String[] { "string", null });
            assertEquals(entry.getValue().getGet().getResponses().size(), expected.size());
            for (Map.Entry<String, Response> responseEntry : entry.getValue().getGet().getResponses().entrySet()) {
                String[] expectedProp = expected.get(responseEntry.getKey());
                Property property = responseEntry.getValue().getSchema();
                assertEquals(property.getType(), expectedProp[0]);
                assertEquals(property.getFormat(), expectedProp[1]);
            }
        } else {
            Operation op = entry.getValue().getGet();
            Property response = op.getResponses().get("200").getSchema();
            Model model = ((BodyParameter) op.getParameters().get(0)).getSchema();
            assertEquals(op.getParameters().size(), 1);
            if ("testObjectResponse".equals(name)) {
                assertEquals(((RefProperty) response).getSimpleRef(), "Tag");
                assertEquals(((RefModel) model).getSimpleRef(), "Tag");
            } else if ("testObjectsResponse".equals(name)) {
                assertEquals(((RefProperty) ((ArrayProperty) response).getItems()).getSimpleRef(), "Tag");
                assertEquals(((RefProperty) ((ArrayModel) model).getItems()).getSimpleRef(), "Tag");
            } else if ("testStringResponse".equals(name)) {
                assertEquals(response.getClass(), StringProperty.class);
                assertEquals(((ModelImpl) model).getType(), "string");
            } else if ("testStringsResponse".equals(name)) {
                assertEquals(((ArrayProperty) response).getItems().getClass(), StringProperty.class);
                assertEquals(((ArrayModel) model).getItems().getClass(), StringProperty.class);
            } else if ("testMapResponse".equals(name)) {
                assertEquals(((RefProperty) ((MapProperty) response).getAdditionalProperties()).getSimpleRef(), "Tag");
                assertNull(model.getProperties());
                assertEquals(((RefProperty) ((ModelImpl) model).getAdditionalProperties()).getSimpleRef(), "Tag");
            } else {
                fail(String.format("Unexpected property: %s", name));
            }
        }
    }
}
Also used : Path(io.swagger.models.Path) MapProperty(io.swagger.models.properties.MapProperty) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) NicknamedOperation(io.swagger.resources.NicknamedOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) RefProperty(io.swagger.models.properties.RefProperty) ResourceWithResponse(io.swagger.resources.ResourceWithResponse) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) ResourceWithEmptyModel(io.swagger.resources.ResourceWithEmptyModel) RefModel(io.swagger.models.RefModel) ArrayModel(io.swagger.models.ArrayModel) ModelImpl(io.swagger.models.ModelImpl) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

Aggregations

ArrayProperty (io.swagger.models.properties.ArrayProperty)82 Property (io.swagger.models.properties.Property)62 StringProperty (io.swagger.models.properties.StringProperty)42 RefProperty (io.swagger.models.properties.RefProperty)39 Test (org.testng.annotations.Test)36 MapProperty (io.swagger.models.properties.MapProperty)25 Model (io.swagger.models.Model)23 IntegerProperty (io.swagger.models.properties.IntegerProperty)23 LongProperty (io.swagger.models.properties.LongProperty)14 ModelImpl (io.swagger.models.ModelImpl)13 BooleanProperty (io.swagger.models.properties.BooleanProperty)12 DoubleProperty (io.swagger.models.properties.DoubleProperty)12 RefModel (io.swagger.models.RefModel)10 FloatProperty (io.swagger.models.properties.FloatProperty)10 Operation (io.swagger.models.Operation)9 ByteArrayProperty (io.swagger.models.properties.ByteArrayProperty)9 DecimalProperty (io.swagger.models.properties.DecimalProperty)9 HashMap (java.util.HashMap)9 Response (io.swagger.models.Response)7 Swagger (io.swagger.models.Swagger)7