Thanks to visit codestin.com
Credit goes to github.com

Skip to content

harden numeric constraint parsing and expand route tests#4054

Merged
ReneWerner87 merged 1 commit intomainfrom
handle-strconv.atoi-errors-in-constraint
Feb 5, 2026
Merged

harden numeric constraint parsing and expand route tests#4054
ReneWerner87 merged 1 commit intomainfrom
handle-strconv.atoi-errors-in-constraint

Conversation

@gaby
Copy link
Member

@gaby gaby commented Feb 5, 2026

Motivation

  • Prevent incorrect matches or panics by treating malformed numeric constraint metadata as invalid and failing the constraint check deterministically.
  • Restore and expand route test coverage so both malformed/escaped metadata scenarios and valid numeric-range behavior are exercised.
  • Make the invalid-metadata test name conform to the project naming convention used elsewhere.

Description

  • In path.go, Constraint.CheckConstraint now checks strconv.Atoi parse errors for numeric metadata-driven constraints (minLen, maxLen, len, betweenLen, min, max, range) and returns false when metadata cannot be parsed.
  • Renamed the test to Test_ConstraintCheckConstraint_InvalidMetadata and added explicit cases for invalid numeric metadata in path_test.go covering minLen, maxLen, len, betweenLen, min, max, and range.
  • Restored/added route testcases in path_testcases_test.go to keep the malformed escaped-comma range(10\,30,1500) checks and add a separate valid range(10,1500) case that matches numeric parameters like 25 and 1200.
  • Applied minor formatting/housekeeping as part of test additions and validation steps.

@gaby gaby requested a review from a team as a code owner February 5, 2026 04:50
@gaby gaby requested review from Copilot and removed request for Copilot February 5, 2026 04:50
@gaby gaby added the codex label Feb 5, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 5, 2026

Walkthrough

The PR replaces silent error handling in numeric constraint validation with explicit parse error checks. When strconv.Atoi fails, constraint functions now return false immediately instead of propagating zero values. Corresponding test coverage is added for invalid metadata scenarios and range constraint edge cases.

Changes

Cohort / File(s) Summary
Constraint Validation Error Handling
path.go
Added explicit parse error handling for strconv.Atoi operations across all numeric constraint checks (betweenLen, minLen, maxLen, len, min, max, range). Each constraint now returns false on parse failure instead of silently using zero values. Removed prior nolint directives in favor of proper error handling.
Test Coverage for Invalid Metadata
path_test.go
Added parameterized test function covering multiple invalid metadata scenarios for constraint checks. Tests verify that CheckConstraint returns false for mismatched data and param values across constraint types (minLen, maxLen, len, betweenLen, min, max, range).
Range Constraint Test Cases
path_testcases_test.go
Inserted new testSet for pattern /api/v1/:param<range(10,1500)> with test cases expecting no match. Positioned before existing testSet for the same pattern to establish baseline non-matching behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested labels

☢️ Bug, v3

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn

Poem

🐰 Hop hop, the errors caught at last,
No silent zeros in the past,
Each parse now checked with honest care,
Constraints validated everywhere! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly summarizes the main changes: hardening numeric constraint parsing error handling and expanding test coverage for route patterns.
Description check ✅ Passed The description provides clear motivation, explains the specific changes across all modified files, and references testing performed. However, it does not follow the required template structure with checklist items and change type selections.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch handle-strconv.atoi-errors-in-constraint

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@ReneWerner87 ReneWerner87 added this to v3 Feb 5, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone Feb 5, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the robustness of route constraint parsing by preventing panics or incorrect matches when numeric constraint metadata is malformed. It also enhances the test suite to ensure comprehensive coverage of these new error handling mechanisms and valid numeric range scenarios, leading to more reliable and predictable routing behavior.

Highlights

  • Numeric Constraint Hardening: The CheckConstraint function now robustly handles malformed numeric metadata by explicitly checking strconv.Atoi parse errors for constraints like minLen, maxLen, len, betweenLen, min, max, and range. If parsing fails, the constraint check deterministically returns false, preventing incorrect matches or panics.
  • Expanded Test Coverage for Invalid Metadata: New test cases have been added in path_test.go under Test_ConstraintCheckConstraint_InvalidMetadata to specifically validate the handling of invalid numeric metadata across various constraint types, ensuring the new error handling works as expected.
  • Route Test Restoration and Expansion: Route test cases in path_testcases_test.go were restored and expanded to cover both malformed/escaped metadata scenarios (e.g., range(10\,30,1500)) and valid numeric-range behavior (e.g., range(10,1500)) for range constraints, improving overall route matching reliability.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • path.go
    • Modified CheckConstraint to explicitly check for strconv.Atoi parse errors when processing numeric constraint metadata (e.g., minLen, maxLen, len, betweenLen, min, max, range). If parsing fails, the constraint check now returns false.
    • Removed a nolint:errcheck comment, indicating that error handling is now properly implemented for these cases.
  • path_test.go
    • Added a new test function Test_ConstraintCheckConstraint_InvalidMetadata to verify that CheckConstraint correctly returns false for various numeric constraints when their metadata is unparseable (e.g., "abc" instead of a number).
  • path_testcases_test.go
    • Restored and added new routeTestCase entries for the range constraint. This includes cases for malformed escaped-comma ranges (range(10\,30,1500)) and a new valid numeric range (range(10,1500)) to ensure correct matching for numbers within the specified range.
Activity
  • The author ran make audit, which reported environmental govulncheck vulnerabilities, but these were not related to the PR changes.
  • make generate, make betteralign, make modernize, make format, and make lint commands were executed successfully.
  • The full test suite (make test) passed with 2633 tests.
  • Targeted tests for Test_ConstraintCheckConstraint_InvalidMetadata and Test_Path_matchParams were run and passed successfully.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively hardens the numeric constraint parsing by adding error handling for malformed metadata, preventing potential panics or incorrect behavior. The accompanying test cases in path_test.go and path_testcases_test.go are well-written and provide good coverage for both invalid metadata scenarios and valid numeric range behavior. My main feedback is focused on reducing code duplication in path.go to improve maintainability, for which I've left a couple of suggestions.

@codecov
Copy link

codecov bot commented Feb 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.14%. Comparing base (eff8808) to head (0fcdd96).
⚠️ Report is 96 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4054      +/-   ##
==========================================
- Coverage   91.19%   91.14%   -0.06%     
==========================================
  Files         119      119              
  Lines       11146    11164      +18     
==========================================
+ Hits        10165    10175      +10     
- Misses        622      628       +6     
- Partials      359      361       +2     
Flag Coverage Δ
unittests 91.14% <100.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ReneWerner87 ReneWerner87 changed the title 🐛 bug: harden numeric constraint parsing and expand route tests harden numeric constraint parsing and expand route tests Feb 5, 2026
@ReneWerner87 ReneWerner87 merged commit 4df2fce into main Feb 5, 2026
26 of 28 checks passed
@ReneWerner87 ReneWerner87 deleted the handle-strconv.atoi-errors-in-constraint branch February 5, 2026 07:31
@github-project-automation github-project-automation bot moved this to Done in v3 Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants