Skip to content

fix: Strict JSON value to fail on duplicate keys when ingesting #1334

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

Merged
merged 2 commits into from
Jun 9, 2025

Conversation

vrongmeal
Copy link
Contributor

@vrongmeal vrongmeal commented Jun 3, 2025

Fixes #1217.

Description

Implements a StrictValue for JSON deserialization that flags duplicate keys in JSON map instead of accepting the last key (like the default implementation).

Added unit tests for verifying deserialization.


This PR has:

  • been tested to ensure log ingestion and log query works.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added documentation for new or modified features or behaviors.

Summary by CodeRabbit

  • New Features
    • Introduced stricter JSON input validation for all HTTP ingest endpoints, now rejecting payloads with duplicate keys.
  • Bug Fixes
    • Improved reliability of JSON parsing by enforcing unique keys in incoming data.

Implements a `StrictValue` for JSON deserialization that flags duplicate
keys in JSON map instead of accepting the last key (like the default
implementation).

Added unit tests for verifying deserialization.

Resolves: parseablehq#1217
Copy link
Contributor

coderabbitai bot commented Jun 3, 2025

Walkthrough

The changes introduce a new StrictValue type to enforce strict JSON parsing with duplicate key detection. All HTTP ingest handler functions now use StrictValue instead of serde_json::Value. A new module implements the strict JSON logic, and the codebase is updated to utilize this stricter deserialization for all ingestion endpoints.

Changes

File(s) Change Summary
src/handlers/http/ingest.rs Replaced all uses of serde_json::Value with StrictValue in function signatures and logic for JSON ingestion.
src/utils/json/mod.rs Added public module declaration for strict.
src/utils/json/strict.rs Introduced new module implementing StrictValue for strict JSON deserialization with duplicate key detection.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant HTTPHandler
    participant StrictValue
    participant ProcessingLogic

    Client->>HTTPHandler: Send JSON payload
    HTTPHandler->>StrictValue: Deserialize JSON (strict mode)
    alt No duplicate keys
        StrictValue->>HTTPHandler: Return parsed Value
        HTTPHandler->>ProcessingLogic: Pass Value for ingestion
        ProcessingLogic->>HTTPHandler: Ingestion result
        HTTPHandler->>Client: Respond success/failure
    else Duplicate keys found
        StrictValue->>HTTPHandler: Return error
        HTTPHandler->>Client: Respond with error
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Ingestion should fail when trying to ingest a log with duplicate keys (#1217)

Poem

A rabbit with code in its paws,
Now checks JSON for logical flaws.
Duplicate keys? No way!
StrictValue saves the day.
Ingestion is neat,
With no repeat—
Bugs hop away, and all is complete! 🐇


📜 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 d1343dc and 122eba1.

📒 Files selected for processing (1)
  • src/utils/json/mod.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/utils/json/mod.rs
✨ 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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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

github-actions bot commented Jun 3, 2025

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@vrongmeal
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

nitisht added a commit to parseablehq/.github that referenced this pull request Jun 3, 2025
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)
src/utils/json/strict.rs (1)

32-32: Fix grammar in comments.

Minor grammatical corrections needed in the comments.

Apply this diff to fix the grammar:

-                    // Number is a i64
+                    // Number is an i64

And at line 41:

-                    // Number is a u64
+                    // Number is a u64

Also applies to: 41-41

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ad12b8d and d1343dc.

📒 Files selected for processing (3)
  • src/handlers/http/ingest.rs (12 hunks)
  • src/utils/json/mod.rs (1 hunks)
  • src/utils/json/strict.rs (1 hunks)
🔇 Additional comments (9)
src/utils/json/mod.rs (1)

31-31: LGTM!

The module declaration correctly exposes the new strict JSON functionality.

src/utils/json/strict.rs (4)

6-17: Well-designed intermediate representation!

The InterimValue enum appropriately covers all JSON types including extended integers, and the use of Vec<(String, InterimValue)> for objects enables duplicate key detection.


54-62: Excellent duplicate key detection!

The implementation correctly detects duplicate keys during object conversion and returns an appropriate error message.


186-216: Clean and effective StrictValue implementation!

The two-phase deserialization approach (InterimValue → serde_json::Value) effectively enforces strict JSON parsing while maintaining compatibility with the existing serde_json ecosystem.


218-370: Comprehensive test coverage!

The test suite thoroughly covers all JSON types, nested structures, and most importantly, validates the duplicate key detection behavior that differentiates StrictValue from serde_json::Value.

src/handlers/http/ingest.rs (4)

41-41: Correct import update!

The import properly includes StrictValue from the strict module alongside the existing JsonFlattenError.


53-53: Clean integration of StrictValue!

The handler correctly receives JSON as StrictValue and extracts the inner value for processing, ensuring strict validation without disrupting the existing logic flow.

Also applies to: 85-86


159-159: Consistent StrictValue integration across all OTEL handlers!

All three OTEL ingestion handlers (logs, metrics, traces) follow the same pattern of receiving StrictValue and extracting the inner value, maintaining consistency across the codebase.

Also applies to: 209-215, 225-225, 273-279, 289-289, 338-344


131-132: Complete and consistent StrictValue adoption!

The remaining handlers (ingest_internal_stream and post_event) also correctly implement the StrictValue pattern, ensuring all ingestion endpoints enforce strict JSON validation.

Also applies to: 137-137, 355-355, 391-391

Copy link
Contributor

@nikhilsinhaparseable nikhilsinhaparseable left a comment

Choose a reason for hiding this comment

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

implementation looks good for ingestion over HTTP, but we still need something similar for ingestion from kafka and kinesis.

@vrongmeal
Copy link
Contributor Author

implementation looks good for ingestion over HTTP, but we still need something similar for ingestion from kafka and kinesis.

The same change can be made there, right? I'll add another commit here.

@vrongmeal
Copy link
Contributor Author

Spoke too soon, I would need a bit more idea on how the ingestion through kafka/kinesis works in code.

@nitisht
Copy link
Member

nitisht commented Jun 8, 2025

Spoke too soon, I would need a bit more idea on how the ingestion through kafka/kinesis works in code.

We can take up Kafka and Kinesis flows separately. If you're fine, I will merge this @vrongmeal

@vrongmeal
Copy link
Contributor Author

If you're fine, I will merge this

Of course! Can go ahead and merge this in

@nitisht nitisht merged commit caa9604 into parseablehq:main Jun 9, 2025
14 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.

Ingestion should fail when trying to ingest a log with duplicate keys
3 participants