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);
}
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);
}
}
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);
}
}
}
}
}
}
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);
}
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);
}
Aggregations