Search in sources :

Example 11 with LongProperty

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

the class ModelSerializerTest method convertModel.

@Test(description = "it should convert a model")
public void convertModel() throws JsonProcessingException {
    final ModelImpl pet = new ModelImpl();
    final HashMap<String, Property> props = new HashMap<String, Property>();
    props.put("intValue", new IntegerProperty());
    props.put("longValue", new LongProperty());
    props.put("dateValue", new DateProperty());
    props.put("dateTimeValue", new DateTimeProperty());
    pet.setProperties(props);
    pet.setRequired(Arrays.asList("intValue", "name"));
    final String json = "{\n" + "   \"required\":[\n" + "      \"intValue\"\n" + "   ],\n" + "   \"properties\":{\n" + "      \"dateValue\":{\n" + "         \"type\":\"string\",\n" + "         \"format\":\"date\"\n" + "      },\n" + "      \"longValue\":{\n" + "         \"type\":\"integer\",\n" + "         \"format\":\"int64\"\n" + "      },\n" + "      \"dateTimeValue\":{\n" + "         \"type\":\"string\",\n" + "         \"format\":\"date-time\"\n" + "      },\n" + "      \"intValue\":{\n" + "         \"type\":\"integer\",\n" + "         \"format\":\"int32\"\n" + "      }\n" + "   }\n" + "}";
    SerializationMatchers.assertEqualsToJson(pet, json);
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) HashMap(java.util.HashMap) DateProperty(io.swagger.models.properties.DateProperty) LongProperty(io.swagger.models.properties.LongProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) ModelImpl(io.swagger.models.ModelImpl) StringProperty(io.swagger.models.properties.StringProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) DateProperty(io.swagger.models.properties.DateProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Test(org.testng.annotations.Test)

Example 12 with LongProperty

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

the class ParameterProcessor method applyAnnotations.

public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, Type type, List<Annotation> annotations) {
    final AnnotationsHelper helper = new AnnotationsHelper(annotations, type);
    if (helper.isContext()) {
        return null;
    }
    final ParamWrapper<?> param = helper.getApiParam();
    if (param.isHidden()) {
        return null;
    }
    final String defaultValue = helper.getDefaultValue();
    if (parameter instanceof AbstractSerializableParameter) {
        final AbstractSerializableParameter<?> p = (AbstractSerializableParameter<?>) parameter;
        if (param.isRequired()) {
            p.setRequired(true);
        }
        if (param.getReadOnly()) {
            p.readOnly(param.getReadOnly());
        }
        if (param.getAllowEmptyValue()) {
            p.allowEmptyValue(param.getAllowEmptyValue());
        }
        if (StringUtils.isNotEmpty(param.getName())) {
            p.setName(param.getName());
        }
        if (StringUtils.isNotEmpty(param.getDescription())) {
            p.setDescription(param.getDescription());
        }
        if (StringUtils.isNotEmpty(param.getExample())) {
            p.setExample(param.getExample());
        }
        if (StringUtils.isNotEmpty(param.getAccess())) {
            p.setAccess(param.getAccess());
        }
        if (StringUtils.isNoneEmpty(param.getCollectionFormat())) {
            p.setCollectionFormat(param.getCollectionFormat());
        }
        if (StringUtils.isNotEmpty(param.getDataType())) {
            if ("java.io.File".equalsIgnoreCase(param.getDataType())) {
                p.setProperty(new FileProperty());
            } else if ("long".equalsIgnoreCase(param.getDataType())) {
                p.setProperty(new LongProperty());
            } else {
                p.setType(param.getDataType());
            }
        }
        if (helper.getMin() != null) {
            p.setMinimum(helper.getMin());
            if (helper.isMinExclusive()) {
                p.setExclusiveMinimum(true);
            }
        }
        if (helper.getMax() != null) {
            p.setMaximum(helper.getMax());
            if (helper.isMaxExclusive()) {
                p.setExclusiveMaximum(true);
            }
        }
        if (helper.getMinItems() != null) {
            p.setMinItems(helper.getMinItems());
        }
        if (helper.getMaxItems() != null) {
            p.setMaxItems(helper.getMaxItems());
        }
        if (helper.getMinLength() != null) {
            p.setMinLength(helper.getMinLength());
        }
        if (helper.getMaxLength() != null) {
            p.setMaxLength(helper.getMaxLength());
        }
        if (helper.getPattern() != null) {
            p.setPattern(helper.getPattern());
        }
        if (helper.isRequired() != null) {
            p.setRequired(true);
        }
        if (helper.getType() != null) {
            p.setType(helper.getType());
        }
        if (helper.getFormat() != null) {
            p.setFormat(helper.getFormat());
        }
        AllowableValues allowableValues = AllowableValuesUtils.create(param.getAllowableValues());
        if (p.getItems() != null || param.isAllowMultiple()) {
            if (p.getItems() == null) {
                // Convert to array
                final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
                args.put(PropertyBuilder.PropertyId.DEFAULT, p.getDefaultValue());
                p.setDefaultValue(null);
                args.put(PropertyBuilder.PropertyId.ENUM, p.getEnum());
                p.setEnum(null);
                args.put(PropertyBuilder.PropertyId.MINIMUM, p.getMinimum());
                p.setMinimum(null);
                args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM, p.isExclusiveMinimum());
                p.setExclusiveMinimum(null);
                args.put(PropertyBuilder.PropertyId.MAXIMUM, p.getMaximum());
                p.setMaximum(null);
                args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM, p.isExclusiveMaximum());
                args.put(PropertyBuilder.PropertyId.MIN_LENGTH, p.getMinLength());
                p.setMinLength(null);
                args.put(PropertyBuilder.PropertyId.MAX_LENGTH, p.getMaxLength());
                p.setMaxLength(null);
                args.put(PropertyBuilder.PropertyId.PATTERN, p.getPattern());
                p.setPattern(null);
                args.put(PropertyBuilder.PropertyId.EXAMPLE, p.getExample());
                p.setExclusiveMaximum(null);
                Property items = PropertyBuilder.build(p.getType(), p.getFormat(), args);
                p.type(ArrayProperty.TYPE).format(null).items(items);
            }
            final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
            if (StringUtils.isNotEmpty(defaultValue)) {
                args.put(PropertyBuilder.PropertyId.DEFAULT, defaultValue);
            }
            if (helper.getMin() != null) {
                args.put(PropertyBuilder.PropertyId.MINIMUM, helper.getMin());
                if (helper.isMinExclusive()) {
                    args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM, true);
                }
            }
            if (helper.getMax() != null) {
                args.put(PropertyBuilder.PropertyId.MAXIMUM, helper.getMax());
                if (helper.isMaxExclusive()) {
                    args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM, true);
                }
            }
            if (helper.getMinLength() != null) {
                args.put(PropertyBuilder.PropertyId.MIN_LENGTH, helper.getMinLength());
            }
            if (helper.getMaxLength() != null) {
                args.put(PropertyBuilder.PropertyId.MAX_LENGTH, helper.getMaxLength());
            }
            if (helper.getPattern() != null) {
                args.put(PropertyBuilder.PropertyId.PATTERN, helper.getPattern());
            }
            //Overwrite Bean validation values with allowable values if present
            if (allowableValues != null) {
                args.putAll(allowableValues.asPropertyArguments());
            }
            PropertyBuilder.merge(p.getItems(), args);
        } else {
            if (StringUtils.isNotEmpty(defaultValue)) {
                p.setDefaultValue(defaultValue);
            }
            //Overwrite Bean validation values with allowable values if present
            if (allowableValues != null) {
                processAllowedValues(allowableValues, p);
            }
        //                else {
        //                    processJsr303Annotations(helper, p);
        //                }
        }
    } else {
        // must be a body param
        BodyParameter bp = new BodyParameter();
        if (helper.getApiParam() != null) {
            ParamWrapper<?> pw = helper.getApiParam();
            if (pw instanceof ApiParamWrapper) {
                ApiParamWrapper apiParam = (ApiParamWrapper) pw;
                Example example = apiParam.getExamples();
                if (example != null && example.value() != null) {
                    for (ExampleProperty ex : example.value()) {
                        String mediaType = ex.mediaType();
                        String value = ex.value();
                        if (!mediaType.isEmpty() && !value.isEmpty()) {
                            bp.example(mediaType.trim(), value.trim());
                        }
                    }
                }
            } else if (pw instanceof ApiImplicitParamWrapper) {
                ApiImplicitParamWrapper apiParam = (ApiImplicitParamWrapper) pw;
                Example example = apiParam.getExamples();
                if (example != null && example.value() != null) {
                    for (ExampleProperty ex : example.value()) {
                        String mediaType = ex.mediaType();
                        String value = ex.value();
                        if (!mediaType.isEmpty() && !value.isEmpty()) {
                            bp.example(mediaType.trim(), value.trim());
                        }
                    }
                }
            }
        }
        bp.setRequired(param.isRequired());
        bp.setName(StringUtils.isNotEmpty(param.getName()) ? param.getName() : "body");
        if (StringUtils.isNotEmpty(param.getDescription())) {
            bp.setDescription(param.getDescription());
        }
        if (StringUtils.isNotEmpty(param.getAccess())) {
            bp.setAccess(param.getAccess());
        }
        final Property property = ModelConverters.getInstance().readAsProperty(type);
        if (property != null) {
            final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
            if (StringUtils.isNotEmpty(defaultValue)) {
                args.put(PropertyBuilder.PropertyId.DEFAULT, defaultValue);
            }
            bp.setSchema(PropertyBuilder.toModel(PropertyBuilder.merge(property, args)));
            for (Map.Entry<String, Model> entry : ModelConverters.getInstance().readAll(type).entrySet()) {
                swagger.addDefinition(entry.getKey(), entry.getValue());
            }
        }
        parameter = bp;
    }
    return parameter;
}
Also used : ExampleProperty(io.swagger.annotations.ExampleProperty) BodyParameter(io.swagger.models.parameters.BodyParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) FileProperty(io.swagger.models.properties.FileProperty) LongProperty(io.swagger.models.properties.LongProperty) Example(io.swagger.annotations.Example) Model(io.swagger.models.Model) EnumMap(java.util.EnumMap) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) LongProperty(io.swagger.models.properties.LongProperty) Property(io.swagger.models.properties.Property) ExampleProperty(io.swagger.annotations.ExampleProperty) FileProperty(io.swagger.models.properties.FileProperty) Map(java.util.Map) EnumMap(java.util.EnumMap) PropertyBuilder(io.swagger.models.properties.PropertyBuilder)

Example 13 with LongProperty

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

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

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

the class PropertySerializationTest method deserializeLongProperty.

@Test(description = "it should deserialize a LongProperty")
public void deserializeLongProperty() throws IOException {
    final String json = "{\"type\":\"integer\",\"format\":\"int64\"}";
    final Property p = m.readValue(json, Property.class);
    assertEquals(p.getType(), "integer");
    assertEquals(p.getFormat(), "int64");
    assertEquals(p.getClass(), LongProperty.class);
    assertEquals(m.writeValueAsString(p), json);
}
Also used : DoubleProperty(io.swagger.models.properties.DoubleProperty) MapProperty(io.swagger.models.properties.MapProperty) FloatProperty(io.swagger.models.properties.FloatProperty) DateProperty(io.swagger.models.properties.DateProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) FileProperty(io.swagger.models.properties.FileProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Aggregations

LongProperty (io.swagger.models.properties.LongProperty)17 StringProperty (io.swagger.models.properties.StringProperty)13 Test (org.testng.annotations.Test)13 Property (io.swagger.models.properties.Property)8 ArrayProperty (io.swagger.models.properties.ArrayProperty)7 IntegerProperty (io.swagger.models.properties.IntegerProperty)7 RefProperty (io.swagger.models.properties.RefProperty)7 Model (io.swagger.models.Model)6 DoubleProperty (io.swagger.models.properties.DoubleProperty)5 Operation (io.swagger.models.Operation)4 Path (io.swagger.models.Path)4 Response (io.swagger.models.Response)4 QueryParameter (io.swagger.models.parameters.QueryParameter)4 BooleanProperty (io.swagger.models.properties.BooleanProperty)4 FloatProperty (io.swagger.models.properties.FloatProperty)4 Contact (io.swagger.models.Contact)3 Info (io.swagger.models.Info)3 ModelImpl (io.swagger.models.ModelImpl)3 Person (io.swagger.models.Person)3 RefModel (io.swagger.models.RefModel)3