use of org.ballerinalang.util.codegen.ParamAnnAttachmentInfo 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);
}
}
}
}
}
}
Aggregations