Search in sources :

Example 41 with StringProperty

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);
}
Also used : Type(java.lang.reflect.Type) LongProperty(io.swagger.models.properties.LongProperty) StringProperty(io.swagger.models.properties.StringProperty) Method(java.lang.reflect.Method) HeaderParameter(io.swagger.models.parameters.HeaderParameter) DoubleProperty(io.swagger.models.properties.DoubleProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 42 with 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));
        }
    }
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) ModelWithTuple2(io.swagger.models.ModelWithTuple2) MapProperty(io.swagger.models.properties.MapProperty) StringProperty(io.swagger.models.properties.StringProperty) AbstractModelWithApiModel(io.swagger.models.composition.AbstractModelWithApiModel) Model(io.swagger.models.Model) ModelWithApiModel(io.swagger.models.ModelWithApiModel) EmptyModel(io.swagger.models.EmptyModel) JodaDateTimeModel(io.swagger.models.JodaDateTimeModel) JacksonReadonlyModel(io.swagger.models.JacksonReadonlyModel) ModelImpl(io.swagger.models.ModelImpl) Map(java.util.Map) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) MapProperty(io.swagger.models.properties.MapProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) BaseIntegerProperty(io.swagger.models.properties.BaseIntegerProperty) ModelWithUrlProperty(io.swagger.models.composition.ModelWithUrlProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

Example 43 with StringProperty

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

Example 44 with StringProperty

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);
}
Also used : Path(io.swagger.models.Path) QueryParameter(io.swagger.models.parameters.QueryParameter) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) Error(io.swagger.models.Error) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) PathParameter(io.swagger.models.parameters.PathParameter) Contact(io.swagger.models.Contact) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) Swagger(io.swagger.models.Swagger) LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) Person(io.swagger.models.Person) SecurityRequirement(io.swagger.models.SecurityRequirement) Test(org.testng.annotations.Test)

Example 45 with StringProperty

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");
}
Also used : 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