Search in sources :

Example 11 with HeaderParameter

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

the class ParameterSerializationTest method serializeStringArrayHeaderParameter.

@Test(description = "it should serialize a string array HeaderParameter")
public void serializeStringArrayHeaderParameter() {
    final HeaderParameter p = new HeaderParameter().type(ArrayProperty.TYPE).property(new StringProperty()).collectionFormat("multi");
    final String json = "{\"in\":\"header\",\"required\":false,\"type\":\"string\",\"collectionFormat\":\"multi\"}";
    SerializationMatchers.assertEqualsToJson(p, json);
}
Also used : StringProperty(io.swagger.models.properties.StringProperty) HeaderParameter(io.swagger.models.parameters.HeaderParameter) Test(org.testng.annotations.Test)

Example 12 with HeaderParameter

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

the class ReaderTest method scanImplicitParam.

@Test(description = "scan implicit params")
public void scanImplicitParam() {
    Swagger swagger = getSwagger(ResourceWithImplicitParams.class);
    List<Parameter> params = swagger.getPath("/testString").getPost().getParameters();
    assertNotNull(params);
    assertEquals(params.size(), 7);
    assertEquals(params.get(0).getName(), "sort");
    assertEquals(params.get(0).getIn(), "query");
    PathParameter pathParam = (PathParameter) params.get(1);
    assertEquals(pathParam.getName(), "type");
    assertEquals(pathParam.getIn(), "path");
    assertEquals(pathParam.getEnum().size(), 3);
    assertEquals(pathParam.getType(), "string");
    HeaderParameter headerParam = (HeaderParameter) params.get(2);
    assertEquals(headerParam.getName(), "size");
    assertEquals(headerParam.getIn(), "header");
    assertEquals(headerParam.getMinimum(), new BigDecimal(1.0));
    FormParameter formParam = (FormParameter) params.get(3);
    assertEquals(formParam.getName(), "width");
    assertEquals(formParam.getIn(), "formData");
    assertEquals(formParam.getMaximum(), new BigDecimal(1.0));
    assertEquals(params.get(4).getName(), "width");
    assertEquals(params.get(4).getIn(), "formData");
    QueryParameter queryParam = (QueryParameter) params.get(5);
    assertEquals(queryParam.getName(), "height");
    assertEquals(queryParam.getIn(), "query");
    assertEquals(queryParam.getMinimum(), new BigDecimal(3.0));
    assertEquals(queryParam.getMaximum(), new BigDecimal(4.0));
    BodyParameter bodyParam = (BodyParameter) params.get(6);
    assertEquals(bodyParam.getName(), "body");
    assertEquals(bodyParam.getIn(), "body");
    assertTrue(bodyParam.getRequired());
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) Swagger(io.swagger.models.Swagger) 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) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) FormParameter(io.swagger.models.parameters.FormParameter) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 13 with HeaderParameter

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

the class SwaggerJersey2JaxrsTest method returnProperBeanParamWithDefaultParameterExtension.

@Test(description = "return the proper @BeanParam Parameters based on the call to DefaultParameterExtension.extractParameters")
public void returnProperBeanParamWithDefaultParameterExtension() throws NoSuchMethodException {
    final Method method = getClass().getDeclaredMethod("testRoute", BaseBean.class, ChildBean.class, RefBean.class, EnumBean.class, Integer.class);
    final List<Pair<Type, Annotation[]>> parameters = getParameters(method.getGenericParameterTypes(), method.getParameterAnnotations());
    for (Pair<Type, Annotation[]> parameter : parameters) {
        Type parameterType = parameter.first();
        List<Parameter> swaggerParams = new DefaultParameterExtension().extractParameters(Arrays.asList(parameter.second()), parameterType, new HashSet<Type>(), SwaggerExtensions.chain());
        // Ensure proper number of parameters returned
        if (parameterType.equals(BaseBean.class)) {
            assertEquals(swaggerParams.size(), 2);
        } else if (parameterType.equals(ChildBean.class)) {
            assertEquals(swaggerParams.size(), 5);
        } else if (parameterType.equals(RefBean.class)) {
            assertEquals(swaggerParams.size(), 5);
        } else if (parameterType.equals(EnumBean.class)) {
            assertEquals(swaggerParams.size(), 1);
            HeaderParameter enumParam = (HeaderParameter) swaggerParams.get(0);
            assertEquals(enumParam.getType(), "string");
            final Set<String> enumValues = Sets.newHashSet(Collections2.transform(Arrays.asList(TestEnum.values()), Functions.toStringFunction()));
            assertEquals(enumParam.getEnum(), enumValues);
        } else if (parameterType.equals(Integer.class)) {
            assertEquals(swaggerParams.size(), 0);
        } else {
            fail(String.format("Parameter of type %s was not expected", parameterType));
        }
        // Ensure the proper parameter type and name is returned (The rest is handled by pre-existing logic)
        for (Parameter param : swaggerParams) {
            assertEquals(param.getName(), param.getClass().getSimpleName().replace("eter", ""));
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ChildBean(io.swagger.params.ChildBean) Type(java.lang.reflect.Type) EnumBean(io.swagger.params.EnumBean) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FormParameter(io.swagger.models.parameters.FormParameter) Parameter(io.swagger.models.parameters.Parameter) DefaultParameterExtension(io.swagger.jaxrs.DefaultParameterExtension) HeaderParameter(io.swagger.models.parameters.HeaderParameter) Pair(org.testng.internal.collections.Pair) Test(org.testng.annotations.Test)

Example 14 with HeaderParameter

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

the class SwaggerJersey2JaxrsTest method returnProperBeanParam.

@Test(description = "return the proper @BeanParam Parameters based on the call to extractParameters")
public void returnProperBeanParam() throws NoSuchMethodException {
    final Method method = getClass().getDeclaredMethod("testRoute", BaseBean.class, ChildBean.class, RefBean.class, EnumBean.class, Integer.class);
    final List<Pair<Type, Annotation[]>> parameters = getParameters(method.getGenericParameterTypes(), method.getParameterAnnotations());
    for (Pair<Type, Annotation[]> parameter : parameters) {
        Type parameterType = parameter.first();
        List<Parameter> swaggerParams = new SwaggerJersey2Jaxrs().extractParameters(Arrays.asList(parameter.second()), parameterType, new HashSet<Type>(), SwaggerExtensions.chain());
        // Ensure proper number of parameters returned
        if (parameterType.equals(BaseBean.class)) {
            assertEquals(swaggerParams.size(), 2);
        } else if (parameterType.equals(ChildBean.class)) {
            assertEquals(swaggerParams.size(), 5);
        } else if (parameterType.equals(RefBean.class)) {
            assertEquals(swaggerParams.size(), 5);
        } else if (parameterType.equals(EnumBean.class)) {
            assertEquals(swaggerParams.size(), 1);
            HeaderParameter enumParam = (HeaderParameter) swaggerParams.get(0);
            assertEquals(enumParam.getType(), "string");
            final Set<String> enumValues = Sets.newHashSet(Collections2.transform(Arrays.asList(TestEnum.values()), Functions.toStringFunction()));
            assertEquals(enumParam.getEnum(), enumValues);
        } else if (parameterType.equals(Integer.class)) {
            assertEquals(swaggerParams.size(), 0);
        } else {
            fail(String.format("Parameter of type %s was not expected", parameterType));
        }
        // Ensure the proper parameter type and name is returned (The rest is handled by pre-existing logic)
        for (Parameter param : swaggerParams) {
            assertEquals(param.getName(), param.getClass().getSimpleName().replace("eter", ""));
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ChildBean(io.swagger.params.ChildBean) Type(java.lang.reflect.Type) SwaggerJersey2Jaxrs(io.swagger.jersey.SwaggerJersey2Jaxrs) EnumBean(io.swagger.params.EnumBean) HeaderParameter(io.swagger.models.parameters.HeaderParameter) FormParameter(io.swagger.models.parameters.FormParameter) Parameter(io.swagger.models.parameters.Parameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) Pair(org.testng.internal.collections.Pair) Test(org.testng.annotations.Test)

Aggregations

HeaderParameter (io.swagger.models.parameters.HeaderParameter)14 Parameter (io.swagger.models.parameters.Parameter)10 Test (org.testng.annotations.Test)10 QueryParameter (io.swagger.models.parameters.QueryParameter)7 FormParameter (io.swagger.models.parameters.FormParameter)6 PathParameter (io.swagger.models.parameters.PathParameter)6 BodyParameter (io.swagger.models.parameters.BodyParameter)5 StringProperty (io.swagger.models.properties.StringProperty)5 Type (java.lang.reflect.Type)5 Method (java.lang.reflect.Method)4 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 Property (io.swagger.models.properties.Property)3 Annotation (java.lang.annotation.Annotation)3 Swagger (io.swagger.models.Swagger)2 DoubleProperty (io.swagger.models.properties.DoubleProperty)2 IntegerProperty (io.swagger.models.properties.IntegerProperty)2 LongProperty (io.swagger.models.properties.LongProperty)2 RefProperty (io.swagger.models.properties.RefProperty)2 ChildBean (io.swagger.params.ChildBean)2 EnumBean (io.swagger.params.EnumBean)2