Search in sources :

Example 36 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project incubator-servicecomb-java-chassis by apache.

the class TestPath method testQueryVarParamWriter.

@Test
public void testQueryVarParamWriter() throws Exception {
    boolean status = true;
    new MockUp<RestParam>() {

        @Mock
        public String getParamName() {
            return "queryVar";
        }
    };
    new MockUp<QueryVarParamWriter>() {

        @Mock
        protected Object getParamValue(Object[] args) {
            return args[0];
        }
    };
    Parameter parameter = new QueryParameter();
    QueryVarParamWriter writer = new QueryVarParamWriter('&', new RestParam(0, parameter, String.class));
    try {
        verify(writer, "T", "&queryVar=T");
        verify(writer, null, "&");
        verify(writer, new String[] { "a", "b" }, "&queryVar=a&queryVar=b");
        verify(writer, new String[] { "a", null, "b" }, "&queryVar=a&queryVar=&queryVar=b");
        verify(writer, Arrays.asList("Lars", "Simon"), "&queryVar=Lars&queryVar=Simon");
        verify(writer, "测试", "&queryVar=%E6%B5%8B%E8%AF%95");
        verify(writer, "a b", "&queryVar=a+b");
        verify(writer, "a+b", "&queryVar=a%2Bb");
    } catch (Exception e) {
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) QueryVarParamWriter(org.apache.servicecomb.common.rest.definition.path.QueryVarParamWriter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 37 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project herd by FINRAOS.

the class RestControllerProcessor method processRestMethodParameter.

/**
 * Process a REST method parameter.
 *
 * @param parameterSource the parameter source information.
 * @param operation the Swagger operation.
 * @param methodParamDescriptions the method parameter Javadoc descriptions.
 *
 * @throws MojoExecutionException if any problems were encountered.
 */
private void processRestMethodParameter(ParameterSource<JavaClassSource> parameterSource, Operation operation, Map<String, String> methodParamDescriptions) throws MojoExecutionException {
    log.debug("Processing parameter \"" + parameterSource.getName() + "\".");
    try {
        AnnotationSource<JavaClassSource> requestParamAnnotationSource = parameterSource.getAnnotation(RequestParam.class);
        AnnotationSource<JavaClassSource> requestBodyAnnotationSource = parameterSource.getAnnotation(RequestBody.class);
        AnnotationSource<JavaClassSource> pathVariableAnnotationSource = parameterSource.getAnnotation(PathVariable.class);
        if (requestParamAnnotationSource != null) {
            log.debug("Parameter \"" + parameterSource.getName() + "\" is a RequestParam.");
            QueryParameter queryParameter = new QueryParameter();
            queryParameter.name(requestParamAnnotationSource.getStringValue("value").trim());
            queryParameter.setRequired(BooleanUtils.toBoolean(requestParamAnnotationSource.getStringValue("required")));
            setParameterType(parameterSource, queryParameter);
            operation.parameter(queryParameter);
            setParamDescription(parameterSource, methodParamDescriptions, queryParameter);
        } else if (requestBodyAnnotationSource != null) {
            log.debug("Parameter \"" + parameterSource.getName() + "\" is a RequestBody.");
            // Add the class name to the list of classes which we will create an example for.
            exampleClassNames.add(parameterSource.getType().getSimpleName());
            BodyParameter bodyParameter = new BodyParameter();
            XmlType xmlType = getXmlType(Class.forName(parameterSource.getType().getQualifiedName()));
            String name = xmlType.name().trim();
            bodyParameter.name(name);
            bodyParameter.setRequired(true);
            bodyParameter.setSchema(new RefModel(name));
            operation.parameter(bodyParameter);
            setParamDescription(parameterSource, methodParamDescriptions, bodyParameter);
        } else if (pathVariableAnnotationSource != null) {
            log.debug("Parameter \"" + parameterSource.getName() + "\" is a PathVariable.");
            PathParameter pathParameter = new PathParameter();
            pathParameter.name(pathVariableAnnotationSource.getStringValue("value").trim());
            setParameterType(parameterSource, pathParameter);
            operation.parameter(pathParameter);
            setParamDescription(parameterSource, methodParamDescriptions, pathParameter);
        }
    } catch (ClassNotFoundException e) {
        throw new MojoExecutionException("Unable to instantiate class \"" + parameterSource.getType().getQualifiedName() + "\". Reason: " + e.getMessage(), e);
    }
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) XmlType(javax.xml.bind.annotation.XmlType)

Example 38 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method addResourceParameters.

/**
 * Creates parameters in the swagger operation using the parameters in the ballerina resource definition.
 *
 * @param resource         The ballerina resource definition.
 * @param operationAdaptor The swagger operation.
 */
private void addResourceParameters(ResourceInfo resource, OperationAdaptor operationAdaptor) {
    if (!"get".equalsIgnoreCase(operationAdaptor.getHttpOperation())) {
        // Creating message body - required.
        ModelImpl messageModel = new ModelImpl();
        messageModel.setType("object");
        Map<String, Model> definitions = new HashMap<>();
        if (!definitions.containsKey("Message")) {
            definitions.put("Message", messageModel);
            this.swaggerDefinition.setDefinitions(definitions);
        }
        // Creating "Message m" parameter
        BodyParameter messageParameter = new BodyParameter();
        messageParameter.setName(resource.getParamNames()[0]);
        RefModel refModel = new RefModel();
        refModel.setReference("Message");
        messageParameter.setSchema(refModel);
        operationAdaptor.getOperation().addParameter(messageParameter);
    }
    // Creating query params and path params
    AttributeInfo attributeInfo = resource.getAttributeInfo(AttributeInfo.Kind.PARAMETER_ANNOTATIONS_ATTRIBUTE);
    if (attributeInfo instanceof ParamAnnotationAttributeInfo) {
        ParamAnnotationAttributeInfo paramAttributeInfo = (ParamAnnotationAttributeInfo) resource.getAttributeInfo(AttributeInfo.Kind.PARAMETER_ANNOTATIONS_ATTRIBUTE);
        ParamAnnAttachmentInfo[] attachmentInfoArray = paramAttributeInfo.getAttachmentInfoArray();
        for (ParamAnnAttachmentInfo paramAnnAttachmentInfo : attachmentInfoArray) {
            if (paramAnnAttachmentInfo.getAnnAttachmentInfos().length > 0) {
                AnnAttachmentInfo annAttachmentInfo = paramAnnAttachmentInfo.getAnnAttachmentInfos()[0];
                Map<String, AnnAttributeValue> paramAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(annAttachmentInfo);
                if (paramAnnAttributeValueMap.size() == 1 && null != paramAnnAttributeValueMap.get("value")) {
                    // Add query parameter
                    if (annAttachmentInfo.getName().equalsIgnoreCase("QueryParam")) {
                        QueryParameter queryParameter = new QueryParameter();
                        // Set in value.
                        queryParameter.setIn("query");
                        // Set parameter name
                        String parameterName = paramAnnAttributeValueMap.get("value").getStringValue();
                        if ((parameterName == null) || parameterName.isEmpty()) {
                            parameterName = resource.getParamNames()[paramAnnAttachmentInfo.getParamIdex()];
                        }
                        queryParameter.setName(parameterName);
                        // Note: 'description' to be added using annotations, hence skipped here.
                        // Setting false to required(as per swagger spec). This can be overridden while parsing
                        // annotations.
                        queryParameter.required(false);
                        // Note: 'allowEmptyValue' to be added using annotations, hence skipped here.
                        // Set type
                        String paramType = resource.getParamTypes()[paramAnnAttachmentInfo.getParamIdex()].getName();
                        if ("int".equals(paramType)) {
                            queryParameter.setType("integer");
                        } else {
                            queryParameter.setType(paramType);
                        }
                        // Note: 'format' to be added using annotations, hence skipped here.
                        operationAdaptor.getOperation().addParameter(queryParameter);
                    }
                    if (annAttachmentInfo.getName().equalsIgnoreCase("PathParam")) {
                        PathParameter pathParameter = new PathParameter();
                        // Set in value
                        pathParameter.setIn("path");
                        // Set parameter name
                        String parameterName = paramAnnAttributeValueMap.get("value").getStringValue();
                        if ((parameterName == null) || parameterName.isEmpty()) {
                            parameterName = resource.getParamNames()[paramAnnAttachmentInfo.getParamIdex()];
                        }
                        pathParameter.setName(parameterName);
                        // Note: 'description' to be added using annotations, hence skipped here.
                        // Note: 'allowEmptyValue' to be added using annotations, hence skipped here.
                        // Set type
                        String paramType = resource.getParamTypes()[paramAnnAttachmentInfo.getParamIdex()].getName();
                        if ("int".equals(paramType)) {
                            pathParameter.setType("integer");
                        } else {
                            pathParameter.setType(paramType);
                        }
                        // Note: 'format' to be added using annotations, hence skipped here.
                        operationAdaptor.getOperation().addParameter(pathParameter);
                    }
                }
            }
        }
    }
}
Also used : ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) ParamAnnotationAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamAnnotationAttributeInfo) HashMap(java.util.HashMap) ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) ParamAnnotationAttributeInfo(org.ballerinalang.util.codegen.attributes.ParamAnnotationAttributeInfo) AttributeInfo(org.ballerinalang.util.codegen.attributes.AttributeInfo) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) ModelImpl(io.swagger.models.ModelImpl)

Example 39 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project java-chassis by ServiceComb.

the class TestQueryProcessor method createProcessor.

private ParamValueProcessor createProcessor(String name, Class<?> type, String defaultValue, boolean required, String collectionFormat) {
    JavaType javaType = TypeFactory.defaultInstance().constructType(type);
    QueryParameter queryParameter = new QueryParameter();
    queryParameter.name(name).required(required).collectionFormat(collectionFormat).setDefaultValue(defaultValue);
    if (javaType.isContainerType()) {
        queryParameter.type(ArrayProperty.TYPE);
    }
    return new QueryProcessor(queryParameter, javaType);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) QueryParameter(io.swagger.models.parameters.QueryParameter) QueryProcessor(org.apache.servicecomb.common.rest.codec.param.QueryProcessorCreator.QueryProcessor)

Example 40 with QueryParameter

use of io.swagger.models.parameters.QueryParameter in project java-chassis by ServiceComb.

the class TestPath method testQueryVarParamWriter.

@Test
public void testQueryVarParamWriter() {
    boolean status = true;
    new MockUp<RestParam>() {

        @Mock
        public String getParamName() {
            return "queryVar";
        }
    };
    new MockUp<QueryVarParamWriter>() {

        @Mock
        protected Object getParamValue(Object[] args) {
            return args[0];
        }
    };
    Parameter parameter = new QueryParameter();
    QueryVarParamWriter writer = new QueryVarParamWriter(new RestParam(parameter, String.class));
    try {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("queryVar", "T");
        verify(writer, parameters, "?queryVar=T");
        parameters.put("queryVar", null);
        verify(writer, parameters, "");
        parameters.put("queryVar", new String[] { "a", "b" });
        verify(writer, parameters, "?queryVar=a&queryVar=b");
        parameters.put("queryVar", new String[] { "a", null, "b" });
        verify(writer, parameters, "?queryVar=a&queryVar=b");
        parameters.put("queryVar", Arrays.asList("Lars", "Simon"));
        verify(writer, parameters, "?queryVar=Lars&queryVar=Simon");
        parameters.put("queryVar", "测试");
        verify(writer, parameters, "?queryVar=%E6%B5%8B%E8%AF%95");
        parameters.put("queryVar", "a b");
        verify(writer, parameters, "?queryVar=a+b");
        parameters.put("queryVar", "a+b");
        verify(writer, parameters, "?queryVar=a%2Bb");
    } catch (Exception e) {
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) QueryVarParamWriter(org.apache.servicecomb.common.rest.definition.path.QueryVarParamWriter) HashMap(java.util.HashMap) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) MockUp(mockit.MockUp) Test(org.junit.Test)

Aggregations

QueryParameter (io.swagger.models.parameters.QueryParameter)88 Test (org.testng.annotations.Test)58 Parameter (io.swagger.models.parameters.Parameter)41 PathParameter (io.swagger.models.parameters.PathParameter)34 BodyParameter (io.swagger.models.parameters.BodyParameter)28 Swagger (io.swagger.models.Swagger)25 HeaderParameter (io.swagger.models.parameters.HeaderParameter)15 Operation (io.swagger.models.Operation)11 FormParameter (io.swagger.models.parameters.FormParameter)11 Test (org.junit.Test)11 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 StringProperty (io.swagger.models.properties.StringProperty)10 Path (io.swagger.models.Path)9 RefModel (io.swagger.models.RefModel)9 Type (java.lang.reflect.Type)9 BigDecimal (java.math.BigDecimal)9 HashMap (java.util.HashMap)9 RefProperty (io.swagger.models.properties.RefProperty)8 Response (io.swagger.models.Response)7 Method (java.lang.reflect.Method)7