Commit 2f6de5c1 by inrgihc

测试问题修复

parent beac5827
...@@ -23,18 +23,18 @@ public class ItemParam extends BaseParam { ...@@ -23,18 +23,18 @@ public class ItemParam extends BaseParam {
public void checkValid() { public void checkValid() {
if (StringUtils.isBlank(getName())) { if (StringUtils.isBlank(getName())) {
throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR, "parameter name must is not blank"); throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR, "input parameter name must is not blank");
} }
if (getType() == ParamTypeEnum.OBJECT) { if (getType() == ParamTypeEnum.OBJECT) {
if (null != children && children.size() > 0) { if (null != children && children.size() > 0) {
for (BaseParam param : children) { for (BaseParam param : children) {
if (StringUtils.isBlank(param.getName())) { if (StringUtils.isBlank(param.getName())) {
throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR, "parameter name must is not blank"); throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR, "input parameter name must is not blank");
} }
} }
} else { } else {
throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT, throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT,
"Object Input param '" + getName() + "' must have child parameter."); "Object input param '" + getName() + "' must have child parameter.");
} }
} else { } else {
children = Collections.emptyList(); children = Collections.emptyList();
......
package com.gitee.sqlrest.common.dto; package com.gitee.sqlrest.common.dto;
import com.gitee.sqlrest.common.enums.ParamTypeEnum; import com.gitee.sqlrest.common.enums.ParamTypeEnum;
import com.gitee.sqlrest.common.exception.CommonException;
import com.gitee.sqlrest.common.exception.ResponseErrorCode;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections;
import java.util.List; import java.util.List;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
...@@ -32,4 +36,29 @@ public class OutParam implements Serializable { ...@@ -32,4 +36,29 @@ public class OutParam implements Serializable {
@ApiModelProperty("Object类型的子元素") @ApiModelProperty("Object类型的子元素")
private List<OutParam> children; private List<OutParam> children;
public void checkValid() {
if (StringUtils.isBlank(getName())) {
throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR, "output parameter name must is not blank");
}
if (null == getType()) {
throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR, "output parameter type must is not empty");
}
if (getType() == ParamTypeEnum.OBJECT) {
if (null != children && children.size() > 0) {
for (OutParam param : children) {
if (StringUtils.isBlank(param.getName())) {
throw new CommonException(ResponseErrorCode.ERROR_INTERNAL_ERROR,
"output parameter name must is not blank");
}
}
} else {
throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT,
"Object output param '" + getName() + "' must have child parameter.");
}
} else {
children = Collections.emptyList();
}
}
} }
...@@ -51,6 +51,7 @@ import java.util.HashMap; ...@@ -51,6 +51,7 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier; import java.util.function.Supplier;
...@@ -213,10 +214,11 @@ public class ApiAssignmentService { ...@@ -213,10 +214,11 @@ public class ApiAssignmentService {
if (pv.getType().isObject() && null != pv.getChildren()) { if (pv.getType().isObject() && null != pv.getChildren()) {
Map<String, Object> objectMap = new HashMap<>(4); Map<String, Object> objectMap = new HashMap<>(4);
for (BaseParamValue spv : pv.getChildren()) { for (BaseParamValue spv : pv.getChildren()) {
Object v = spv.getType().getConverter().apply(spv.getValue());
if (spv.getIsArray()) { if (spv.getIsArray()) {
objectMap.put(spv.getName(), Arrays.asList(spv.getType().getConverter().apply(spv.getValue()))); objectMap.put(spv.getName(), null == v ? null : Arrays.asList(v));
} else { } else {
objectMap.put(spv.getName(), spv.getType().getConverter().apply(spv.getValue())); objectMap.put(spv.getName(), v);
} }
} }
if (objectMap.size() > 0) { if (objectMap.size() > 0) {
...@@ -230,6 +232,7 @@ public class ApiAssignmentService { ...@@ -230,6 +232,7 @@ public class ApiAssignmentService {
} else { } else {
List<Object> values = value.stream().map(ParamValue::getValue) List<Object> values = value.stream().map(ParamValue::getValue)
.map(s -> type.getConverter().apply(s)) .map(s -> type.getConverter().apply(s))
.filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
params.put(paramName, values); params.put(paramName, values);
} }
...@@ -238,15 +241,14 @@ public class ApiAssignmentService { ...@@ -238,15 +241,14 @@ public class ApiAssignmentService {
if (null != value.get(0).getChildren()) { if (null != value.get(0).getChildren()) {
Map<String, Object> objectMap = new HashMap<>(4); Map<String, Object> objectMap = new HashMap<>(4);
for (BaseParamValue spv : value.get(0).getChildren()) { for (BaseParamValue spv : value.get(0).getChildren()) {
Object v = spv.getType().getConverter().apply(spv.getValue());
if (spv.getIsArray()) { if (spv.getIsArray()) {
objectMap.put(spv.getName(), Arrays.asList(spv.getType().getConverter().apply(spv.getValue()))); objectMap.put(spv.getName(), null == v ? null : Arrays.asList(v));
} else { } else {
objectMap.put(spv.getName(), spv.getType().getConverter().apply(spv.getValue())); objectMap.put(spv.getName(), v);
} }
} }
if (objectMap.size() > 0) { params.put(paramName, objectMap);
params.put(paramName, objectMap);
}
} }
} else { } else {
params.put(paramName, type.getConverter().apply(value.get(0).getValue())); params.put(paramName, type.getConverter().apply(value.get(0).getValue()));
...@@ -336,6 +338,11 @@ public class ApiAssignmentService { ...@@ -336,6 +338,11 @@ public class ApiAssignmentService {
itemParam.checkValid(); itemParam.checkValid();
} }
} }
if (!CollectionUtils.isEmpty(request.getOutputs())) {
for (OutParam outParam : request.getOutputs()) {
outParam.checkValid();
}
}
if (null == request.getDatasourceId() || null == dataSourceDao.getById(request.getDatasourceId())) { if (null == request.getDatasourceId() || null == dataSourceDao.getById(request.getDatasourceId())) {
throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT, throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT,
"Invalid datasourceId or maybe not exist."); "Invalid datasourceId or maybe not exist.");
...@@ -409,6 +416,11 @@ public class ApiAssignmentService { ...@@ -409,6 +416,11 @@ public class ApiAssignmentService {
itemParam.checkValid(); itemParam.checkValid();
} }
} }
if (!CollectionUtils.isEmpty(request.getOutputs())) {
for (OutParam outParam : request.getOutputs()) {
outParam.checkValid();
}
}
if (null == request.getDatasourceId() || null == dataSourceDao.getById(request.getDatasourceId())) { if (null == request.getDatasourceId() || null == dataSourceDao.getById(request.getDatasourceId())) {
throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT, throw new CommonException(ResponseErrorCode.ERROR_INVALID_ARGUMENT,
"Invalid datasourceId or maybe not exist."); "Invalid datasourceId or maybe not exist.");
......
...@@ -58,6 +58,12 @@ public class XmlSqlTemplate { ...@@ -58,6 +58,12 @@ public class XmlSqlTemplate {
name = name.replaceAll(Pattern.quote(matcher.group()), ""); name = name.replaceAll(Pattern.quote(matcher.group()), "");
matcher = pattern.matcher(name); matcher = pattern.matcher(name);
} }
int commaIdx = name.indexOf(",");
if (commaIdx > 0) {
name = name.substring(0, commaIdx);
}
int idx = name.indexOf("."); int idx = name.indexOf(".");
if (idx > 0) { if (idx > 0) {
subName = name; subName = name;
......
<!ELEMENT script (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ELEMENT trim (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ATTLIST trim
prefix CDATA #IMPLIED
prefixOverrides CDATA #IMPLIED
suffix CDATA #IMPLIED
suffixOverrides CDATA #IMPLIED
>
<!ELEMENT where (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ELEMENT set (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ELEMENT foreach (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ATTLIST foreach
collection CDATA #REQUIRED
item CDATA #IMPLIED
index CDATA #IMPLIED
open CDATA #IMPLIED
close CDATA #IMPLIED
separator CDATA #IMPLIED
>
<!ELEMENT choose (when* , otherwise?)>
<!ELEMENT when (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ATTLIST when
test CDATA #REQUIRED
>
<!ELEMENT otherwise (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ELEMENT if (#PCDATA | trim | where | set | foreach | choose | if)*>
<!ATTLIST if
test CDATA #REQUIRED
>
\ No newline at end of file
...@@ -210,4 +210,23 @@ public class TemplateTest { ...@@ -210,4 +210,23 @@ public class TemplateTest {
SqlMeta sqlMeta = template.process(params); SqlMeta sqlMeta = template.process(params);
System.out.println(sqlMeta); System.out.println(sqlMeta);
} }
@Test
public void testObjNotRequiredField() {
String sql = "SELECT * from employees.departments "
+ "<if test='obj.deptNo != null and obj.deptNo.size() > 0'> "
+ " WHERE dept_no in "
+ " <foreach open=\"(\" close=\")\" collection=\"obj.deptNo\" separator=\",\" item=\"item\" index=\"index\"> "
+ " #{item} "
+ " </foreach> "
+ "</if>";
XmlSqlTemplate template = new XmlSqlTemplate(sql);
System.out.println(template.getParameterNames());
Map<String, Object> obj = new HashMap<>();
Map<String, Object> params = new HashMap<>();
params.put("obj", obj);
SqlMeta sqlMeta = template.process(params);
System.out.println(sqlMeta);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment