Search in sources :

Example 36 with Parameter

use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.

the class ServletReaderExtension method readImplicitParam.

private Parameter readImplicitParam(Swagger swagger, ApiImplicitParam param) {
    final Parameter p = ParameterFactory.createParam(param.paramType());
    if (p == null) {
        return null;
    }
    final Type type = ReflectionUtils.typeFromString(param.dataType());
    return ParameterProcessor.applyAnnotations(swagger, p, type == null ? String.class : type, Collections.<Annotation>singletonList(param));
}
Also used : Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) 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) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 37 with Parameter

use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.

the class OperationTest method testParameter.

@Test
public void testParameter() {
    // given
    Parameter parameter = Mockito.mock(Parameter.class);
    operation.setParameters(null);
    // when
    operation.parameter(parameter);
    assertTrue(operation.getParameters().contains(parameter), "The newly added parameter must be contained in the parameters list");
}
Also used : Parameter(io.swagger.models.parameters.Parameter) Test(org.testng.annotations.Test)

Example 38 with Parameter

use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.

the class SwaggerTest method testParameter.

@Test
public void testParameter() {
    // given
    Parameter parameter = Mockito.mock(Parameter.class);
    // when
    swagger.setParameters(null);
    String key = "key";
    // then
    assertNull(swagger.getParameter(key), "Cannot retrieve a key without adding it first");
    // when
    swagger.parameter(key, parameter);
    // then
    assertEquals(swagger.getParameters().get(key), parameter, "Must be able to retrieve the added key");
    assertEquals(swagger.getParameter(key), parameter, "Must be able to retrieave the added key");
}
Also used : Parameter(io.swagger.models.parameters.Parameter) Test(org.testng.annotations.Test)

Example 39 with Parameter

use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.

the class DefaultParameterExtension method handleAdditionalAnnotation.

/**
     * Adds additional annotation processing support 
     * 
     * @param parameters
     * @param annotation
     * @param type
     * @param typesToSkip
     */
private void handleAdditionalAnnotation(List<Parameter> parameters, Annotation annotation, final Type type, Set<Type> typesToSkip) {
    if (CLASS_BEAN_PARAM != null && CLASS_BEAN_PARAM.isAssignableFrom(annotation.getClass())) {
        // Use Jackson's logic for processing Beans
        final BeanDescription beanDesc = mapper.getSerializationConfig().introspect(constructType(type));
        final List<BeanPropertyDefinition> properties = beanDesc.findProperties();
        for (final BeanPropertyDefinition propDef : properties) {
            final AnnotatedField field = propDef.getField();
            final AnnotatedMethod setter = propDef.getSetter();
            final AnnotatedMethod getter = propDef.getGetter();
            final List<Annotation> paramAnnotations = new ArrayList<Annotation>();
            final Iterator<SwaggerExtension> extensions = SwaggerExtensions.chain();
            Type paramType = null;
            // Gather the field's details
            if (field != null) {
                paramType = field.getRawType();
                for (final Annotation fieldAnnotation : field.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            // Gather the setter's details but only the ones we need
            if (setter != null) {
                // Do not set the param class/type from the setter if the values are already identified
                if (paramType == null) {
                    paramType = setter.getRawParameterTypes() != null ? setter.getRawParameterTypes()[0] : null;
                }
                for (final Annotation fieldAnnotation : setter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            // Gather the getter's details but only the ones we need
            if (getter != null) {
                // Do not set the param class/type from the getter if the values are already identified
                if (paramType == null) {
                    paramType = getter.getRawReturnType();
                }
                for (final Annotation fieldAnnotation : getter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            if (paramType == null) {
                continue;
            }
            // Re-process all Bean fields and let the default swagger-jaxrs/swagger-jersey-jaxrs processors do their thing
            List<Parameter> extracted = extensions.next().extractParameters(paramAnnotations, paramType, typesToSkip, extensions);
            // since downstream processors won't know how to introspect @BeanParam, process here
            for (Parameter param : extracted) {
                if (ParameterProcessor.applyAnnotations(null, param, paramType, paramAnnotations) != null) {
                    parameters.add(param);
                }
            }
        }
    }
}
Also used : AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type) AbstractSwaggerExtension(io.swagger.jaxrs.ext.AbstractSwaggerExtension) SwaggerExtension(io.swagger.jaxrs.ext.SwaggerExtension) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) HeaderParameter(io.swagger.models.parameters.HeaderParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField)

Example 40 with Parameter

use of io.swagger.models.parameters.Parameter in project swagger-core by swagger-api.

the class Reader method getParameters.

private List<Parameter> getParameters(Type type, List<Annotation> annotations) {
    final Iterator<SwaggerExtension> chain = SwaggerExtensions.chain();
    if (!chain.hasNext()) {
        return Collections.emptyList();
    }
    LOGGER.debug("getParameters for {}", type);
    Set<Type> typesToSkip = new HashSet<Type>();
    final SwaggerExtension extension = chain.next();
    LOGGER.debug("trying extension {}", extension);
    final List<Parameter> parameters = extension.extractParameters(annotations, type, typesToSkip, chain);
    if (!parameters.isEmpty()) {
        final List<Parameter> processed = new ArrayList<Parameter>(parameters.size());
        for (Parameter parameter : parameters) {
            if (ParameterProcessor.applyAnnotations(swagger, parameter, type, annotations) != null) {
                processed.add(parameter);
            }
        }
        return processed;
    } else {
        LOGGER.debug("no parameter found, looking at body params");
        final List<Parameter> body = new ArrayList<Parameter>();
        if (!typesToSkip.contains(type)) {
            Parameter param = ParameterProcessor.applyAnnotations(swagger, null, type, annotations);
            if (param != null) {
                body.add(param);
            }
        }
        return body;
    }
}
Also used : Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) ParameterizedType(java.lang.reflect.ParameterizedType) SwaggerExtension(io.swagger.jaxrs.ext.SwaggerExtension) ArrayList(java.util.ArrayList) 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) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Parameter (io.swagger.models.parameters.Parameter)186 BodyParameter (io.swagger.models.parameters.BodyParameter)102 QueryParameter (io.swagger.models.parameters.QueryParameter)101 PathParameter (io.swagger.models.parameters.PathParameter)89 Test (org.testng.annotations.Test)80 HeaderParameter (io.swagger.models.parameters.HeaderParameter)66 Swagger (io.swagger.models.Swagger)63 FormParameter (io.swagger.models.parameters.FormParameter)52 Operation (io.swagger.models.Operation)31 SerializableParameter (io.swagger.models.parameters.SerializableParameter)29 Test (org.junit.Test)25 Type (java.lang.reflect.Type)23 ArrayList (java.util.ArrayList)23 Path (io.swagger.models.Path)19 Annotation (java.lang.annotation.Annotation)17 JavaType (com.fasterxml.jackson.databind.JavaType)14 Response (io.swagger.models.Response)14 Method (java.lang.reflect.Method)14 Property (io.swagger.models.properties.Property)13 ModelImpl (io.swagger.models.ModelImpl)12