Skip to content

PathException with "The path does not apply to this data!" #79

Open
aevent-dev2 wants to merge 1 commit into
swaggest:masterfrom
automa8:master
Open

PathException with "The path does not apply to this data!" #79
aevent-dev2 wants to merge 1 commit into
swaggest:masterfrom
automa8:master

Conversation

@aevent-dev2

Copy link
Copy Markdown

The key in {"+1": ...} gets cast to 1.

…s like {"+1": ...} because filter_var('+1', FILTER_VALIDATE_INT) is 1.

@llamapreview llamapreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI Code Review by LlamaPReview

🎯 TL;DR & Recommendation

Recommendation: Approve with suggestions

This PR fixes a bug where filter_var('+1', FILTER_VALIDATE_INT) incorrectly returns integer 1, causing a PathException for JSON objects with keys like "+1". However, the fix fails to handle keys starting with '-' (e.g., -1), which could lead to similar path resolution issues.

📄 Documentation Diagram

This diagram illustrates the JSON pointer path resolution logic with the new sign-prefixed key handling.

sequenceDiagram
    participant U as User
    participant JP as JsonPointer
    participant D as Data
    U->>JP: resolve(path)
    loop each path item
        JP->>JP: filter_var(key, FILTER_VALIDATE_INT)
        alt intKey is false
            JP->>D: access as string key
        else intKey is not false
            JP->>JP: check for leading '+' or '-'?
            note over JP: PR #35;79: added check for sign prefix<br/>to avoid misinterpreting signed integer keys
            alt hasSign
                JP->>JP: set intKey = false
                JP->>D: access as string key
            else
                JP->>D: access as integer key
            end
        end
    end
Loading

🌟 Strengths

  • Includes a test case covering both positive and negative signed integer-like keys.
  • Directly addresses a real-world bug reported by a user.

💡 Suggestions (P2)

  • src/JsonPointer.php: Extend the sign-prefix check to include both '+' and '-' to prevent filter_var('-1', FILTER_VALIDATE_INT) from returning -1, which could cause path exceptions for objects with negative integer keys.

💡 Have feedback? We'd love to hear it in our GitHub Discussions.
✨ This review was generated by LlamaPReview Advanced, which is free for all open-source projects. Learn more.

Comment thread src/JsonPointer.php
Comment on lines +124 to +131
// Avoid PathException with objects like {"+1": ...}
$hasPlus = $intKey !== false &&
is_string($key) &&
$key !== '' &&
$key[0] === '+';
if ($hasPlus) {
$intKey = false;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 | Confidence: Medium

The fix correctly prevents filter_var('+1', FILTER_VALIDATE_INT) from returning integer 1, which would otherwise cause a PathException when the target data is an object (or an associative array treated as object). However, the same problem exists for keys that start with '-' (e.g., -1), because filter_var('-1', FILTER_VALIDATE_INT) returns integer -1. This integer can also lead to incorrect path resolution when the data is an object (e.g., $ref[-1] fails on a stdClass and may behave wrongly on arrays with string keys). The PR’s test includes a -1 scenario but does not confirm that the fix works for it; without handling the '-' prefix, the test may fail or behave inconsistently in certain data contexts (e.g., when the actual data is an object or an array with string keys). The impact is moderate: users with JSON objects whose keys are negative integers will still experience the original bug. A more complete fix should also set $intKey = false for keys starting with '-' (or any sign prefix followed by digits).

Code Suggestion:

// Avoid PathException with objects like {"+1": ...} or {"-1": ...}
$hasSign = $intKey !== false &&
    is_string($key) &&
    $key !== '' &&
    ($key[0] === '+' || $key[0] === '-');
if ($hasSign) {
    $intKey = false;
}

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.

1 participant