Skip to content

add pagination to tags and text_search endpoints#760

Open
akshat4703 wants to merge 1 commit intoOWASP:mainfrom
akshat4703:akshat/feat-tags-text-search-pagination
Open

add pagination to tags and text_search endpoints#760
akshat4703 wants to merge 1 commit intoOWASP:mainfrom
akshat4703:akshat/feat-tags-text-search-pagination

Conversation

@akshat4703
Copy link

@akshat4703 akshat4703 commented Feb 26, 2026

Summary

  • Adds pagination and response-format parity to /rest/v1/tags and /rest/v1/text_search so large result sets can be consumed safely and consistently.
  • These endpoints previously returned full result sets in one response, which can become heavy for clients and backend resources. This change improves scalability and keeps behavior consistent across output formats.

Changes

  1. Added pagination support (page, items_per_page) to:
  • /rest/v1/tags
  • /rest/v1/text_search
  1. Introduced shared pagination helpers in the API layer.

  2. Applied pagination consistently before rendering all supported formats:

  • JSON
  • Markdown
  • CSV
  • OSCAL
  1. Added out-of-range page handling (404).

  2. Enhanced /rest/v1/tags JSON response with pagination metadata:

  • page
  • total_pages
  1. Updated OpenAPI docs to include new query parameters for both endpoints.

  2. Added regression tests for:

  • pagination behavior
  • format parity across paged responses
  • out-of-range requests

Impact

  • Reduces payload size and improves response performance on large queries.
  • Prevents client-side overfetching and timeout-prone calls.
  • Makes paged results consistent regardless of selected output format.

Validation

  • conda run -n opencre python -m unittest application.tests.web_main_test -v ✅
  • conda run -n opencre python -m unittest application.tests.cwe_parser_test -v ✅

Copilot AI review requested due to automatic review settings February 26, 2026 06:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds pagination support to the /rest/v1/tags and /rest/v1/text_search endpoints to handle large result sets more efficiently. The implementation introduces shared pagination helper functions that work consistently across different response formats (JSON, Markdown, CSV, OSCAL).

Changes:

  • Added _parse_positive_int and _paginate_documents helper functions for consistent pagination logic
  • Implemented pagination in /rest/v1/tags endpoint with page and total_pages metadata in JSON responses
  • Implemented pagination in /rest/v1/text_search endpoint (though JSON response lacks pagination metadata)
  • Added comprehensive tests for pagination behavior and format parity

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
application/web/web_main.py Added pagination helper functions and applied them to tags and text_search endpoints across all output formats
application/tests/web_main_test.py Added comprehensive tests for pagination behavior, format parity, and out-of-range page handling for both endpoints
docs/api/openapi.yaml Updated OpenAPI spec to document pagination parameters (though added to incorrect endpoints)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +68 to +81
- name: page
in: query
required: false
description: 1-based page number for paginated results.
schema:
type: integer
minimum: 1
- name: items_per_page
in: query
required: false
description: Number of items per page.
schema:
type: integer
minimum: 1
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The pagination parameters were added to the wrong endpoint. This PR adds pagination to /rest/v1/name/{crename}, but this endpoint returns a single CRE (see web_main.py lines 148-152 where it expects at most one result). Pagination doesn't make sense for this endpoint. Instead, these pagination parameters should be added to /rest/v1/tags and /rest/v1/text_search endpoints, which do implement pagination in their code but are missing the OpenAPI documentation for these parameters.

Suggested change
- name: page
in: query
required: false
description: 1-based page number for paginated results.
schema:
type: integer
minimum: 1
- name: items_per_page
in: query
required: false
description: Number of items per page.
schema:
type: integer
minimum: 1

Copilot uses AI. Check for mistakes.
Comment on lines +448 to 460
_, _, paged_documents = _paginate_documents(documents)
if opt_format == SupportedFormats.Markdown.value:
return f"<pre>{mdutils.cre_to_md(documents)}</pre>"
return f"<pre>{mdutils.cre_to_md(paged_documents)}</pre>"
elif opt_format == SupportedFormats.CSV.value:
docs = sheet_utils.ExportSheet().prepare_spreadsheet(
docs=documents, storage=database
docs=paged_documents, storage=database
)
return write_csv(docs=docs).getvalue().encode("utf-8")
elif opt_format == SupportedFormats.OSCAL.value:
return jsonify(json.loads(oscal_utils.list_to_oscal(documents)))
return jsonify(json.loads(oscal_utils.list_to_oscal(paged_documents)))

res = [doc.todict() for doc in documents]
res = [doc.todict() for doc in paged_documents]
return jsonify(res)
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

The text_search endpoint is missing pagination metadata (page and total_pages) in its JSON response. The tags endpoint returns {"data": [...], "page": X, "total_pages": Y}, but text_search only returns the raw array. This creates an inconsistency between the two endpoints that were both updated to support pagination in this PR. The JSON response should include page and total_pages fields for consistency.

Copilot uses AI. Check for mistakes.
Comment on lines +126 to +155
- name: page
in: query
required: false
schema:
type: integer
- name: items_per_page
in: query
required: false
schema:
type: integer
- name: format
in: query
required: false
schema:
type: string
enum: [json, md, csv, oscal]
- name: page
in: query
required: false
description: 1-based page number for paginated results.
schema:
type: integer
minimum: 1
- name: items_per_page
in: query
required: false
description: Number of items per page.
schema:
type: integer
minimum: 1
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

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

Duplicate pagination parameters defined for this endpoint. The page and items_per_page parameters are defined twice: first at lines 126-135 (without descriptions) and again at lines 142-155 (with descriptions). Remove the first occurrence (lines 126-135) and keep only the detailed definitions added in this PR.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants