apache / apache/shenyu

[BUG] Mock request lookup by apiId returns an arbitrary record when duplicates exist

Open
#6,521 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
8.8k
Forks
3.1k
Avg merge
7d 1h
Merged PRs (30d)
85

Description

### Search before asking

- [x] I had searched in the [issues](https://github.com/apache/shenyu/issues) and found no similar issues.

### Apache ShenYu Component

shenyu-admin

### What happened

`MockRequestRecordServiceImpl.queryByApiId(...)` returns the first row from `selectByQuery(...)`:

```java
MockRequestRecordQuery mockRequestRecordQuery = new MockRequestRecordQuery();
mockRequestRecordQuery.setApiId(apiId);
List mockRequestRecordDOList = mockRequestRecordMapper.selectByQuery(mockRequestRecordQuery);
return mockRequestRecordDOList.isEmpty()
? new MockRequestRecordVO()
: MockRequestRecordVO.buildMockRequestRecordVO(mockRequestRecordDOList.get(0));
```

But the mapper query has no `ORDER BY`:

```xml

SELECT ...
FROM mock_request_record


and api_id = #{apiId, jdbcType=VARCHAR}

...

```

and the table schema only defines `id` as the primary key. There is no uniqueness constraint on `api_id`:

```sql
CREATE TABLE `mock_request_record` (
`id` varchar(128) NOT NULL,
`api_id` varchar(128) NOT NULL,
...
PRIMARY KEY (`id`)
)
```

If multiple mock request records exist for the same `apiId`, the returned record depends on the database execution plan and can change unpredictably.

### Expected behavior

`queryByApiId(...)` should be deterministic. It should either enforce a unique mock request record per `apiId`, add a deterministic ordering and selection rule, or return all matching records instead of silently picking the first unordered row.

### How to reproduce

1. Insert or create two `mock_request_record` rows with the same `api_id` and different request data.
2. Call the admin API path that uses `MockRequestRecordServiceImpl.queryByApiId(apiId)`.
3. The service returns `selectByQuery(...).get(0)` without any SQL ordering or uniqueness guarantee.
4. Which mock record is returned is not stable across database plans/storage state.

### Debug logs

_No response_

### Environment

Current `master` branch.

### Are you willing to submit a PR?

- [ ] Yes I am willing to submit a PR!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at MockRequestRecordServiceImpl.queryByApiId(...) and the mapper's selectByQuery XML, then reproduce the behavior with duplicate api_id rows. The issue presents several possible fixes but does not select one; done requires an agreed deterministic contract and a regression test proving duplicate lookups no longer return an arbitrary record.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, sql
Domain
api, backend, database
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.