Skip to content

Conversation

@kart-u
Copy link
Contributor

@kart-u kart-u commented Oct 24, 2025

Proposed change

Resolves #2467

Issue Overview - on visting https://nest.owasp.org/chapters/virtual virtual chapter was on not rendering and giving error 500

Solution - In ChapterMap.tsx we need a filter for case when latitude and longitude are null/none (case of virtual chapter)
image

On side note regarding local test check __tests__/unit/pages/Home.test.tsx this is failing on original main branch even without any changes.

Checklist

  • I've read and followed the contributing guidelines.
  • I've run make check-test locally; all checks and tests passed.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 24, 2025

Summary by CodeRabbit

Bug Fixes

  • Fixed incorrect display of chapters with missing or invalid location coordinates
  • Improved geolocation data validation to prevent markers from appearing at invalid locations
  • Enhanced map view initialization and bounds calculations with better handling of edge cases

Walkthrough

Filters out chapters lacking numeric lat/lng before marker creation, replaces falsy fallbacks with nullish coalescing for lat/lng, and adds tests ensuring undefined/null coords are ignored while [0,0] is accepted.

Changes

Cohort / File(s) Change Summary
Tests — ChapterMap
frontend/__tests__/unit/components/ChapterMap.test.tsx
Import Leaflet as L; remove in-test require calls; add tests to ensure chapters with undefined/null coords are excluded from markers and that [0,0] yields a marker; adapt test setup to use the imported L.
Component — ChapterMap
frontend/src/components/ChapterMap.tsx
Add validGeoLocData pre-filter to include only numeric lat/lng; replace `

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Check filtering correctness for edge cases (0, null, undefined) and numeric parsing.
  • Verify all places that previously read raw geo data now use validGeoLocData.
  • Confirm tests correctly mock/import Leaflet and cover cluster/popups behavior unaffected by filtering.

Possibly related PRs

Suggested reviewers

  • arkid15r
  • kasya
  • abhayymishraa

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title Check ✅ Passed The PR title "fix: Fix error 500 for OWASP Virtual Chapter" directly addresses the main objective of the changeset. The changes in both the source component and test file are specifically designed to fix the error 500 that occurs when visiting the virtual chapter page. The title is concise, uses standard commit conventions, and clearly communicates the bug fix nature of the changes without unnecessary noise or vagueness.
Linked Issues Check ✅ Passed The code changes directly address the requirements from linked issue #2467. The issue reported an error 500 when accessing the virtual chapter page and suspected missing geo coordinates as the cause. The changes implement a comprehensive solution by adding a validGeoLocData filter to exclude chapters with null or undefined coordinates, using nullish coalescing operators for safe lat/lng lookups, and updating all downstream logic to use the filtered dataset. Additionally, the test file enhancements validate the fix by verifying that virtual chapters with undefined or null coordinates are properly filtered out, while chapters with valid coordinates (including [0, 0]) are handled correctly. These changes directly resolve the expected behavior requirement of having the page load without producing a 500 error.
Out of Scope Changes Check ✅ Passed All code changes are directly scoped to resolving issue #2467. The modifications to ChapterMap.tsx focus exclusively on handling missing geo coordinates through filtering and nullish coalescing, which directly addresses the virtual chapter error. The test file enhancements in ChapterMap.test.tsx add validation for the filtering logic and edge cases like null/undefined coordinates and [0, 0] coordinates, all of which are necessary to verify the fix. The developer's note about the pre-existing test failure in Home.test.tsx is informational and not part of the changes in this PR. No unrelated modifications or scope creep is evident.
Description Check ✅ Passed The PR description is directly related to the changeset and clearly explains the problem, solution, and changes made. It references the linked issue #2467, describes the error scenario (visiting the virtual chapter URL), identifies the root cause (missing latitude/longitude coordinates), and explains the solution approach (adding a filter in ChapterMap.tsx). The description includes supporting evidence in the form of a screenshot and confirms that local tests have been run, making it substantive and relevant to the code changes.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a 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

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

Copy link
Contributor

@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 (1)
frontend/src/components/ChapterMap.tsx (1)

57-85: Marker creation logic is sound.

The use of validGeoLocData ensures only chapters with valid coordinates reach this point. The nullish coalescing operator is applied consistently.

Consider extracting coordinates once during filtering to avoid duplication:

const validGeoLocData = geoLocData
  .map((chapter) => {
    const lat = chapter._geoloc?.lat ?? chapter.geoLocation?.lat
    const lng = chapter._geoloc?.lng ?? chapter.geoLocation?.lng
    return typeof lat === 'number' && typeof lng === 'number'
      ? { ...chapter, coordinates: { lat, lng } }
      : null
  })
  .filter((chapter): chapter is Chapter & { coordinates: { lat: number; lng: number } } => 
    chapter !== null
  )

// Then use: chapter.coordinates.lat, chapter.coordinates.lng

This eliminates repeated coordinate lookups and improves type safety.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8df8423 and a8e3f02.

📒 Files selected for processing (2)
  • frontend/__tests__/unit/components/ChapterMap.test.tsx (2 hunks)
  • frontend/src/components/ChapterMap.tsx (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
frontend/__tests__/unit/components/ChapterMap.test.tsx (1)
frontend/__tests__/unit/data/mockChapterData.ts (1)
  • mockChapterData (1-32)
🔇 Additional comments (5)
frontend/__tests__/unit/components/ChapterMap.test.tsx (3)

166-182: Excellent test coverage for undefined coordinates.

This test correctly validates that virtual chapters without location data are filtered out, preventing the 500 error.


184-200: Good coverage for null coordinates.

This test ensures null values are also filtered correctly, complementing the undefined test case.


238-252: Critical edge case test for zero coordinates.

This test is particularly important because it validates that the nullish coalescing (??) operator correctly preserves valid coordinates at [0, 0] (Gulf of Guinea), which would be incorrectly filtered with a logical OR (||) operator.

frontend/src/components/ChapterMap.tsx (2)

89-108: Local view logic correctly updated.

The changes consistently use validGeoLocData for bounds calculation and nearest chapter selection, ensuring the local view only considers chapters with valid coordinates. The length check at line 89 properly guards against empty filtered data.


51-55: Filter logic is correct; coordinate mixing doesn't occur in practice.

The nullish coalescing approach properly handles the fallback between _geoloc and geoLocation. Test data and the data model show that both fields contain the same coordinates when defined, so mixing doesn't happen in practice. The current implementation correctly prevents the 500 error for virtual chapters and is ready as-is.

The suggested refactor to validate coordinates come from the same source is a nice-to-have for stricter validation if you prefer that pattern, but not necessary given your data model structure.

Comment on lines 168 to 169
const L = require('leaflet')
const virtualChapterData: Chapter[] = [
Copy link
Collaborator

Choose a reason for hiding this comment

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

We do not use require in this project. This is why you were getting the linting error - please do not just disable the error with the comment, they are there for a reason.

So please update these tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about this. I wrote the test this way because the previous tests were also using require. I have now replaced all these tests using require with import

Copy link
Collaborator

Choose a reason for hiding this comment

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

@kart-u yes, sorry! After I left this message I did a global search and found a few more spots where we had the require statement that clearly missed in some prev. PRs. I'll be cleaning those up. Thanks for updating your code!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hello @kasya
just wanted to check if everything in current PR looks good or is there anything else you’d like me to update?

@kart-u kart-u requested a review from kasya October 26, 2025 21:32
Copy link
Contributor

@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 (1)
frontend/__tests__/unit/components/ChapterMap.test.tsx (1)

156-186: Good coverage for virtual chapter filtering.

The tests properly validate that chapters with undefined or null coordinates are filtered out, which directly addresses the 500 error for the OWASP Virtual Chapter. The distinction between null and undefined is appropriate for nullish coalescing behavior.

Consider adding tests for partial null coordinates (e.g., lat valid but lng null) to ensure the implementation handles mixed scenarios correctly:

it('filters out chapters with partial null coordinates', () => {
  const partialNullData: Chapter[] = [
    mockChapterData[0],
    {
      ...mockChapterData[1],
      _geoloc: { lat: 51.5074, lng: null },
      geoLocation: { lat: 51.5074, lng: null },
    },
  ]

  render(<ChapterMap {...defaultProps} geoLocData={partialNullData} />)
  expect(L.marker).toHaveBeenCalledTimes(1)
})
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1c9ec30 and 3a8d8b2.

📒 Files selected for processing (1)
  • frontend/__tests__/unit/components/ChapterMap.test.tsx (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
frontend/__tests__/unit/components/ChapterMap.test.tsx (1)
frontend/__tests__/unit/data/mockChapterData.ts (1)
  • mockChapterData (1-32)
🔇 Additional comments (2)
frontend/__tests__/unit/components/ChapterMap.test.tsx (2)

2-2: LGTM! Import statement follows project standards.

The change from require to import aligns with the feedback from previous reviews and project conventions.


219-231: Excellent edge case coverage for zero coordinates.

This test is crucial because it validates that legitimate [0, 0] coordinates (Gulf of Guinea intersection of Prime Meridian and Equator) are not filtered out. Since 0 is falsy in JavaScript, this confirms the implementation correctly uses nullish coalescing rather than falsy checks.

Copy link
Collaborator

@kasya kasya left a comment

Choose a reason for hiding this comment

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

This works great now! Thanks @kart-u ! 👍🏼

@sonarqubecloud
Copy link

@kasya kasya enabled auto-merge October 31, 2025 01:12
@kasya kasya added this pull request to the merge queue Oct 31, 2025
Merged via the queue into OWASP:main with commit 18878d4 Oct 31, 2025
26 checks passed
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.

Fix error 500 for OWASP Virtual Chapter

2 participants