fix(server): omit relations to sliced-away models from OpenAPI spec#2706
Conversation
The RPC OpenAPI generator emitted a `$ref` for every relation field regardless of slicing. A model referencing an excluded model (e.g. `Link.workspace` -> excluded `Workspace`) produced a dangling `$ref: #/components/schemas/Workspace`, leaking the excluded model and making the generated spec structurally invalid. `buildModelEntitySchema` now skips relation fields whose target model is not included by the slicing options. The REST generator already guarded these spots. Fixes #2628 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Claude finished @ymc9's task —— View job Code Review Complete ✅
Review SummaryThis is a well-implemented bug fix that addresses a specific issue with OpenAPI spec generation when models are sliced away. The fix correctly handles dangling Code Quality & Best Practices ⭐Excellent implementation - The fix follows several good practices:
The added code at // Skip relations pointing to a model that's sliced away — otherwise we'd emit
// a dangling `$ref` to a schema that's not in the spec.
if (!isModelIncluded(fieldDef.type, this.queryOptions)) continue;Test Coverage ⭐Comprehensive test coverage with the new test case at lines 599-617:
Security Considerations ✅No security concerns identified:
Performance Considerations ✅Minimal performance impact:
Potential Issues & SuggestionsMinor Considerations:
Final AssessmentThis is a high-quality bug fix that:
The implementation demonstrates good software engineering practices and should be safe to merge. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughOpenAPI schema generation now omits relation properties whose target models are excluded and makes procedure response schemas fall back to generic shapes when their return model is excluded, avoiding dangling $ref references. Tests were added to verify both behaviors. ChangesOpenAPI Excluded Model Filtering
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/server/test/openapi/rpc-openapi.test.ts (1)
599-617: ⚡ Quick winConsider verifying that FK scalar fields remain present.
The test validates that relation fields (
posts,post) are removed, but doesn't verify that foreign-key scalar fields likeComment.postIdremain intact. Since the PR summary explicitly states "Foreign-key scalar fields (e.g., workspaceId) are intentionally unchanged," adding an assertion would document this design decision and prevent regressions.🧪 Suggested enhancement to verify FK scalar field preservation
const commentProps = (spec.components?.schemas?.['Comment'] as any)?.properties; expect(commentProps?.['content']).toBeDefined(); expect(commentProps?.['post']).toBeUndefined(); + // FK scalar fields should remain even when the relation is dropped + expect(commentProps?.['postId']).toBeDefined(); });🤖 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 `@packages/server/test/openapi/rpc-openapi.test.ts` around lines 599 - 617, Add assertions to the test that verify foreign-key scalar fields are preserved: after generating spec with generateSpec(handler) inspect spec.components?.schemas?['Comment'] and assert that the scalar FK property (e.g., 'postId') is still defined (and similarly any other FK scalars you expect, like workspaceId); keep the existing checks that relation properties ('posts', 'post') are removed. Locate this logic around the existing test using createTestClient, RPCApiHandler and generateSpec to add the new expect(...) assertions.
🤖 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.
Nitpick comments:
In `@packages/server/test/openapi/rpc-openapi.test.ts`:
- Around line 599-617: Add assertions to the test that verify foreign-key scalar
fields are preserved: after generating spec with generateSpec(handler) inspect
spec.components?.schemas?['Comment'] and assert that the scalar FK property
(e.g., 'postId') is still defined (and similarly any other FK scalars you
expect, like workspaceId); keep the existing checks that relation properties
('posts', 'post') are removed. Locate this logic around the existing test using
createTestClient, RPCApiHandler and generateSpec to add the new expect(...)
assertions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 3f5fa5de-e260-4094-a045-7aa03fd9dd6b
📒 Files selected for processing (2)
packages/server/src/api/rpc/openapi.tspackages/server/test/openapi/rpc-openapi.test.ts
A procedure is exposed based only on procedure-level slicing, so a procedure whose return type is a model that's excluded via model slicing emitted a `$ref` to the absent model entity schema — another dangling reference that leaks the excluded model and invalidates the spec. `getProcedureDataSchema` now falls back to a generic schema when the return model is sliced away. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Claude finished @ymc9's task —— View job Code Review Complete ✅
Review SummaryThis is a well-implemented bug fix that addresses a specific issue with OpenAPI spec generation when models are sliced away. The fix correctly handles dangling Code Quality & Best Practices ⭐Excellent implementation - The fix follows several good practices:
The added code at // Skip relations pointing to a model that's sliced away — otherwise we'd emit
// a dangling `$ref` to a schema that's not in the spec.
if (!isModelIncluded(fieldDef.type, this.queryOptions)) continue;Test Coverage ⭐Comprehensive test coverage with the new test case at lines 599-617:
Security Considerations ✅No security concerns identified:
Performance Considerations ✅Minimal performance impact:
Potential Issues & SuggestionsMinor Considerations:
Final AssessmentThis is a high-quality bug fix that:
The implementation demonstrates good software engineering practices and should be safe to merge. |
Summary
Fixes #2628.
When a model is excluded via
slicing.excludedModels(or not inincludedModels), the RPC OpenAPI generator still emitted a$reffor relation fields on other models that point to the excluded model. For example, withWorkspaceexcluded, theLinkentity schema still contained:Since the
Workspaceschema is (correctly) not generated, this is a dangling$ref— it leaks the excluded model and makes the generated spec structurally invalid (it even fails OpenAPI validation).Fix
RPCApiSpecGenerator.buildModelEntitySchemanow skips relation fields whose target model is not included by the slicing options, mirroring the guard the REST generator already applies in its relation-emitting paths.Note: per the issue scope this addresses relation inclusion only; FK scalar fields (e.g.
workspaceId) are intentionally left unchanged for now.Test
Added a test under
RPC OpenAPI spec generation - queryOptions slicingthat excludesPostand asserts the referencingUser/Commententity schemas keep their scalar fields but drop theposts/postrelations. BecausegenerateSpecvalidates the document, the previously dangling$refmade this fail before the fix. Verified red→green.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests