Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ffi): Add type checking to only accept JSON objects as user-defined stream-level metadata for the new IR format. #691

Merged
merged 2 commits into from
Jan 24, 2025

Conversation

LinZhihao-723
Copy link
Member

@LinZhihao-723 LinZhihao-723 commented Jan 24, 2025

Description

As introduced in #677, we added support for serializing/deserializing user-defined stream-level metadata. However, we didn't add any type checking in this implementation, meaning that user can give any valid nlohmann::json instances, including arrays and primitive type values.

After an offline discussion with @kirkrodrigues, we decided to constrain the metadata type to only accept JSON objects (key-value pair dictionaries) to avoid undefined behaviors in our ffi libraries. This PR implements this new requirement.

Ideally, the way to implement this PR is to constrain the type of the input to be nlohmann::json::object_t, so that other types of inputs can't be compiled. However, after testing I realized the compiler doesn't prevent an implicit conversion from nlohmann::json to nlohmann::json::object_t, meaning that you could still pass in any nlohmann::json object to a nlohmann::json::object_t typed parameter, and the error will be raised happens during the runtime through a nlohmann::json exception. To prevent the use of exceptions, we add explicit type checking before serializing the user-defined metadata, and return std::errc::protocol_not_supported when seeing a non-JSON-object input.

Validation performed

  • Ensure all workflows passed.
  • Added new unit tests to ensure invalid types of values can't be serialized.

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced metadata validation for serialization process.
  • Bug Fixes

    • Added robust error handling for invalid user-defined metadata inputs.
  • Tests

    • Introduced comprehensive test coverage for invalid user-defined metadata scenarios.

The update improves the reliability of metadata processing by ensuring only valid JSON objects are accepted during serialization, preventing potential data integrity issues.

Copy link
Contributor

coderabbitai bot commented Jan 24, 2025

Walkthrough

This pull request introduces enhanced error handling for the Serializer class in the IR stream component. The changes focus on validating user-defined metadata during serialization, specifically ensuring that when metadata is provided, it must be a JSON object. A new error condition has been added to return std::errc::protocol_not_supported if the metadata does not meet this requirement. The modifications include updates to the implementation in the .cpp file, documentation in the .hpp file, and the addition of a corresponding test case to verify the new validation logic.

Changes

File Change Summary
components/core/src/clp/ffi/ir_stream/Serializer.cpp Added error handling to validate that optional_user_defined_metadata is a JSON object
components/core/src/clp/ffi/ir_stream/Serializer.hpp Updated documentation comments for create method to clarify metadata requirements and potential error conditions
components/core/tests/test-ir_encoding_methods.cpp Added new test case ffi_ir_stream_Serializer_serialize_invalid_user_defined_metadata to validate metadata input validation

Sequence Diagram

sequenceDiagram
    participant Client
    participant Serializer
    participant Metadata

    Client->>Serializer: create(optional_user_defined_metadata)
    Serializer->>Metadata: Validate metadata type
    alt Metadata is JSON object
        Serializer-->>Client: Serializer instance
    else Metadata is not JSON object
        Serializer-->>Client: Error (std::errc::protocol_not_supported)
    end
Loading

Possibly related PRs

Suggested reviewers

  • kirkrodrigues

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a809436 and 5942e4d.

📒 Files selected for processing (1)
  • components/core/src/clp/ffi/ir_stream/Serializer.hpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • components/core/src/clp/ffi/ir_stream/Serializer.hpp
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: centos-stream-9-static-linked-bins
  • GitHub Check: centos-stream-9-dynamic-linked-bins
  • GitHub Check: ubuntu-focal-static-linked-bins
  • GitHub Check: ubuntu-focal-dynamic-linked-bins
  • GitHub Check: ubuntu-jammy-static-linked-bins
  • GitHub Check: ubuntu-jammy-dynamic-linked-bins
  • GitHub Check: build-macos (macos-14, false)
  • GitHub Check: build-macos (macos-14, true)
  • GitHub Check: build-macos (macos-13, false)
  • GitHub Check: lint-check (ubuntu-latest)
  • GitHub Check: lint-check (macos-latest)
  • GitHub Check: build-macos (macos-13, true)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 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. (Beta)
  • @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: 0

🧹 Nitpick comments (1)
components/core/src/clp/ffi/ir_stream/Serializer.cpp (1)

553-555: Consider including diagnostic details on invalid JSON metadata
While returning std::errc::protocol_not_supported is correct, adding context about which field is invalid (or logging an explanation) can help developers quickly pinpoint issues.

 if (false == optional_user_defined_metadata.value().is_object()) {
+    // Optional: Provide helpful logging/diagnostics about invalid metadata
+    // e.g. LOG_WARNING("User-defined metadata is not a JSON object: "
+    //                 << optional_user_defined_metadata.value().dump());
     return std::errc::protocol_not_supported;
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 66067d6 and a809436.

📒 Files selected for processing (3)
  • components/core/src/clp/ffi/ir_stream/Serializer.cpp (1 hunks)
  • components/core/src/clp/ffi/ir_stream/Serializer.hpp (1 hunks)
  • components/core/tests/test-ir_encoding_methods.cpp (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
components/core/src/clp/ffi/ir_stream/Serializer.cpp (1)

Pattern **/*.{cpp,hpp,java,js,jsx,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

components/core/src/clp/ffi/ir_stream/Serializer.hpp (1)

Pattern **/*.{cpp,hpp,java,js,jsx,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

components/core/tests/test-ir_encoding_methods.cpp (1)

Pattern **/*.{cpp,hpp,java,js,jsx,ts,tsx}: - Prefer false == <expression> rather than !<expression>.

⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: ubuntu-jammy-static-linked-bins
  • GitHub Check: centos-stream-9-static-linked-bins
  • GitHub Check: centos-stream-9-dynamic-linked-bins
  • GitHub Check: ubuntu-jammy-dynamic-linked-bins
  • GitHub Check: ubuntu-focal-static-linked-bins
  • GitHub Check: ubuntu-focal-dynamic-linked-bins
  • GitHub Check: build-macos (macos-14, false)
  • GitHub Check: build-macos (macos-14, true)
  • GitHub Check: lint-check (ubuntu-latest)
  • GitHub Check: build-macos (macos-13, false)
  • GitHub Check: lint-check (macos-latest)
  • GitHub Check: build-macos (macos-13, true)
🔇 Additional comments (2)
components/core/src/clp/ffi/ir_stream/Serializer.hpp (1)

44-48: Documentation clarifies object-only input for metadata
The docstring precisely documents that optional_user_defined_metadata must be a JSON object. This ensures consistency between the intended design and the actual validation logic. Good job!

components/core/tests/test-ir_encoding_methods.cpp (1)

1497-1516: Comprehensive coverage of invalid user-defined metadata
These tests thoroughly validate all major cases for invalid inputs (string, integer, float, boolean, null, array). This ensures that only JSON objects are permitted and is aligned with the PR objective. Great work!

@LinZhihao-723 LinZhihao-723 merged commit b80f756 into y-scope:main Jan 24, 2025
21 checks passed
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