Search in sources :

Example 61 with Parameter

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

the class ParameterSerializationTest method deserializeReadOnlyParameter.

@Test(description = "it should deserialize a read only parameter")
public void deserializeReadOnlyParameter() throws IOException {
    final String json = "{" + "   \"in\":\"path\"," + "   \"required\":false," + "   \"type\":\"string\"," + "   \"readOnly\":\"true\"" + "}";
    final Parameter p = m.readValue(json, Parameter.class);
    assertTrue(p.isReadOnly());
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) Test(org.testng.annotations.Test)

Example 62 with Parameter

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

the class ReaderUtils method collectConstructorParameters.

/**
     * Collects constructor-level parameters from class.
     *
     * @param cls     is a class for collecting
     * @param swagger is the instance of the Swagger
     * @return the collection of supported parameters
     */
public static List<Parameter> collectConstructorParameters(Class<?> cls, Swagger swagger) {
    if (cls.isLocalClass() || (cls.isMemberClass() && !Modifier.isStatic(cls.getModifiers()))) {
        return Collections.emptyList();
    }
    List<Parameter> selected = Collections.emptyList();
    int maxParamsCount = 0;
    for (Constructor<?> constructor : cls.getDeclaredConstructors()) {
        if (!ReflectionUtils.isConstructorCompatible(constructor) && !ReflectionUtils.isInject(Arrays.asList(constructor.getDeclaredAnnotations()))) {
            continue;
        }
        final Type[] genericParameterTypes = constructor.getGenericParameterTypes();
        final Annotation[][] annotations = constructor.getParameterAnnotations();
        int paramsCount = 0;
        final List<Parameter> parameters = new ArrayList<Parameter>();
        for (int i = 0; i < genericParameterTypes.length; i++) {
            final List<Annotation> tmpAnnotations = Arrays.asList(annotations[i]);
            if (isContext(tmpAnnotations)) {
                paramsCount++;
            } else {
                final Type genericParameterType = genericParameterTypes[i];
                final List<Parameter> tmpParameters = collectParameters(genericParameterType, tmpAnnotations);
                if (tmpParameters.size() >= 1) {
                    for (Parameter tmpParameter : tmpParameters) {
                        if (ParameterProcessor.applyAnnotations(swagger, tmpParameter, genericParameterType, tmpAnnotations) != null) {
                            parameters.add(tmpParameter);
                        }
                    }
                    paramsCount++;
                }
            }
        }
        if (paramsCount >= maxParamsCount) {
            maxParamsCount = paramsCount;
            selected = parameters;
        }
    }
    return selected;
}
Also used : Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) Parameter(io.swagger.models.parameters.Parameter) Annotation(java.lang.annotation.Annotation)

Example 63 with Parameter

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

the class ReaderUtils method collectFieldParameters.

/**
     * Collects field-level parameters from class.
     *
     * @param cls     is a class for collecting
     * @param swagger is the instance of the Swagger
     * @return the collection of supported parameters
     */
public static List<Parameter> collectFieldParameters(Class<?> cls, Swagger swagger) {
    final List<Parameter> parameters = new ArrayList<Parameter>();
    for (Field field : ReflectionUtils.getDeclaredFields(cls)) {
        final List<Annotation> annotations = Arrays.asList(field.getAnnotations());
        final Type genericType = field.getGenericType();
        for (Parameter parameter : collectParameters(genericType, annotations)) {
            if (ParameterProcessor.applyAnnotations(swagger, parameter, genericType, annotations) != null) {
                parameters.add(parameter);
            }
        }
    }
    return parameters;
}
Also used : Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) Parameter(io.swagger.models.parameters.Parameter) Annotation(java.lang.annotation.Annotation)

Example 64 with Parameter

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

the class SimpleReaderTest method scanResourceWithParamRange.

@Test(description = "scan a resource with param range")
public void scanResourceWithParamRange() {
    Swagger swagger = getSwagger(ResourceWithRanges.class);
    List<Parameter> params = getGetParameters(swagger, "/{id}");
    PathParameter param0 = (PathParameter) params.get(0);
    assertEquals(param0.getName(), "id");
    assertEquals(param0.getDefaultValue(), 5);
    assertEquals(param0.getMinimum(), new BigDecimal(0.0));
    assertEquals(param0.getMaximum(), new BigDecimal(10.0));
    PathParameter param1 = (PathParameter) params.get(1);
    assertEquals(param1.getName(), "minValue");
    assertEquals(param1.getMinimum(), new BigDecimal(0.0));
    assertNull(param1.getMaximum(), null);
    PathParameter param2 = (PathParameter) params.get(2);
    assertEquals(param2.getName(), "maxValue");
    assertNull(param2.getMinimum());
    assertEquals(param2.getMaximum(), new BigDecimal(100.0));
    PathParameter param3 = (PathParameter) params.get(3);
    assertEquals(param3.getName(), "values");
    IntegerProperty items = (IntegerProperty) param3.getItems();
    assertEquals(items.getMinimum(), new BigDecimal(0.0));
    assertEquals(items.getMaximum(), new BigDecimal(5.0));
    assertEquals(items.getExclusiveMinimum(), Boolean.TRUE);
    assertEquals(items.getExclusiveMaximum(), Boolean.TRUE);
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) Swagger(io.swagger.models.Swagger) SerializableParameter(io.swagger.models.parameters.SerializableParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 65 with Parameter

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

the class SimpleReaderTest method scanClassWithExampleClassQuery.

@Test(description = "scan a resource with query param example (dataTypeClass)")
public void scanClassWithExampleClassQuery() {
    Swagger swagger = getSwagger(ClassWithExamplePostClass.class);
    Parameter param = swagger.getPaths().get("/external/info").getGet().getParameters().get(0);
    QueryParameter bp = (QueryParameter) param;
    assertNotNull(bp.getExample());
    Object value = bp.getExample();
    assertEquals("a,b,c", value);
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) Swagger(io.swagger.models.Swagger) SerializableParameter(io.swagger.models.parameters.SerializableParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Test(org.testng.annotations.Test)

Aggregations

Parameter (io.swagger.models.parameters.Parameter)92 BodyParameter (io.swagger.models.parameters.BodyParameter)54 QueryParameter (io.swagger.models.parameters.QueryParameter)53 PathParameter (io.swagger.models.parameters.PathParameter)51 Test (org.testng.annotations.Test)51 HeaderParameter (io.swagger.models.parameters.HeaderParameter)38 Swagger (io.swagger.models.Swagger)33 FormParameter (io.swagger.models.parameters.FormParameter)27 Operation (io.swagger.models.Operation)20 SerializableParameter (io.swagger.models.parameters.SerializableParameter)15 Type (java.lang.reflect.Type)14 ArrayList (java.util.ArrayList)14 Annotation (java.lang.annotation.Annotation)10 Path (io.swagger.models.Path)9 Method (java.lang.reflect.Method)9 JavaType (com.fasterxml.jackson.databind.JavaType)7 ContextParameter (io.servicecomb.swagger.extend.parameter.ContextParameter)6 Property (io.swagger.models.properties.Property)6 HashMap (java.util.HashMap)6 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)5