Skip to content
Draft
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
13 changes: 13 additions & 0 deletions .changesets/1793711888-00a6fda2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
id: 1793711888-00a6fda2
features:
- pagination
targets:
- csharp
- unity
- php
- ruby
type: fix
bump: patch
description: keep paginating on the result length when a response omits or nulls numPages
author: AshGodfrey
date: "2026-11-03"
1 change: 1 addition & 0 deletions internal/features/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ const (
TestPaginationLimitOffsetPageBody
TestPaginationLimitOffsetPageBodyNullable
TestPaginationLimitOffsetPageBodyOptionalNullable
TestPaginationLimitOffsetPageNumPagesMissing
TestPaginationLimitOffsetDeepOutputsPageBody
TestPaginationLimitOffsetPageParams
TestPaginationLimitOffsetPageParamsFlat
Expand Down
5 changes: 5 additions & 0 deletions internal/features/tests_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions services/speakeasy-api-test-service/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func main() {
r.HandleFunc("/pagination/cursor/deep_nested_outputs", pagination.HandleCursorDeepNestedOutputs).Methods(http.MethodGet)
r.HandleFunc("/pagination/limitoffset/nullable_offset", pagination.HandleLimitOffsetNullable).Methods(http.MethodGet)
r.HandleFunc("/pagination/limitoffset/nullable_body", pagination.HandleLimitOffsetPageNullableBody).Methods(http.MethodPut)
r.HandleFunc("/pagination/limitoffset/page/num_pages_missing", pagination.HandleLimitOffsetPageNumPagesMissing).Methods(http.MethodGet)
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet, http.MethodPost)
r.HandleFunc("/errors/errorUnion/malformed", errors.HandleMalformedErrorUnionResponse).Methods(http.MethodGet)
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet, http.MethodPost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,41 @@ func getNonNumericValue(queryValue string, hasBody bool, paginationValue string)
}
}
}

// HandleLimitOffsetPageNumPagesMissing serves three pages of seven items where
// the first response omits numPages and the second carries numPages: null, so
// only the final page states the total. Pagination must advance through the
// first two pages on the result length and stop after the third.
func HandleLimitOffsetPageNumPagesMissing(w http.ResponseWriter, r *http.Request) {
const limit = 7

page, err := strconv.Atoi(r.FormValue("page"))
if err != nil || page < 1 {
page = 1
}
if requested := r.FormValue("limit"); requested != "" && requested != strconv.Itoa(limit) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("limit must be 7"))
return
}

start := (page - 1) * limit
results := make([]interface{}, 0)
for i := start; i < total && len(results) < limit; i++ {
results = append(results, i)
}

res := map[string]interface{}{"resultArray": results}
switch page {
case 1:
case 2:
res["numPages"] = nil
default:
res["numPages"] = int(math.Ceil(float64(total) / float64(limit)))
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(res); err != nil {
w.WriteHeader(500)
}
}
1 change: 1 addition & 0 deletions templates/templates/cli/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ function isTestSkipped(test: string): boolean {
"unions-body-variant-overloads-nested-union",
"unions-body-variant-overloads-open-enum",
"request-bodies-base64-file-input-raw-bytes",
"pagination-limit-offset-page-num-pages-missing",
].includes(test);
}

Expand Down
7 changes: 7 additions & 0 deletions templates/templates/csharp/pagination.cs.stmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ Func<Task<{{ sanitizeType .Local.Response.Type false "" }}?>> nextFunc = async d

var numPages = body.SelectToken("{{ sanitizeJsonPath $pagination.Outputs.NumPages }}");

{{- if $pagination.Outputs.Results }}
if (numPages != null && (numPages.Type == JTokenType.Integer || numPages.Type == JTokenType.Float) && numPages.Value<long>() <= page)
{
return null;
}
{{- else }}
if (numPages == null || numPages.Value<long>() <= page)
{
return null;
}
{{- end }}

{{- end }}
{{- end }}
Expand Down
33 changes: 33 additions & 0 deletions templates/templates/csharp/tests/primary/PaginationShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,39 @@ public async Task PaginationLimitOffsetPageBodyNullable()
Assert.Null(nullRes);
}

[Fact]
public async Task PaginationLimitOffsetPageNumPagesMissing()
{
CommonHelpers.RecordTest("pagination-limit-offset-page-num-pages-missing");
var sdk = new SDK(serverUrl: Helpers.HttpBinUrl);

@cubic-dev-ai cubic-dev-ai Bot Nov 3, 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.

P2: This regression test does not verify that pagination avoids the fourth HTTP request: Assert.Null(nullRes) can pass after an unwanted page-4 fetch. Use PaginationRecorderClient and assert that exactly three requests were recorded.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At templates/templates/csharp/tests/primary/PaginationShould.cs, line 214:

<comment>This regression test does not verify that pagination avoids the fourth HTTP request: `Assert.Null(nullRes)` can pass after an unwanted page-4 fetch. Use `PaginationRecorderClient` and assert that exactly three requests were recorded.</comment>

<file context>
@@ -207,6 +207,39 @@ public async Task PaginationLimitOffsetPageBodyNullable()
+    public async Task PaginationLimitOffsetPageNumPagesMissing()
+    {
+        CommonHelpers.RecordTest("pagination-limit-offset-page-num-pages-missing");
+        var sdk = new SDK(serverUrl: Helpers.HttpBinUrl);
+
+        // the test service omits numPages on the first page and returns null on
</file context>
Fix with cubic


// the test service omits numPages on the first page and returns null on
// the second, so termination must fall back to the result length
var res = await sdk.Pagination.PaginationLimitOffsetPageNumPagesMissingAsync(page: 1, limit: 7);

Assert.Equal(HttpStatusCode.OK, res.HttpMeta.Response.StatusCode);
Assert.NotNull(res.Res);
Assert.False(res.Res.NumPages.IsSet);
Assert.Equal(new List<long> { 0, 1, 2, 3, 4, 5, 6 }, res.Res.ResultArray);

var nextRes = await res.Next!();
Assert.NotNull(nextRes);
Assert.Equal(HttpStatusCode.OK, nextRes.HttpMeta.Response.StatusCode);
Assert.NotNull(nextRes.Res);
Assert.True(nextRes.Res.NumPages.IsNull);
Assert.Equal(new List<long> { 7, 8, 9, 10, 11, 12, 13 }, nextRes.Res.ResultArray);

var lastRes = await nextRes.Next!();
Assert.NotNull(lastRes);
Assert.Equal(HttpStatusCode.OK, lastRes.HttpMeta.Response.StatusCode);
Assert.NotNull(lastRes.Res);
Assert.Equal(3L, lastRes.Res.NumPages.Value);
Assert.Equal(new List<long> { 14, 15, 16, 17, 18, 19 }, lastRes.Res.ResultArray);

var nullRes = await lastRes.Next!();
Assert.Null(nullRes);
}

[Fact]
public async Task PaginationLimitOffsetDeepOutputsPageBody()
{
Expand Down
1 change: 1 addition & 0 deletions templates/templates/go/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ function isTestSkipped(test: string): boolean {
"unions-body-variant-overloads-nested-union",
"unions-body-variant-overloads-open-enum",
"request-bodies-base64-file-input-raw-bytes",
"pagination-limit-offset-page-num-pages-missing",
].includes(test);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.openapis.openapi.models.operations.PaginationLimitOffsetOptionalPageParamsResponse;
import org.openapis.openapi.models.operations.PaginationLimitOffsetPageBodyNullableResponse;
import org.openapis.openapi.models.operations.PaginationLimitOffsetPageBodyResponse;
import org.openapis.openapi.models.operations.PaginationLimitOffsetPageNumPagesMissingResponse;
import org.openapis.openapi.models.operations.PaginationOffsetNullableResponse;
import org.openapis.openapi.models.operations.PaginationLimitOffsetPageParamsResponse;
import org.openapis.openapi.models.operations.PaginationURLParamsResponse;
Expand Down Expand Up @@ -154,6 +155,45 @@ void paginationLimitOffsetPageBodyNullable() throws Exception {
pages);
}

@Test
void paginationLimitOffsetPageNumPagesMissing() throws Exception {
recordTest("pagination-limit-offset-page-num-pages-missing");

SDK s = SDK.builder().serverURL(Helpers.HTTPBIN_URL).build();

// the test service omits numPages on the first page and returns null on
// the second, so termination must fall back to the result length
Iterator<PaginationLimitOffsetPageNumPagesMissingResponse> iterator = s.pagination()
.paginationLimitOffsetPageNumPagesMissing()
.page(1)
.limit(7L)
.callAsIterable().iterator();

assertTrue(iterator.hasNext());
PaginationLimitOffsetPageNumPagesMissingResponse res = iterator.next();
assertEquals(200, res.statusCode());
assertTrue(res.res().isPresent());
assertFalse(res.res().get().numPages().isPresent());
assertEquals(List.of(0L, 1L, 2L, 3L, 4L, 5L, 6L), res.res().get().resultArray());

assertTrue(iterator.hasNext());
PaginationLimitOffsetPageNumPagesMissingResponse nextRes = iterator.next();
assertEquals(200, nextRes.statusCode());
assertTrue(nextRes.res().isPresent());
assertTrue(nextRes.res().get().numPages().isPresent());
assertNull(nextRes.res().get().numPages().get());
assertEquals(List.of(7L, 8L, 9L, 10L, 11L, 12L, 13L), nextRes.res().get().resultArray());

assertTrue(iterator.hasNext());
PaginationLimitOffsetPageNumPagesMissingResponse lastRes = iterator.next();
assertEquals(200, lastRes.statusCode());
assertTrue(lastRes.res().isPresent());
assertEquals(3L, lastRes.res().get().numPages().get());
assertEquals(List.of(14L, 15L, 16L, 17L, 18L, 19L), lastRes.res().get().resultArray());

assertFalse(iterator.hasNext());
}

@Test
void paginationWrappedOptionalBody() throws Exception {
recordTest("pagination-wrapped-optional-body");
Expand Down
1 change: 1 addition & 0 deletions templates/templates/mcp-typescript/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ function isTestSkipped(test: string): boolean {
"unions-body-variant-overloads-nested-union",
"unions-body-variant-overloads-open-enum",
"request-bodies-base64-file-input-raw-bytes",
"pagination-limit-offset-page-num-pages-missing",
].includes(test);
}

Expand Down
33 changes: 33 additions & 0 deletions templates/templates/php/Tests/primary/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,39 @@ public function testPaginationLimitOffsetPageBodyNullable(): void
], $pages);
}

public function testPaginationLimitOffsetPageNumPagesMissing(): void
{
CommonHelpers::recordTest('pagination-limit-offset-page-num-pages-missing');

$sdk = \OpenAPI\OpenAPI\SDK::builder()->setServerUrl(Helpers::getHttpBinUrl())->build();

$this->assertInstanceOf(\OpenAPI\OpenAPI\SDK::class, $sdk);

// the test service omits numPages on the first page and returns null on
// the second, so termination must fall back to the result length
$responses = $sdk->pagination->paginationLimitOffsetPageNumPagesMissing(page: 1, limit: 7);

$pages = [];
$numPages = [];
$last = null;
foreach ($responses as $response) {
$this->assertEquals(200, $response->httpMeta->response->getStatusCode());
$this->assertNotNull($response->res);
$pages[] = $response->res->resultArray;
$numPages[] = $response->res->numPages;
$last = $response;
}

$this->assertEquals([
[0, 1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19],
], $pages);
$this->assertSame([null, null, 3], $numPages);
$this->assertNotNull($last);
$this->assertNull(($last->next)());
}

public function testPaginationLimitOffsetDeepOutputsPageBody(): void
{
CommonHelpers::recordTest('pagination-limit-offset-deep-outputs-page-body');
Expand Down
8 changes: 7 additions & 1 deletion templates/templates/php/includes/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,15 @@ function templatePagination(op: Operation): string {
op.Extensions.Pagination.Outputs.NumPages,
)}');\n`;
}
response += `${indent}if ($numPages == null || $numPages[0] <= $page) {
if (op.Extensions.Pagination.Outputs.Results) {
response += `${indent}if (is_array($numPages) && isset($numPages[0]) && is_numeric($numPages[0]) && $numPages[0] <= $page) {
return null;
}\n`;
} else {
response += `${indent}if ($numPages == null || $numPages[0] <= $page) {
return null;
}\n`;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions templates/templates/pythonv2/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ function isTestSkipped(test: string): boolean {
"unions-body-variant-overloads-request-body",
"unions-body-variant-overloads-nested-union",
"unions-body-variant-overloads-open-enum",
"pagination-limit-offset-page-num-pages-missing",
].includes(test);
}

Expand Down
8 changes: 7 additions & 1 deletion templates/templates/ruby/includes/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,15 @@ function templatePagination(op: Operation): string {
op.Extensions.Pagination.Outputs.NumPages,
)}', ::JSON.parse(response_data)).search\n`;
}
response += `${indent}if num_pages.nil? || num_pages[0] <= request_page
if (op.Extensions.Pagination.Outputs.Results) {
response += `${indent}if !num_pages.nil? && num_pages[0].is_a?(Numeric) && num_pages[0] <= request_page
next nil
end\n`;
} else {
response += `${indent}if num_pages.nil? || num_pages[0] <= request_page
next nil
end\n`;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,39 @@ def test_pagination_limit_offset_page_body_nullable
)
end

def test_pagination_limit_offset_page_num_pages_missing
record_test("pagination-limit-offset-page-num-pages-missing")

assert_instance_of(OpenApiSDK::SDK, @sdk)

# the test service omits numPages on the first page and returns null on
# the second, so termination must fall back to the result length
responses = @sdk
.pagination
.pagination_limit_offset_page_num_pages_missing(page: 1, limit: 7)

pages = []
num_pages = []
last = nil
responses.each do |response|
assert(response)
assert(response.http_meta)
assert(response.http_meta.response)
assert_equal(200, response.http_meta.response.status)
assert(response.res)
pages << response.res.result_array
num_pages << response.res.num_pages
last = response
end

assert_equal(
[[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19]],
pages
)
assert_equal([nil, nil, 3], num_pages)
assert_nil(T.must(last).next_page.call)
end

def test_pagination_limit_offset_deep_outputs_page_body
record_test("pagination-limit-offset-deep-outputs-page-body")

Expand Down
1 change: 1 addition & 0 deletions templates/templates/typescriptv2/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ function isTestSkipped(test: string): boolean {
"unions-body-variant-overloads-nested-union",
"unions-body-variant-overloads-open-enum",
"request-bodies-base64-file-input-raw-bytes",
"pagination-limit-offset-page-num-pages-missing",
].includes(test);
}

Expand Down
7 changes: 7 additions & 0 deletions templates/templates/unity/pagination.cs.stmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@

var numPages = body.SelectToken("{{ sanitizeJsonPath $pagination.Outputs.NumPages }}");

{{- if $pagination.Outputs.Results }}
if (numPages != null && (numPages.Type == JTokenType.Integer || numPages.Type == JTokenType.Float) && numPages.Value<long>() <= page)
{
return null;
}
{{- else }}
if (numPages == null || numPages.Value<long>() <= page)
{
return null;
}
{{- end }}

{{- end }}
{{- end }}
Expand Down
Loading
Loading