Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Apr 15, 2025

No description provided.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 15, 2025

📝 Walkthrough

Walkthrough

The changes update the server's REST API request handling logic to improve type coercion, particularly for fields of JSON type or custom type definitions. The coercion method now receives full field metadata, enabling it to parse JSON strings for relevant fields and provide more robust error handling for invalid JSON input. Corresponding updates are made to filter construction logic to support equality filtering on JSON fields. The test suite is expanded with new schema definitions and test cases to verify correct behavior for JSON field filtering and error handling.

Changes

File(s) Change Summary
packages/server/src/api/rest/index.ts Refactored the coerce method to accept FieldInfo instead of a type string, added JSON parsing logic, updated filter value handling for JSON fields, and adjusted all relevant calls.
packages/server/tests/api/rest.test.ts Extended the test schema with JSON fields, added and updated tests to cover JSON field creation, filtering, and error handling for invalid JSON input.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant REST API (RequestHandler)
    participant Database

    Client->>REST API (RequestHandler): Sends request with JSON field/filter
    REST API (RequestHandler)->>REST API (RequestHandler): Calls coerce(fieldInfo, value)
    alt fieldInfo is JSON or type definition
        REST API (RequestHandler)->>REST API (RequestHandler): Parse value as JSON
        alt Parsing fails
            REST API (RequestHandler)->>Client: Return 400 error (invalid-value)
        else Parsing succeeds
            REST API (RequestHandler)->>Database: Query with parsed JSON value
            Database-->>REST API (RequestHandler): Return result
            REST API (RequestHandler)-->>Client: Return result
        end
    else fieldInfo is primitive
        REST API (RequestHandler)->>REST API (RequestHandler): Coerce value to primitive type
        REST API (RequestHandler)->>Database: Query with coerced value
        Database-->>REST API (RequestHandler): Return result
        REST API (RequestHandler)-->>Client: Return result
    end
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

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: 2

🧹 Nitpick comments (2)
packages/server/src/api/rest/index.ts (1)

1749-1750: TODO for extending JSON filtering.
There's a placeholder note for adding inequality filters. Let me know if you need help implementing them, as they can get tricky for structured or nested JSON values.

packages/server/tests/api/rest.test.ts (1)

758-773: Good error handling test for invalid JSON

The test properly verifies that the server returns an appropriate error response when invalid JSON syntax is provided in the filter. This is critical for providing good developer feedback.

Consider adding additional error test cases for:

  1. Type mismatches (e.g., passing a number when an object is expected)
  2. Filtering with null values or empty objects
  3. Using complex nested properties in filters

These would further strengthen the robustness of your JSON filtering implementation.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 002e132 and 2483557.

📒 Files selected for processing (2)
  • packages/server/src/api/rest/index.ts (8 hunks)
  • packages/server/tests/api/rest.test.ts (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/server/src/api/rest/index.ts (1)
packages/runtime/src/cross/model-meta.ts (1)
  • FieldInfo (26-107)
🪛 Biome (1.9.4)
packages/server/src/api/rest/index.ts

[error] 1358-1358: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)


[error] 1389-1389: Avoid the use of spread (...) syntax on accumulators.

Spread syntax should be avoided on accumulators (like those in .reduce) because it causes a time complexity of O(n^2).
Consider methods such as .splice or .push instead.

(lint/performance/noAccumulatingSpread)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build-test (20.x)
  • GitHub Check: build-test (20.x)
  • GitHub Check: build-test (20.x)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: OSSAR-Scan
🔇 Additional comments (11)
packages/server/src/api/rest/index.ts (6)

1352-1352: Implementation for single ID field scenario looks correct.
No issues found here; the code effectively handles a single field ID by coercing it according to its field type.


1385-1385: Implementation for single ID field scenario looks correct.
The code snippet properly sets the object key for an ID field and handles type coercion.


1439-1448: Good JSON parsing for type definitions and JSON-typed fields.
Throwing an InvalidValueError ensures invalid JSON values are promptly identified and handled. This change solidly supports parsing stringified JSON input, aligning with the PR's goal of improved JSON equality filtering.


1775-1775: Bulk coercion for array values looks correct.
Applying .map with the coerce method for each item is straightforward and consistent.


1786-1789: Equality filtering support for JSON fields is a great addition.
Returning { equals: coerced } correctly handles direct equality comparisons on JSON-typed fields.


1794-1794: Bulk coercion for comma-separated values looks good.
This is consistent with existing logic for filtering multiple possible values.

packages/server/tests/api/rest.test.ts (5)

25-27: Good implementation of the Address type definition

The Address type with a city field is well-defined and provides a good test case for the JSON field filtering functionality.


37-38: Good addition of JSON fields to User model

The two different JSON field types provide comprehensive test coverage:

  1. address as a typed JSON field using the Address type definition
  2. someJson as a generic JSON field

This allows testing both structured and unstructured JSON filtering.


437-438: Good test data setup for JSON fields

The test data is appropriately set up with:

  • A structured JSON object for the address field
  • A simple string value for the someJson field

This provides a good foundation for testing the JSON filtering functionality.


726-740: Thorough test coverage for typed JSON equality filtering

The test correctly verifies JSON equality filtering on the typed address field with:

  • A positive test case that should match one user (Seattle)
  • A negative test case that should match no users (Tokyo)

The use of JSON.stringify() to properly format the filter value is a good practice.


742-756: Good test coverage for plain JSON equality filtering

The test properly verifies equality filtering for the untyped someJson field with:

  • A positive test case matching the string value 'foo'
  • A negative test case with a non-matching string value 'bar'

This confirms that JSON filtering works with primitive values as well as objects.

@ymc9 ymc9 merged commit 401f45d into dev Apr 15, 2025
11 checks passed
@ymc9 ymc9 deleted the fix/rest-json-filter branch April 15, 2025 22:56
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.

2 participants