Skip to content

Conversation

marcalexiei
Copy link

@marcalexiei marcalexiei commented Oct 8, 2025

Summary by CodeRabbit

  • Bug Fixes

    • OpenAPI generation now correctly excludes routes by specified tags.
    • Handles missing operation details gracefully to prevent errors during schema creation.
  • Tests

    • Added coverage to verify route exclusion by tags in the OpenAPI output.

Copy link

coderabbitai bot commented Oct 8, 2025

Walkthrough

The change makes hooks.detail optional in toOpenAPISchema and updates tag-exclusion checks to use optional chaining. A new test verifies that routes tagged with excluded tags are omitted from the generated OpenAPI JSON while untagged routes remain.

Changes

Cohort / File(s) Summary
OpenAPI schema guard update
src/openapi.ts
Made hooks.detail optional (detail?) and updated exclusion logic to use hooks.detail?.tags?.some(...) to prevent runtime errors when detail is undefined.
Test: exclude by tags
test/index.test.ts
Added test ensuring routes with excluded tags (e.g., "internal") are omitted from OpenAPI output while others remain.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant C as Client
    participant S as Server
    participant OA as OpenAPI Plugin
    participant R as Routes

    C->>S: GET /openapi.json
    S->>OA: Generate OpenAPI spec
    loop For each route
        OA->>R: Read route hooks
        Note over OA,R: Check exclusion by tags
        OA->>OA: if hooks.detail?.tags intersects exclude.tags -> skip
        OA->>OA: else include operation in spec
    end
    OA-->>S: OpenAPI document
    S-->>C: 200 OK + JSON

    %% Styling (labels also describe changes)
    rect rgba(220,240,255,0.5)
    Note right of OA: Updated guard uses optional chaining on detail/tags
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitch my whiskers, sift through logs,
A missing detail? Optional—no fogs.
Tags that whisper “internal,” I hop past,
The spec stays crisp, compiled fast.
With one soft thump upon the ground,
The OpenAPI map is sound. 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title clearly describes the main fix of avoiding an error when applying exclude.tags to routes lacking tags and directly references the key functionality under change without extraneous details or vague terms.
Linked Issues Check ✅ Passed The implementation makes hooks.detail optional and uses safe optional chaining to check tags, while the new test confirms that routes with excluded tags are omitted without errors, fully satisfying the requirements of issue #273.
Out of Scope Changes Check ✅ Passed All changes are directly related to handling optional route details and excluding tagged routes, and no unrelated code or features have been introduced outside the scope of the linked issue objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ 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

@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)
test/index.test.ts (1)

276-308: LGTM! Test validates the fix for issue #273.

The test correctly verifies that:

  • Routes with excluded tags are omitted from OpenAPI JSON
  • Routes without tags remain in the output
  • No TypeError occurs (which was the original bug)

Consider adding test coverage for additional edge cases:

it('should partially exclude routes with mixed tags', async () => {
  const app = new Elysia()
    .use(openapi({ exclude: { tags: ['internal'] } }))
    .get('/public', () => 'public', {
      detail: { tags: ['public', 'api'] }
    })
    .get('/mixed', () => 'mixed', {
      detail: { tags: ['internal', 'public'] }
    })

  await app.modules

  const res = await app.handle(req('/openapi/json'))
  const response = await res.json()
  
  // /public should be included (no internal tag)
  expect(response.paths['/public']).toBeDefined()
  // /mixed should be excluded (has internal tag)
  expect(response.paths['/mixed']).toBeUndefined()
})

This would verify behavior when a route has multiple tags, some of which match the exclusion list.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 070919e and 4af8db4.

📒 Files selected for processing (2)
  • src/openapi.ts (2 hunks)
  • test/index.test.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/index.test.ts (1)
src/index.ts (1)
  • openapi (40-181)
🔇 Additional comments (2)
src/openapi.ts (2)

323-323: LGTM! Correctly makes detail optional.

This change accurately reflects that routes may not have detail defined, preventing TypeErrors when accessing properties on undefined.


381-385: LGTM! Proper optional chaining prevents TypeError.

The optional chaining on hooks.detail?.tags correctly handles cases where detail is undefined, which was the root cause of the bug reported in #273.

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.

bug: setting tags on only one API cause error on openapi page

1 participant