Search in sources :

Example 36 with Property

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

the class ResponseTest method testHeader.

@Test
public void testHeader() {
    // given
    Response response = new Response();
    String name = "name";
    Property property = new BooleanProperty();
    // when
    response.header(name, property);
    // then
    assertEquals(response.getHeaders().get("name"), property, "The retrieved property must be the same as the set one");
}
Also used : BooleanProperty(io.swagger.models.properties.BooleanProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Example 37 with Property

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

the class AbstractSerializableParameterTest method testGettersAndSetters.

/*
     * Tests getters and setters methods on {@link
     * AbstractSerializableParameter} It was not possible to cove it with {@link
     * io.swagger.PojosTest} so a manual implementation is provided for now TODO
     * improve PojosTest to test getters and setters for abstracts classes
     */
@Test
public void testGettersAndSetters() {
    // given
    String type = "type";
    // when
    instance.setType(type);
    // then
    assertEquals(instance.getType(), type, "The get type must be the same as the set one");
    // given
    String format = "format";
    // when
    instance.setFormat(format);
    // then
    assertEquals(instance.getFormat(), format, "The get format must be the same as the set one");
    // given
    String collectionFormat = "collectionFormat";
    // when
    instance.setCollectionFormat(collectionFormat);
    // then
    assertEquals(instance.getCollectionFormat(), collectionFormat, "The get collectionFormat must be the same as the set one");
    // given
    Property items = new BooleanProperty();
    // when
    instance.setItems(items);
    // then
    assertEquals(instance.getItems(), items, "The get items must be the same as the set one");
    // given
    List<String> _enum = Arrays.asList("_enum");
    // when
    instance._enum(_enum);
    instance.setEnum(_enum);
    // then
    assertEquals(instance.getEnum(), _enum, "The get _enum must be the same as the set one");
    // given
    Boolean exclusiveMaximum = true;
    // when
    instance.setExclusiveMaximum(exclusiveMaximum);
    // then
    assertEquals(instance.isExclusiveMaximum(), exclusiveMaximum, "The get exclusiveMaximum must be the same as the set one");
    // given
    Double maximum = 1.0;
    // when
    instance.setMaximum(new BigDecimal(maximum));
    // then
    assertEquals(instance.getMaximum(), new BigDecimal(maximum), "The get maximum must be the same as the set one");
    // given
    Boolean exclusiveMinimum = true;
    // when
    instance.setExclusiveMinimum(exclusiveMinimum);
    // then
    assertEquals(instance.isExclusiveMinimum(), exclusiveMinimum, "The get exclusiveMinimum must be the same as the set one");
    // given
    Double minimum = 0.1;
    // when
    instance.setMinimum(new BigDecimal(minimum));
    // then
    assertEquals(instance.getMinimum(), new BigDecimal(minimum), "The get minimum must be the same as the set one");
    // given
    String example = "example";
    // when
    instance.setExample(example);
    // then
    assertEquals(instance.getExample(), example, "The get example must be the same as the set one");
    // given
    Integer maxItems = 100;
    // when
    instance.setMaxItems(maxItems);
    // then
    assertEquals(instance.getMaxItems(), maxItems, "The get maxItems must be the same as the set one");
    // given
    Integer minItems = 10;
    // when
    instance.setMinItems(minItems);
    // then
    assertEquals(instance.getMinItems(), minItems, "The get minItems must be the same as the set one");
    // given
    Integer maxLength = 500;
    // when
    instance.setMaxLength(maxLength);
    // then
    assertEquals(instance.getMaxLength(), maxLength, "The get maxLength must be the same as the set one");
    // given
    Integer minLength = 25;
    // when
    instance.setMinLength(minLength);
    // then
    assertEquals(instance.getMinLength(), minLength, "The get minLength must be the same as the set one");
    // given
    String pattern = "String pattern";
    // when
    instance.setPattern(pattern);
    // then
    assertEquals(instance.getPattern(), pattern, "The get pattern must be the same as the set one");
    // given
    Boolean uniqueItems = true;
    // when
    instance.setUniqueItems(uniqueItems);
    // then
    assertEquals(instance.isUniqueItems(), uniqueItems, "The get uniqueItems must be the same as the set one");
    // given
    Number multipleOf = 5;
    // when
    instance.setMultipleOf(multipleOf);
    // then
    assertEquals(instance.getMultipleOf(), multipleOf, "The get multipleOf must be the same as the set one");
    // given
    String defaultValue = "defaultValue";
    // when
    instance.setDefaultValue(defaultValue);
    // then
    assertEquals(instance.getDefaultValue(), defaultValue, "The get defaultValue must be the same as the set one");
    // when
    instance.required(true);
    // then
    assertTrue(instance.getRequired(), "The get required must be the same as the set one");
    // given
    StringProperty property = new StringProperty();
    property._enum(_enum);
    // when
    instance.property(property);
    // then
    assertEquals(instance.getEnum(), _enum, "The get _enum must be the same as the set one");
    assertEquals(instance.getType(), property.getType(), "The get type must be the same as the set property type");
    // given
    ArrayProperty arrayProperty = new ArrayProperty();
    // when
    arrayProperty.items(items);
    instance.property(arrayProperty);
    // then
    assertEquals(instance.getItems(), items, "The get items must be the same as the set one");
    assertEquals(instance.getType(), arrayProperty.getType(), "The get type must be the same as the set property type");
    assertEquals(instance.getDefaultCollectionFormat(), "csv", "The get collection format must be csv");
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) StringProperty(io.swagger.models.properties.StringProperty) BaseIntegerProperty(io.swagger.models.properties.BaseIntegerProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 38 with Property

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

the class Reader method parseResponseHeaders.

private Map<String, Property> parseResponseHeaders(ResponseHeader[] headers) {
    Map<String, Property> responseHeaders = null;
    if (headers != null) {
        for (ResponseHeader header : headers) {
            String name = header.name();
            if (!"".equals(name)) {
                if (responseHeaders == null) {
                    responseHeaders = new LinkedHashMap<String, Property>();
                }
                String description = header.description();
                Class<?> cls = header.response();
                if (!isVoid(cls)) {
                    final Property property = ModelConverters.getInstance().readAsProperty(cls);
                    if (property != null) {
                        Property responseProperty = ContainerWrapper.wrapContainer(header.responseContainer(), property, ContainerWrapper.ARRAY, ContainerWrapper.LIST, ContainerWrapper.SET);
                        responseProperty.setDescription(description);
                        responseHeaders.put(name, responseProperty);
                        appendModels(cls);
                    }
                }
            }
        }
    }
    return responseHeaders;
}
Also used : ResponseHeader(io.swagger.annotations.ResponseHeader) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty)

Example 39 with Property

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

the class Reader method parseMethod.

private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annotatedMethod, List<Parameter> globalParameters, List<ApiResponse> classApiResponses) {
    Operation operation = new Operation();
    if (annotatedMethod != null) {
        method = annotatedMethod.getAnnotated();
    }
    ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
    ApiResponses responseAnnotation = ReflectionUtils.getAnnotation(method, ApiResponses.class);
    String operationId = null;
    // check if it's an inherited or implemented method.
    boolean methodInSuperType = false;
    if (!cls.isInterface()) {
        methodInSuperType = ReflectionUtils.findMethod(method, cls.getSuperclass()) != null;
    }
    if (!methodInSuperType) {
        for (Class<?> implementedInterface : cls.getInterfaces()) {
            methodInSuperType = ReflectionUtils.findMethod(method, implementedInterface) != null;
            if (methodInSuperType) {
                break;
            }
        }
    }
    if (!methodInSuperType) {
        operationId = method.getName();
    } else {
        operationId = this.getOperationId(method.getName());
    }
    String responseContainer = null;
    Type responseType = null;
    Map<String, Property> defaultResponseHeaders = new LinkedHashMap<String, Property>();
    if (apiOperation != null) {
        if (apiOperation.hidden()) {
            return null;
        }
        if (!apiOperation.nickname().isEmpty()) {
            operationId = apiOperation.nickname();
        }
        defaultResponseHeaders = parseResponseHeaders(apiOperation.responseHeaders());
        operation.summary(apiOperation.value()).description(apiOperation.notes());
        if (!isVoid(apiOperation.response())) {
            responseType = apiOperation.response();
        }
        if (!apiOperation.responseContainer().isEmpty()) {
            responseContainer = apiOperation.responseContainer();
        }
        List<SecurityRequirement> securities = new ArrayList<SecurityRequirement>();
        for (Authorization auth : apiOperation.authorizations()) {
            if (!auth.value().isEmpty()) {
                SecurityRequirement security = new SecurityRequirement();
                security.setName(auth.value());
                for (AuthorizationScope scope : auth.scopes()) {
                    if (!scope.scope().isEmpty()) {
                        security.addScope(scope.scope());
                    }
                }
                securities.add(security);
            }
        }
        for (SecurityRequirement sec : securities) {
            operation.security(sec);
        }
        if (!apiOperation.consumes().isEmpty()) {
            String[] consumesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.consumes() });
            for (String consume : consumesAr) {
                operation.consumes(consume);
            }
        }
        if (!apiOperation.produces().isEmpty()) {
            String[] producesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.produces() });
            for (String produce : producesAr) {
                operation.produces(produce);
            }
        }
    }
    if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.responseReference())) {
        Response response = new Response().description(SUCCESSFUL_OPERATION);
        response.schema(new RefProperty(apiOperation.responseReference()));
        operation.addResponse(String.valueOf(apiOperation.code()), response);
    } else if (responseType == null) {
        // pick out response from method declaration
        LOGGER.debug("picking up response class from method {}", method);
        responseType = method.getGenericReturnType();
    }
    if (isValidResponse(responseType)) {
        final Property property = ModelConverters.getInstance().readAsProperty(responseType);
        if (property != null) {
            final Property responseProperty = ContainerWrapper.wrapContainer(responseContainer, property);
            final int responseCode = (apiOperation == null) ? 200 : apiOperation.code();
            operation.response(responseCode, new Response().description(SUCCESSFUL_OPERATION).schema(responseProperty).headers(defaultResponseHeaders));
            appendModels(responseType);
        }
    }
    operation.operationId(operationId);
    if (operation.getConsumes() == null || operation.getConsumes().isEmpty()) {
        final Consumes consumes = ReflectionUtils.getAnnotation(method, Consumes.class);
        if (consumes != null) {
            for (String mediaType : ReaderUtils.splitContentValues(consumes.value())) {
                operation.consumes(mediaType);
            }
        }
    }
    if (operation.getProduces() == null || operation.getProduces().isEmpty()) {
        final Produces produces = ReflectionUtils.getAnnotation(method, Produces.class);
        if (produces != null) {
            for (String mediaType : ReaderUtils.splitContentValues(produces.value())) {
                operation.produces(mediaType);
            }
        }
    }
    List<ApiResponse> apiResponses = new ArrayList<ApiResponse>();
    if (responseAnnotation != null) {
        apiResponses.addAll(Arrays.asList(responseAnnotation.value()));
    }
    Class<?>[] exceptionTypes = method.getExceptionTypes();
    for (Class<?> exceptionType : exceptionTypes) {
        ApiResponses exceptionResponses = ReflectionUtils.getAnnotation(exceptionType, ApiResponses.class);
        if (exceptionResponses != null) {
            apiResponses.addAll(Arrays.asList(exceptionResponses.value()));
        }
    }
    for (ApiResponse apiResponse : apiResponses) {
        addResponse(operation, apiResponse);
    }
    // merge class level @ApiResponse
    for (ApiResponse apiResponse : classApiResponses) {
        String key = (apiResponse.code() == 0) ? "default" : String.valueOf(apiResponse.code());
        if (operation.getResponses() != null && operation.getResponses().containsKey(key)) {
            continue;
        }
        addResponse(operation, apiResponse);
    }
    if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null) {
        operation.setDeprecated(true);
    }
    // process parameters
    for (Parameter globalParameter : globalParameters) {
        operation.parameter(globalParameter);
    }
    Annotation[][] paramAnnotations = ReflectionUtils.getParameterAnnotations(method);
    if (annotatedMethod == null) {
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        for (int i = 0; i < genericParameterTypes.length; i++) {
            final Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls);
            List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]));
            for (Parameter parameter : parameters) {
                operation.parameter(parameter);
            }
        }
    } else {
        for (int i = 0; i < annotatedMethod.getParameterCount(); i++) {
            AnnotatedParameter param = annotatedMethod.getParameter(i);
            final Type type = TypeFactory.defaultInstance().constructType(param.getParameterType(), cls);
            List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]));
            for (Parameter parameter : parameters) {
                operation.parameter(parameter);
            }
        }
    }
    if (operation.getResponses() == null) {
        Response response = new Response().description(SUCCESSFUL_OPERATION);
        operation.defaultResponse(response);
    }
    processOperationDecorator(operation, method);
    return operation;
}
Also used : AnnotatedParameter(com.fasterxml.jackson.databind.introspect.AnnotatedParameter) ArrayList(java.util.ArrayList) ApiOperation(io.swagger.annotations.ApiOperation) Operation(io.swagger.models.Operation) ApiResponse(io.swagger.annotations.ApiResponse) LinkedHashMap(java.util.LinkedHashMap) RefProperty(io.swagger.models.properties.RefProperty) Authorization(io.swagger.annotations.Authorization) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) ApiResponses(io.swagger.annotations.ApiResponses) Response(io.swagger.models.Response) ApiResponse(io.swagger.annotations.ApiResponse) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) ParameterizedType(java.lang.reflect.ParameterizedType) Produces(javax.ws.rs.Produces) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) AnnotatedParameter(com.fasterxml.jackson.databind.introspect.AnnotatedParameter) AuthorizationScope(io.swagger.annotations.AuthorizationScope) SecurityRequirement(io.swagger.models.SecurityRequirement)

Example 40 with Property

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

the class PostParamTest method findAPostOperationWithStringsArray.

@Test(description = "find a Post operation with an array of strings")
public void findAPostOperationWithStringsArray() {
    Path petPath = getPath("arrayOfStrings");
    assertNotNull(petPath);
    Operation petPost = petPath.getPost();
    assertNotNull(petPost);
    assertEquals(petPost.getParameters().size(), 1);
    BodyParameter petPostBodyParam = (BodyParameter) petPost.getParameters().get(0);
    assertEquals(petPostBodyParam.getName(), BODY);
    Model inputModel = petPostBodyParam.getSchema();
    assertTrue(inputModel instanceof ArrayModel);
    ArrayModel ap = (ArrayModel) inputModel;
    Property inputSchema = ap.getItems();
    assertTrue(inputSchema instanceof StringProperty);
}
Also used : Path(io.swagger.models.Path) Model(io.swagger.models.Model) ArrayModel(io.swagger.models.ArrayModel) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) ArrayModel(io.swagger.models.ArrayModel) StringProperty(io.swagger.models.properties.StringProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) Test(org.testng.annotations.Test)

Aggregations

Property (io.swagger.models.properties.Property)145 Test (org.testng.annotations.Test)96 ArrayProperty (io.swagger.models.properties.ArrayProperty)86 StringProperty (io.swagger.models.properties.StringProperty)75 RefProperty (io.swagger.models.properties.RefProperty)65 MapProperty (io.swagger.models.properties.MapProperty)59 Model (io.swagger.models.Model)52 IntegerProperty (io.swagger.models.properties.IntegerProperty)48 LongProperty (io.swagger.models.properties.LongProperty)35 DoubleProperty (io.swagger.models.properties.DoubleProperty)32 FloatProperty (io.swagger.models.properties.FloatProperty)27 BooleanProperty (io.swagger.models.properties.BooleanProperty)25 ObjectProperty (io.swagger.models.properties.ObjectProperty)25 Operation (io.swagger.models.Operation)24 DateTimeProperty (io.swagger.models.properties.DateTimeProperty)23 DateProperty (io.swagger.models.properties.DateProperty)22 ApiModelProperty (io.swagger.annotations.ApiModelProperty)20 FileProperty (io.swagger.models.properties.FileProperty)18 ModelImpl (io.swagger.models.ModelImpl)17 Response (io.swagger.models.Response)16