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