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