Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1816,12 +1816,22 @@ void testSearchWithVeryLargeSize(TestNamespace ns) throws Exception {
void testSearchWithNegativeOffset(TestNamespace ns) throws Exception {
OpenMetadataClient client = SdkClients.adminClient();

// Negative offset is invalid - the search engine rejects it. That rejection is the caller's
// error, so it now arrives as a 400 rather than the 500 the engine's status used to be
// flattened into (#27990).
assertThrows(
InvalidRequestException.class,
() -> client.search().query("*").index("table_search_index").from(-1).size(10).execute());
// The engine would also answer 400 (#27990); the message shows the request never reached it.
InvalidRequestException thrown =
assertThrows(
InvalidRequestException.class,
() ->
client
.search()
.query("*")
.index("table_search_index")
.from(-25)
.size(25)
.execute());

assertTrue(
thrown.getMessage().contains("query param from must be greater than or equal to 0"),
thrown.getMessage());
}

@Test
Expand Down Expand Up @@ -2144,4 +2154,48 @@ void testValidQueryFilterStillApplies() throws Exception {

assertEquals(200, response.statusCode(), response.body());
}

@ParameterizedTest
@ValueSource(
strings = {
"/v1/search/query?q=*&index=table_search_index&size=-1",
"/v1/search/nlq/query?q=tables&index=table_search_index&from=-25",
"/v1/search/fieldQuery?fieldName=name&fieldValue=orders&index=table_search_index&from=-25",
"/v1/search/fieldQuery?fieldName=name&fieldValue=orders&index=table_search_index&size=-1"
})
void testNegativePaginationIsRejectedAsBadRequest(String path) throws Exception {
HttpResponse<String> response = httpGetSearch(path);

assertEquals(400, response.statusCode(), path + " returned " + response.body());
assertTrue(
response.body().contains("must be greater than or equal to 0"),
path + " returned " + response.body());
}

@Test
void testPreviewSearchWithNegativeOffsetIsRejectedAsBadRequest() throws Exception {
String previewRequest =
"""
{"query": "*", "index": "table", "from": -25, "size": 25, "searchSettings": {}}
""";

HttpResponse<String> response = httpPostJson("/v1/search/preview", previewRequest);

assertEquals(400, response.statusCode(), response.body());
assertTrue(
response.body().contains("from must be greater than or equal to 0"), response.body());
}

private HttpResponse<String> httpPostJson(String path, String jsonBody) throws Exception {
HttpRequest request =
HttpRequest.newBuilder()
.uri(URI.create(SdkClients.getServerUrl() + path))
.header("Authorization", "Bearer " + SdkClients.getAdminToken())
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.timeout(Duration.ofSeconds(30))
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
return HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.DefaultValue;
Expand Down Expand Up @@ -105,6 +107,7 @@
@Collection(name = "search")
@LatencyPhase
public class SearchResource {
private static final String MUST_BE_NON_NEGATIVE = "must be greater than or equal to 0";

@gitar-bot gitar-bot Bot Oct 9, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Quality: Reject-not-clamp pagination rule lives only in the PR description

This PR sets a new API contract for /v1/search/query, /nlq/query, /fieldQuery and /preview: a negative from/size is now rejected with a 400. Clients and the UI have to agree with that contract, which makes it a decision-record trigger. The reasoning is only in the description: "Negative values are rejected, not clamped to 0. Clamping would hide the caller's paging bug and serve page 1 to a client that asked for page 0." The description also says /v1/search/export and /export/async deliberately keep ignoring from <= 0. Neither rule is written down anywhere in the tree, and nothing in docs/decisions/ covers search pagination. The repo squash-merges, so the next contributor can't see why. They could "helpfully" clamp instead, or make export consistent and break it, and nothing in the tree would stop them. Per ADR:2026-10-08-a-decision-is-recorded-in-the-tree-not-in-the-pr-body, add a record. Suggested wording: "Search endpoints that paginate reject a negative from or size with 400 at the resource boundary and never clamp it to 0, and size=0 stays valid for count-only queries. The export endpoints are the exception: they ignore from <= 0." If you think no lasting rule survives this change, say so and this can be closed.

Add a decision record stating the pagination rule:

# docs/decisions/2026-10-xx-search-pagination-rejects-negative-values.md
- **Status:** Accepted
## Decision
Search endpoints that paginate (`/v1/search/query`, `/nlq/query`, `/fieldQuery`, `/preview`)
reject a negative `from` or `size` with 400 before the cache or engine is touched; they never
clamp to 0, because clamping hides the caller's paging bug. `size=0` stays valid (count-only).
`/v1/search/export*` ignore `from <= 0`.

Was this helpful? React with 👍 / 👎

private final SearchRepository searchRepository;
private final Authorizer authorizer;

Expand Down Expand Up @@ -164,10 +167,12 @@ public Response search(
Boolean deleted,
@Parameter(description = "From field to paginate the results, defaults to 0")
@DefaultValue("0")
@Min(value = 0, message = MUST_BE_NON_NEGATIVE)
@QueryParam("from")
int from,
@Parameter(description = "Size field to limit the no.of results returned, defaults to 10")
@DefaultValue("10")
@Min(value = 0, message = MUST_BE_NON_NEGATIVE)
@QueryParam("size")
int size,
@Parameter(
Expand Down Expand Up @@ -546,6 +551,8 @@ public Response previewSearch(
@RequestBody(description = "Preview request containing search settings", required = true)
PreviewSearchRequest previewRequest)
throws IOException {
requireNonNegative("from", previewRequest.getFrom());
requireNonNegative("size", previewRequest.getSize());

SubjectContext subjectContext = getSubjectContext(securityContext);

Expand All @@ -569,6 +576,12 @@ public Response previewSearch(
searchRequest, subjectContext, previewRequest.getSearchSettings());
}

private static void requireNonNegative(String fieldName, Integer value) {
if (value != null && value < 0) {
throw new BadRequestException(String.format("%s %s", fieldName, MUST_BE_NON_NEGATIVE));
}
}

@GET
@Path("/nlq/query")
@Operation(
Expand Down Expand Up @@ -600,10 +613,12 @@ public Response searchWithNLQ(
boolean deleted,
@Parameter(description = "From field to paginate the results, defaults to 0")
@DefaultValue("0")
@Min(value = 0, message = MUST_BE_NON_NEGATIVE)
@QueryParam("from")
int from,
@Parameter(description = "Size field to limit the no.of results returned, defaults to 10")
@DefaultValue("10")
@Min(value = 0, message = MUST_BE_NON_NEGATIVE)
@QueryParam("size")
int size,
@Parameter(
Expand Down Expand Up @@ -735,10 +750,12 @@ public Response searchByField(
boolean deleted,
@Parameter(description = "From field to paginate the results, defaults to 0")
@DefaultValue("0")
@Min(value = 0, message = MUST_BE_NON_NEGATIVE)
@QueryParam("from")
int from,
@Parameter(description = "Size field to limit the no.of results returned, defaults to 10")
@DefaultValue("10")
@Min(value = 0, message = MUST_BE_NON_NEGATIVE)
@QueryParam("size")
int size)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.openmetadata.service.resources.search;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verifyNoInteractions;

import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.core.SecurityContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.MockedStatic;
import org.openmetadata.schema.search.PreviewSearchRequest;
import org.openmetadata.service.Entity;
import org.openmetadata.service.search.SearchRepository;
import org.openmetadata.service.security.Authorizer;

class SearchResourceTest {

@ParameterizedTest
@CsvSource({"-25, 25, from", "0, -1, size", ", -1, size"})
void previewSearchRejectsNegativePaginationWithoutSearching(
Integer from, Integer size, String rejectedField) {
SearchRepository searchRepository = mock(SearchRepository.class);
try (MockedStatic<Entity> entity = mockStatic(Entity.class)) {
entity.when(Entity::getSearchRepository).thenReturn(searchRepository);
SearchResource resource = new SearchResource(mock(Authorizer.class));
PreviewSearchRequest request =
new PreviewSearchRequest()
.withQuery("*")
.withIndex("table")
.withFrom(from)
.withSize(size);

BadRequestException error =
assertThrows(
BadRequestException.class,
() -> resource.previewSearch(mock(SecurityContext.class), request));

assertEquals(rejectedField + " must be greater than or equal to 0", error.getMessage());
verifyNoInteractions(searchRepository);
}
}
}
Loading