Skip to content

HYPERFLEET-1274 - refactor: move list parameter parsing to handler layer#307

Open
kuudori wants to merge 2 commits into
openshift-hyperfleet:mainfrom
kuudori:HYPERFLEET-1274
Open

HYPERFLEET-1274 - refactor: move list parameter parsing to handler layer#307
kuudori wants to merge 2 commits into
openshift-hyperfleet:mainfrom
kuudori:HYPERFLEET-1274

Conversation

@kuudori

@kuudori kuudori commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Extract query parameter parsing from pkg/services/types.go into pkg/handlers/list_params.go, aligning with the handler-validates, service-executes pattern
  • Use go-playground/validator for struct-level validation with structured error details via ValidationWithDetails
  • Remove redundant defensive size checks from the service layer (generic.go) while keeping a Size <= 0 guard
  • Add comprehensive table-driven tests covering defaults, custom values, format errors, range errors, ref pairing, and edge cases

Test plan

  • make verify-all passes (vet + lint + unit tests)
  • make test-integration passes
  • Verify list endpoints return correct pagination defaults (page=1, size=20, order=created_time desc)
  • Verify validation errors return 400 with structured details array
  • Verify ?fields=name response includes id field automatically
  • Verify ?ref_type=dep without ?ref_target_id returns validation error

@openshift-ci
openshift-ci Bot requested review from ciaranRoche and mliptak0 July 21, 2026 20:50
@openshift-ci

openshift-ci Bot commented Jul 21, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign kuudori for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 382fa03c-56bd-4775-81f1-1a305076867f

📥 Commits

Reviewing files that changed from the base of the PR and between 47e3fa1 and ece2403.

📒 Files selected for processing (7)
  • pkg/api/metadata_types.go
  • pkg/handlers/list_params.go
  • pkg/handlers/list_params_test.go
  • pkg/services/adapter_status.go
  • pkg/services/generic.go
  • pkg/services/generic_test.go
  • pkg/services/types.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • openshift-hyperfleet/architecture (manual)
  • openshift-hyperfleet/hyperfleet-api (manual)
  • openshift-hyperfleet/hyperfleet-sentinel (manual)
  • openshift-hyperfleet/hyperfleet-adapter (manual)
  • openshift-hyperfleet/hyperfleet-broker (manual)
🚧 Files skipped from review as they are similar to previous changes (6)
  • pkg/services/adapter_status.go
  • pkg/services/generic_test.go
  • pkg/handlers/list_params_test.go
  • pkg/handlers/list_params.go
  • pkg/services/types.go
  • pkg/services/generic.go

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Added consistent list query parsing and validation across all resource list endpoints (pagination, search, ordering, fields selection, and reference filtering).
    • Field selection now automatically includes the resource ID; default ordering is applied when none is provided.
  • Bug Fixes
    • Improved validation and error reporting for invalid or incomplete list parameters (including paired reference inputs).
    • Standardized paging behavior when calculating offsets.
  • Documentation
    • Updated mock generation instructions for the current development workflow.
  • Other
    • Paging metadata now uses 64-bit page numbers.

Walkthrough

List query parsing and validation move from pkg/services into pkg/handlers, covering pagination, search, ordering, fields, and reference-pair validation. List endpoints use the new parser, while services retain parameterless defaults, use 64-bit page values, and update list execution to use DAO values and reject non-positive sizes. Field filtering reuses handler normalization helpers, and mock-generation documentation is updated.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ListHandler
  participant parseListParams
  participant ListService
  Client->>ListHandler: Send list request with query parameters
  ListHandler->>parseListParams: Parse and validate query
  parseListParams-->>ListHandler: Return ListArguments or validation error
  ListHandler->>ListService: Execute list operation with ListArguments
  ListService-->>ListHandler: Return list results
Loading

Suggested reviewers: ciaranroche, mliptak0

🚥 Pre-merge checks | ✅ 10 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (10 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main change: moving list parameter parsing into the handler layer.
Description check ✅ Passed The description is directly related to the PR and describes the parsing, validation, and test changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Sec-02: Secrets In Log Output ✅ Passed PASS: no non-test log statement uses token/password/secret/credential as a field or interpolated value; touched files add no logging. CWE-532 not triggered.
No Hardcoded Secrets ✅ Passed No CWE-798/CWE-321 secrets found in touched files; only placeholder password examples in docs, with no apiKey/secret/token assignments or embedded credentials.
No Weak Cryptography ✅ Passed Changed files contain no banned crypto primitives, ECB, custom crypto, or secret-comparison code; searches across modified Go files were empty. CWE-327/CWE-328 not implicated.
No Injection Vectors ✅ Passed No CWE-89/78/79/502 vectors in the patch; SQL paths use dao.NewWhere/Squirrel, and ArgsToOrder whitelists order fields.
No Privileged Containers ✅ Passed PASS: PR changes only code/docs; no Kubernetes/OpenShift manifests, Helm templates, or Dockerfiles were modified, and existing Dockerfile USER root is a documented build-stage exception (CWE-250).
No Pii Or Sensitive Data In Logs ✅ Passed No new slog/zap/fmt.Print* calls in touched files; existing logs emit generic codes/reasons and resource_type/resource_id only, not PII. CWE-532 not triggered.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

Comment @coderabbitai help to get the list of available commands.

@hyperfleet-ci-bot

hyperfleet-ci-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

Risk Score: 3 — risk/medium

Signal Detail Points
PR size 1094 lines (>500) +2
Sensitive paths none +0
Test coverage Missing tests for: pkg/api +1

Computed by hyperfleet-risk-scorer

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/handlers/list_params_test.go (1)

246-255: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Missing coverage for the page upper bound.

Once Page gains a max (see pkg/handlers/list_params.go Line 20-21), add a range-error case (e.g. ?page=99999999999999999999) so the overflow guard is regression-locked. Error paths are otherwise well covered.

As per path instructions: "Error paths SHOULD be tested, not just happy paths".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/handlers/list_params_test.go` around lines 246 - 255, Add a table-driven
test case in the list-parameter validation tests covering a page value above the
`Page` maximum, such as an excessively large numeric page query, and assert the
expected range-validation error. Keep the existing valid large-page case
unchanged and use the current error assertion pattern.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/handlers/list_params.go`:
- Around line 20-21: Cap Page or explicitly validate its multiplication with
Size before loadList computes the offset and calls Fetch. Update the Page
validation in list parameters and ensure (args.Page-1)*int(args.Size) cannot
overflow into a negative offset, while preserving valid pagination behavior.

---

Nitpick comments:
In `@pkg/handlers/list_params_test.go`:
- Around line 246-255: Add a table-driven test case in the list-parameter
validation tests covering a page value above the `Page` maximum, such as an
excessively large numeric page query, and assert the expected range-validation
error. Keep the existing valid large-page case unchanged and use the current
error assertion pattern.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Central YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 15f509e2-4d57-4412-9603-b9ad8c7ae08f

📥 Commits

Reviewing files that changed from the base of the PR and between 99c22d1 and aa6578b.

📒 Files selected for processing (11)
  • docs/development.md
  • pkg/handlers/framework.go
  • pkg/handlers/list_params.go
  • pkg/handlers/list_params_test.go
  • pkg/handlers/resource_handler.go
  • pkg/handlers/resource_status_handler.go
  • pkg/handlers/root_resource_handler.go
  • pkg/services/generic.go
  • pkg/services/resource.go
  • pkg/services/types.go
  • pkg/services/types_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • openshift-hyperfleet/architecture (manual)
  • openshift-hyperfleet/hyperfleet-api (manual)
  • openshift-hyperfleet/hyperfleet-sentinel (manual)
  • openshift-hyperfleet/hyperfleet-adapter (manual)
  • openshift-hyperfleet/hyperfleet-broker (manual)
💤 Files with no reviewable changes (1)
  • pkg/services/types_test.go

Comment thread pkg/handlers/list_params.go Outdated
Extract query parameter parsing from pkg/services/types.go into
pkg/handlers/list_params.go. Use go-playground/validator for struct-level
validation and return structured error details via ValidationWithDetails.

Remove defensive size checks from generic.go service layer (now validated
at the handler boundary) while keeping a guard for Size <= 0.

Update stale mockgen example in docs/development.md.
Comment thread pkg/handlers/list_params.go Outdated
RefType string `validate:"required_with=RefTargetID"`
RefTargetID string `validate:"required_with=RefType"`
Size int64 `validate:"min=1,max=100"`
Page int64 `validate:"min=1,max=10000000"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why max - 10000000? Is that a realistic max? What about like 100000? What's the reason?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, there shouldn't be any max there, but (Page-1)*Size can overflow int, and a negative number will be passed to the database, so it's just-a-big-number.

But honestly, I agree with you, 10000000 might be confusing, 100000 is not 🙂

@ma-hill ma-hill left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

```| page | integer (int64)| No | `1` | Must be >= 1 |
| `size` | integer (int64)| No | `20` | Must be between 1 and 100 |
in api-resources.md are we changing page and size to int64, maybe reasonable to change the docs too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants