use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.
the class ParameterProcessorTest method beanValidationArrayParametrizedMethodTest.
@Test
public void beanValidationArrayParametrizedMethodTest() throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod("beanValidationArrayParametrizedMethod", List.class, List.class, List.class, String.class);
final Type[] genericParameterTypes = method.getGenericParameterTypes();
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
//First param - items specified
HeaderParameter headerParam1 = new HeaderParameter().type(ArrayProperty.TYPE).items(new LongProperty());
HeaderParameter param1 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam1, genericParameterTypes[0], Arrays.asList(paramAnnotations[0]));
assertNotNull(param1);
assertEquals((int) param1.getMinItems(), 5);
assertEquals((int) param1.getMaxItems(), 10);
Property items1 = param1.getItems();
assertTrue(items1 instanceof LongProperty);
LongProperty longItems = (LongProperty) items1;
assertEquals(longItems.getMinimum(), new BigDecimal(5));
assertNull(longItems.getExclusiveMinimum());
assertEquals(longItems.getMaximum(), new BigDecimal(10));
assertNull(longItems.getExclusiveMaximum());
//Second param - items specified
HeaderParameter headerParam2 = new HeaderParameter().type(ArrayProperty.TYPE).items(new DoubleProperty());
HeaderParameter param2 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam2, genericParameterTypes[1], Arrays.asList(paramAnnotations[1]));
assertNotNull(param2);
assertEquals((int) param2.getMinItems(), 5);
assertEquals((int) param2.getMaxItems(), 10);
Property items2 = param2.getItems();
assertTrue(items2 instanceof DoubleProperty);
DoubleProperty doubleItems = (DoubleProperty) items2;
assertEquals(doubleItems.getMinimum(), new BigDecimal(5.5));
assertTrue(doubleItems.getExclusiveMinimum());
assertEquals(doubleItems.getMaximum(), new BigDecimal(10.5));
assertTrue(doubleItems.getExclusiveMaximum());
//Third param - items specified
HeaderParameter headerParam3 = new HeaderParameter().type(ArrayProperty.TYPE).items(new StringProperty());
HeaderParameter param3 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam3, genericParameterTypes[2], Arrays.asList(paramAnnotations[2]));
assertNotNull(param3);
assertEquals((int) param3.getMinItems(), 5);
assertEquals((int) param3.getMaxItems(), 10);
Property items3 = param3.getItems();
assertTrue(items3 instanceof StringProperty);
StringProperty stringItems = (StringProperty) items3;
assertEquals(stringItems.getPattern(), TEST_PATTERN_REGXP);
//Fourth param - items specified
HeaderParameter headerParam4 = new HeaderParameter().type(StringProperty.TYPE);
HeaderParameter param4 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam4, genericParameterTypes[3], Arrays.asList(paramAnnotations[3]));
assertNotNull(param4);
assertEquals(param4.getType(), ArrayProperty.TYPE);
assertEquals((int) param4.getMinItems(), 5);
assertEquals((int) param4.getMaxItems(), 10);
Property items4 = param4.getItems();
assertTrue(items4 instanceof StringProperty);
}
use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.
the class ModelConverterTest method processModelWithPairProperties.
@Test(description = "it should process a model with org.apache.commons.lang3.tuple.Pair properties")
public void processModelWithPairProperties() {
final ModelWithTuple2.TupleAsMapModelConverter asMapConverter = new ModelWithTuple2.TupleAsMapModelConverter(Json.mapper());
ModelConverters.getInstance().addConverter(asMapConverter);
final Map<String, Model> asMap = readAll(ModelWithTuple2.class);
ModelConverters.getInstance().removeConverter(asMapConverter);
assertEquals(asMap.size(), 4);
for (String item : Arrays.asList("MapOfString", "MapOfComplexLeft")) {
ModelImpl model = (ModelImpl) asMap.get(item);
assertEquals(model.getType(), "object");
assertNull(model.getProperties());
assertNotNull(model.getAdditionalProperties());
}
final ModelWithTuple2.TupleAsMapPropertyConverter asPropertyConverter = new ModelWithTuple2.TupleAsMapPropertyConverter(Json.mapper());
ModelConverters.getInstance().addConverter(asPropertyConverter);
final Map<String, Model> asProperty = readAll(ModelWithTuple2.class);
ModelConverters.getInstance().removeConverter(asPropertyConverter);
assertEquals(asProperty.size(), 2);
for (Map.Entry<String, Property> entry : asProperty.get("ModelWithTuple2").getProperties().entrySet()) {
String name = entry.getKey();
Property property = entry.getValue();
if ("timesheetStates".equals(name)) {
assertEquals(property.getClass(), MapProperty.class);
} else if ("manyPairs".equals(name)) {
assertEquals(property.getClass(), ArrayProperty.class);
Property items = ((ArrayProperty) property).getItems();
assertNotNull(items);
assertEquals(items.getClass(), MapProperty.class);
Property stringProperty = ((MapProperty) items).getAdditionalProperties();
assertNotNull(stringProperty);
assertEquals(stringProperty.getClass(), StringProperty.class);
} else if ("complexLeft".equals(name)) {
assertEquals(property.getClass(), ArrayProperty.class);
Property items = ((ArrayProperty) property).getItems();
assertNotNull(items);
assertEquals(items.getClass(), MapProperty.class);
Property additionalProperty = ((MapProperty) items).getAdditionalProperties();
assertNotNull(additionalProperty);
assertEquals(additionalProperty.getClass(), RefProperty.class);
assertEquals(((RefProperty) additionalProperty).getSimpleRef(), "ComplexLeft");
} else {
fail(String.format("Unexpected property: %s", name));
}
}
}
use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.
the class BeanValidatorTest method readBeanValidatorTest.
@Test(description = "read bean validations")
public void readBeanValidatorTest() {
final Map<String, Model> schemas = ModelConverters.getInstance().readAll(BeanValidationsModel.class);
final Model model = schemas.get("BeanValidationsModel");
final Map<String, Property> properties = model.getProperties();
final IntegerProperty age = (IntegerProperty) properties.get("age");
Assert.assertEquals(age.getMinimum(), new BigDecimal(13.0));
Assert.assertEquals(age.getMaximum(), new BigDecimal(99.0));
final StringProperty password = (StringProperty) properties.get("password");
Assert.assertEquals((int) password.getMinLength(), 6);
Assert.assertEquals((int) password.getMaxLength(), 20);
final StringProperty email = (StringProperty) properties.get("email");
Assert.assertEquals((String) email.getPattern(), "(.+?)@(.+?)");
final DoubleProperty minBalance = (DoubleProperty) properties.get("minBalance");
Assert.assertTrue(minBalance.getExclusiveMinimum());
final DoubleProperty maxBalance = (DoubleProperty) properties.get("maxBalance");
Assert.assertTrue(maxBalance.getExclusiveMaximum());
final ArrayProperty items = (ArrayProperty) properties.get("items");
Assert.assertEquals((int) items.getMinItems(), 2);
Assert.assertEquals((int) items.getMaxItems(), 10);
}
use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.
the class SecurityDefinitionTest method createModelWithSecurityRequirements.
@Test(description = "it should create a model with security requirements")
public void createModelWithSecurityRequirements() throws IOException {
final Model personModel = ModelConverters.getInstance().read(Person.class).get("Person");
final Model errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
final Info info = new Info().version("1.0.0").title("Swagger Petstore");
final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
info.setContact(contact);
final Swagger swagger = new Swagger().info(info).host("petstore.swagger.io").scheme(Scheme.HTTP).consumes("application/json").produces("application/json").model("Person", personModel).model("Error", errorModel);
swagger.securityDefinition("githubAccessCode", new OAuth2Definition().accessCode("http://foo.com/accessCode", "http://foo.com/tokenUrl").scope("user:email", "Grants read access to a user’s email addresses."));
final Operation get = new Operation().produces("application/json").summary("finds pets in the system").description("a longer description").tag("Pet Operations").operationId("get pet by id");
get.parameter(new QueryParameter().name("tags").description("tags to filter by").required(false).property(new StringProperty()));
get.parameter(new PathParameter().name("petId").description("pet to fetch").property(new LongProperty()));
final Response response = new Response().description("pets returned").schema(new RefProperty().asDefault("Person"));
final Response errorResponse = new Response().description("error response").schema(new RefProperty().asDefault("Error"));
get.response(200, response).defaultResponse(errorResponse).security(new SecurityRequirement("internal_oauth2").scope("user:email")).security(new SecurityRequirement("api_key"));
swagger.path("/pets", new Path().get(get));
final String json = ResourceUtils.loadClassResource(getClass(), "ModelWithSecurityRequirements.json");
SerializationMatchers.assertEqualsToJson(swagger, json);
}
use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.
the class JsonDeserializationTest method testDeserializeConstrainedStringProperty.
@Test(description = "should deserialize a string property with constraints")
public void testDeserializeConstrainedStringProperty() throws Exception {
Swagger swagger = TestUtils.deserializeJsonFileFromClasspath("specFiles/propertiesWithConstraints.json", Swagger.class);
StringProperty property = (StringProperty) swagger.getDefinitions().get("Health").getProperties().get("string_with_constraints");
assertEquals(property.getMinLength(), Integer.valueOf(10));
assertEquals(property.getMaxLength(), Integer.valueOf(100));
assertEquals(property.getPattern(), "apattern");
}
Aggregations