Skip to content

Conversation

@shubham1g5
Copy link
Contributor

@shubham1g5 shubham1g5 commented Jun 13, 2025

Backmerge 2.57

Summary by CodeRabbit

  • New Features

    • Added validation to ensure all polygon and point coordinates are within valid geographic ranges. The app now alerts users if invalid coordinates are entered.
  • Bug Fixes

    • Improved error handling for invalid or malformed polygon and point coordinates, preventing incorrect processing and providing clearer feedback.
  • Tests

    • Expanded test coverage to verify that errors are correctly raised for out-of-range or incorrectly formatted coordinates.

@coderabbitai
Copy link

coderabbitai bot commented Jun 13, 2025

Walkthrough

Coordinate validation logic was added to polygon utility methods to ensure all latitude and longitude values are within valid geographic ranges. The methods now throw IllegalArgumentException for invalid input. Corresponding tests were updated and expanded to verify that exceptions are thrown for malformed or out-of-range coordinates.

Changes

File(s) Change Summary
src/main/java/org/javarosa/core/model/utils/PolygonUtils.java Added coordinate validation to polygon creation and point methods; updated method signatures to throw IllegalArgumentException.
src/test/java/org/javarosa/xpath/test/XPathEvalTest.java Expanded tests for invalid polygon and point coordinates, verifying exception handling.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant PolygonUtils

    Caller->>PolygonUtils: createPolygon(coordinates)
    PolygonUtils->>PolygonUtils: isValidCoordinates(lat, lon) for each point
    alt Invalid coordinate
        PolygonUtils-->>Caller: throw IllegalArgumentException
    else All valid
        PolygonUtils-->>Caller: return Polygon
    end

    Caller->>PolygonUtils: isPointInsideOrOnPolygon(point, polygon)
    PolygonUtils->>PolygonUtils: isValidCoordinates(point.lat, point.lon)
    alt Invalid coordinate
        PolygonUtils-->>Caller: throw IllegalArgumentException
    else Valid
        PolygonUtils-->>Caller: boolean result
    end

    Caller->>PolygonUtils: getClosestPointOnPolygon(point, polygon)
    PolygonUtils->>PolygonUtils: isValidCoordinates(point.lat, point.lon)
    alt Invalid coordinate
        PolygonUtils-->>Caller: throw IllegalArgumentException
    else Valid
        PolygonUtils-->>Caller: closest point string
    end
Loading

Possibly related PRs

Suggested labels

cross requested

Poem

In fields of code where polygons roam,
Coordinates now must stay close to home.
If you wander too far, an exception will shout,
"Latitude or longitude? You're out!"
With tests to ensure that errors are caught,
Our polygons behave just as they ought.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch commcare_2.57
  • Post Copyable Unit Tests in Comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@shubham1g5 shubham1g5 requested a review from pm-dimagi June 13, 2025 14:48
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/main/java/org/javarosa/core/model/utils/PolygonUtils.java (3)

37-38: Validation should happen before mutating state

You add the Coordinate to the array and only then validate it.
If validation fails, the partially-filled coordinates array is left in an inconsistent state until the exception unwinds.
It’s safer (and fractionally cheaper) to validate the raw lat/lon doubles before constructing / storing the Coordinate.

-coordinates[i] = new Coordinate(longitude, latitude); // JTS uses x=longitude, y=latitude
-isValidCoordinates(coordinates[i].getY(), coordinates[i].getX());
+isValidCoordinates(latitude, longitude);
+coordinates[i] = new Coordinate(longitude, latitude); // JTS uses x=longitude, y=latitude

54-58: Method name implies a boolean result but returns void

isValidCoordinates(...) suggests a predicate returning true/false.
Since the method throws on failure and has no return value, a clearer name such as validateCoordinates(..) (or assertValidCoordinates) prevents confusion.

While you’re here, consider including the offending values in the exception message to aid debugging.

-private static void isValidCoordinates(double latitude, double longitude) {
-    if ((latitude < -90.0 || latitude > 90.0) || (longitude < -180.0 || longitude > 180.0)) {
-        throw new IllegalArgumentException("Invalid polygon coordinates");
-    }
-}
+private static void validateCoordinates(double latitude, double longitude) {
+    if ((latitude < -90.0 || latitude > 90.0) || (longitude < -180.0 || longitude > 180.0)) {
+        throw new IllegalArgumentException(
+            String.format("Invalid coordinates: lat=%f lon=%f (valid lat ∈ [-90,90], lon ∈ [-180,180])",
+                          latitude, longitude));
+    }
+}

Note: remember to update the three call sites.


67-70: Repeated GeometryFactory instantiation

Both isPointInsideOrOnPolygon and getClosestPointOnPolygon create a new GeometryFactory on every call.
GeometryFactory is inexpensive, but it’s also stateless; a single private static final GeometryFactory GF = new GeometryFactory(); reused everywhere avoids needless allocations.

Also applies to: 82-85

src/test/java/org/javarosa/xpath/test/XPathEvalTest.java (1)

684-704: Tests are valuable but highly repetitive – consider parameterisation

The six new negative-case assertions differ only in their input strings.
JUnit 4 doesn’t have native parameterised tests, but @RunWith(Parameterized.class) or JUnit 5’s @ParameterizedTest would let you express the same coverage with a single data-driven method, reducing boiler-plate and making future additions easier.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b1c4f3e and 7ebdd13.

📒 Files selected for processing (2)
  • src/main/java/org/javarosa/core/model/utils/PolygonUtils.java (3 hunks)
  • src/test/java/org/javarosa/xpath/test/XPathEvalTest.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: task-list-completed
  • GitHub Check: build

@shubham1g5 shubham1g5 merged commit 0e029fb into master Jun 13, 2025
2 of 3 checks passed
@shubham1g5 shubham1g5 deleted the commcare_2.57 branch June 13, 2025 15:21
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.

4 participants