Search in sources :

Example 21 with PathParameter

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

the class TestPath method testUrlPathBuilder.

@Test
public void testUrlPathBuilder() throws Exception {
    Map<String, RestParam> paramMap = new HashMap<>();
    Parameter pathParameter = new PathParameter();
    pathParameter.setName("id");
    RestParam oRestParam = new RestParam(0, pathParameter, int.class);
    paramMap.put(oRestParam.getParamName(), oRestParam);
    Parameter queryParameter = new QueryParameter();
    queryParameter.setName("q");
    oRestParam = new RestParam(1, queryParameter, String.class);
    paramMap.put(oRestParam.getParamName(), oRestParam);
    URLPathBuilder oURLPathBuilder = new URLPathBuilder("/root/{id}", paramMap);
    Object[] args = new Object[] { 100, "query" };
    Assert.assertEquals("/root/100?q=query", oURLPathBuilder.createRequestPath(args));
    Assert.assertEquals("/root/100", oURLPathBuilder.createPathString(args));
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) HashMap(java.util.HashMap) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) URLPathBuilder(org.apache.servicecomb.common.rest.definition.path.URLPathBuilder) PathParameter(io.swagger.models.parameters.PathParameter) Test(org.junit.Test)

Example 22 with PathParameter

use of io.swagger.models.parameters.PathParameter 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 23 with PathParameter

use of io.swagger.models.parameters.PathParameter 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(ResourceNode resource, OperationAdaptor operationAdaptor) {
    if (!"get".equalsIgnoreCase(operationAdaptor.getHttpOperation())) {
        // Creating request body - required.
        ModelImpl messageModel = new ModelImpl();
        messageModel.setType("object");
        Map<String, Model> definitions = new HashMap<>();
        if (!definitions.containsKey("Request")) {
            definitions.put("Request", messageModel);
            this.swaggerDefinition.setDefinitions(definitions);
        }
        // Creating "Request rq" parameter
        BodyParameter messageParameter = new BodyParameter();
        messageParameter.setName(resource.getParameters().get(0).getName().getValue());
        RefModel refModel = new RefModel();
        refModel.setReference("Request");
        messageParameter.setSchema(refModel);
        operationAdaptor.getOperation().addParameter(messageParameter);
    }
    for (int i = 2; i < resource.getParameters().size(); i++) {
        VariableNode parameterDef = resource.getParameters().get(i);
        String typeName = parameterDef.getTypeNode().toString().toLowerCase(Locale.getDefault());
        PathParameter pathParameter = new PathParameter();
        // Set in value
        pathParameter.setIn("path");
        // Set parameter name
        String parameterName = parameterDef.getName().getValue();
        pathParameter.setName(parameterName);
        // Set type
        if (typeName.contains("[]")) {
            pathParameter.setType("array");
            switch(typeName.replace("[]", "").trim()) {
                case "string":
                    pathParameter.items(new StringProperty());
                    break;
                case "int":
                    pathParameter.items(new IntegerProperty());
                    break;
                case "boolean":
                    pathParameter.items(new BooleanProperty());
                    break;
                default:
                    break;
            }
        } else if ("int".equals(typeName)) {
            pathParameter.setType("integer");
        } else {
            pathParameter.setType(typeName);
        }
        // Note: 'format' to be added using annotations, hence skipped here.
        operationAdaptor.getOperation().addParameter(pathParameter);
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) BooleanProperty(io.swagger.models.properties.BooleanProperty) StringProperty(io.swagger.models.properties.StringProperty) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) VariableNode(org.ballerinalang.model.tree.VariableNode) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ModelImpl(io.swagger.models.ModelImpl)

Example 24 with PathParameter

use of io.swagger.models.parameters.PathParameter 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 25 with PathParameter

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

the class TestClassUtils method isRawJsonType.

@Test
public void isRawJsonType() {
    PathParameter param = new PathParameter();
    Assert.assertFalse(SwaggerUtils.isRawJsonType(param));
    param.setVendorExtension(SwaggerConst.EXT_RAW_JSON_TYPE, Boolean.FALSE);
    Assert.assertFalse(SwaggerUtils.isRawJsonType(param));
    param.setVendorExtension(SwaggerConst.EXT_RAW_JSON_TYPE, Boolean.TRUE);
    Assert.assertTrue(SwaggerUtils.isRawJsonType(param));
}
Also used : PathParameter(io.swagger.models.parameters.PathParameter) Test(org.junit.Test)

Aggregations

PathParameter (io.swagger.models.parameters.PathParameter)51 QueryParameter (io.swagger.models.parameters.QueryParameter)35 Parameter (io.swagger.models.parameters.Parameter)32 Test (org.testng.annotations.Test)32 BodyParameter (io.swagger.models.parameters.BodyParameter)26 HeaderParameter (io.swagger.models.parameters.HeaderParameter)16 Swagger (io.swagger.models.Swagger)14 StringProperty (io.swagger.models.properties.StringProperty)12 FormParameter (io.swagger.models.parameters.FormParameter)11 Operation (io.swagger.models.Operation)10 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 Response (io.swagger.models.Response)8 IntegerProperty (io.swagger.models.properties.IntegerProperty)8 RefProperty (io.swagger.models.properties.RefProperty)8 Path (io.swagger.models.Path)6 ArrayProperty (io.swagger.models.properties.ArrayProperty)6 HashMap (java.util.HashMap)6 ModelImpl (io.swagger.models.ModelImpl)5 RefModel (io.swagger.models.RefModel)5 ArrayList (java.util.ArrayList)5