F2-17根因: 列表搜索流分页修复(page/perPage→引擎 PAGE_START/PAGE_SIZE)

FlowController 把 amis page/perPage 原样当过滤条件,而合成搜索流 listSelect
认的是 PAGE_START/PAGE_SIZE,导致永远返回全表(实测 page1=page2,total=266)。
翻译为引擎分页键并剔除原始键;过滤与分页共存(TITLE=放假 266→42 实测)。14 单测。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-12 09:38:39 +08:00
parent 830ea8fa19
commit f5850a884d
2 changed files with 112 additions and 0 deletions

View File

@@ -62,12 +62,16 @@ public class FlowController {
this.flowNameResolver = flowNameResolver; this.flowNameResolver = flowNameResolver;
} }
/** amis crud 默认每页条数(与前端 amis 默认 perPage 一致)。 */
private static final int DEFAULT_PER_PAGE = 10;
/** GET 执行:query 参数透传为服务流入参 map(保留原始大小写键,交由流脚本/服务节点消费)。 */ /** GET 执行:query 参数透传为服务流入参 map(保留原始大小写键,交由流脚本/服务节点消费)。 */
@GetMapping("/{id}") @GetMapping("/{id}")
@Operation(summary = "执行服务流(GET):query 参数作为入参 map,返回流输出 JSON。报表 D 类查询入口") @Operation(summary = "执行服务流(GET):query 参数作为入参 map,返回流输出 JSON。报表 D 类查询入口")
public ResponseEntity<Map<String, Object>> executeGet(@PathVariable String id, public ResponseEntity<Map<String, Object>> executeGet(@PathVariable String id,
@RequestParam Map<String, String> allParams) { @RequestParam Map<String, String> allParams) {
Map<String, Object> map = new HashMap<>(allParams); Map<String, Object> map = new HashMap<>(allParams);
applyPaging(map);
return run(id, map); return run(id, map);
} }
@@ -77,9 +81,45 @@ public class FlowController {
public ResponseEntity<Map<String, Object>> executePost(@PathVariable String id, public ResponseEntity<Map<String, Object>> executePost(@PathVariable String id,
@RequestBody(required = false) Map<String, Object> body) { @RequestBody(required = false) Map<String, Object> body) {
Map<String, Object> map = body == null ? new HashMap<>() : new HashMap<>(body); Map<String, Object> map = body == null ? new HashMap<>() : new HashMap<>(body);
applyPaging(map);
return run(id, map); return run(id, map);
} }
/**
* 把 amis crud 的字面分页参数 {@code page}/{@code perPage} 翻译成合成搜索流引擎
* ({@code POJOCRUDService.listSelect})认得的分页键 {@link POJOCRUDService#PAGE_START}/
* {@link POJOCRUDService#PAGE_SIZE},并从入参 map 中剔除原始 {@code page}/{@code perPage}
* (否则被引擎当作不存在的列名拼进 where,命名/报表流亦同——剔除即可,无副作用)。
*
* <p>缺省:page=1,perPage={@value #DEFAULT_PER_PAGE}(amis 默认)。键类型与 PickerController
* 一致放 int({@code (page-1)*perPage} 与 {@code perPage})。其它 query 参数(如 TITLE)原样保留为过滤条件。
* 仅当显式带 page/perPage 时才翻译,避免给无分页语义的流凭空注入分页键。
*/
private void applyPaging(Map<String, Object> map) {
boolean hasPage = map.containsKey("page");
boolean hasPerPage = map.containsKey("perPage");
if (!hasPage && !hasPerPage) {
return;
}
int page = parsePositiveInt(map.remove("page"), 1);
int perPage = parsePositiveInt(map.remove("perPage"), DEFAULT_PER_PAGE);
map.put(POJOCRUDService.PAGE_START, (page - 1) * perPage);
map.put(POJOCRUDService.PAGE_SIZE, perPage);
}
/** 解析分页参数为正整数;缺省/空白/非法/非正 → 回退默认值(page/perPage 至少为 1)。 */
private static int parsePositiveInt(Object raw, int fallback) {
if (raw == null) {
return fallback;
}
try {
int v = Integer.parseInt(String.valueOf(raw).trim());
return v > 0 ? v : fallback;
} catch (NumberFormatException e) {
return fallback;
}
}
/** /**
* 统一执行 + 响应封装(F2-14 三级解析)。 * 统一执行 + 响应封装(F2-14 三级解析)。
* *

View File

@@ -260,4 +260,76 @@ class FlowControllerTest {
assertEquals(1, resp.getBody().get("status")); assertEquals(1, resp.getBody().get("status"));
assertNotNull(resp.getBody().get("msg")); assertNotNull(resp.getBody().get("msg"));
} }
@Test
@DisplayName("12. 分页翻译: page/perPage → PAGE_START/PAGE_SIZE,剔除原始键,过滤键保留")
void translatesPagingForSynthesizedSearch() {
String flowId = "PM_PROJECT_SEARCH";
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
FlowController controller = new FlowController(engine, resolverReturning(flowId, synthesized(flowId)));
Map<String, String> params = new HashMap<>();
params.put("page", "2");
params.put("perPage", "2");
params.put("TITLE", "放假"); // F2-17 #3 过滤行为不可破坏
controller.executeGet(flowId, params);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId));
Map<String, Object> sent = mapCap.getValue();
// 引擎认的分页键存在且为 int(与 PickerController 一致),PAGE_START=(2-1)*2=2,PAGE_SIZE=2
assertEquals(2, sent.get(POJOCRUDService.PAGE_START), "PAGE_START=(page-1)*perPage");
assertEquals(2, sent.get(POJOCRUDService.PAGE_SIZE), "PAGE_SIZE=perPage");
assertInstanceOf(Integer.class, sent.get(POJOCRUDService.PAGE_START), "分页键须为 int");
assertInstanceOf(Integer.class, sent.get(POJOCRUDService.PAGE_SIZE), "分页键须为 int");
// 原始字面键已剔除,避免被引擎当不存在的列名条件
org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey("page"), "原始 page 应剔除");
org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey("perPage"), "原始 perPage 应剔除");
// 过滤键保留
assertEquals("放假", sent.get("TITLE"), "其它 query 参数仍作过滤条件保留");
}
@Test
@DisplayName("13. 分页缺省: 只给 page,perPage 回落 amis 默认 10;page 缺省 1")
void pagingDefaults() {
String flowId = "PM_PROJECT_SEARCH";
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
FlowController controller = new FlowController(engine, resolverReturning(flowId, synthesized(flowId)));
Map<String, String> params = new HashMap<>();
params.put("perPage", "5"); // 只给 perPage,page 缺省 1
controller.executeGet(flowId, params);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId));
Map<String, Object> sent = mapCap.getValue();
assertEquals(0, sent.get(POJOCRUDService.PAGE_START), "page 缺省 1 → PAGE_START=0");
assertEquals(5, sent.get(POJOCRUDService.PAGE_SIZE), "perPage=5");
}
@Test
@DisplayName("14. 无分页参数: 不注入分页键(命名/报表流无分页语义时不凭空加键)")
void noPagingKeysWhenAbsent() {
String flowId = "COMMON_REPORT";
FlowExcuteService engine = mock(FlowExcuteService.class);
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
FlowController controller = new FlowController(engine, resolverReturning(flowId, namedHit()));
Map<String, String> params = new HashMap<>();
params.put("DEPT", "财务");
controller.executeGet(flowId, params);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId));
Map<String, Object> sent = mapCap.getValue();
org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey(POJOCRUDService.PAGE_START));
org.junit.jupiter.api.Assertions.assertFalse(sent.containsKey(POJOCRUDService.PAGE_SIZE));
assertEquals("财务", sent.get("DEPT"), "过滤键保留");
}
} }