• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wol-soft / php-json-schema-model-generator-production / 41222065034

31 Oct 2026 04:56PM UTC coverage: 38.961% (+1.0%) from 37.962%
41222065034

Pull #23

github

claude
Fix off-by-one in the instance pointer of nested tuple errors

InvalidTupleException is keyed by the 1-based tuple position (the message
reports the first element as "invalid tuple #1"), but passed that key
unchanged to setInstancePointerParent(). A JSON pointer addresses the
0-based array index, so every nested error pointed at the following
element: for the value [["a", "x"], "b"] of the tuple "pair" the error at
the second item of the first tuple was reported at /pair/1/2 instead of
/pair/0/1.

The pointer now uses position - 1. The message numbering and the keys
returned by getInvalidTuples() are unchanged. The key contracts of
InvalidTupleException (1-based) and InvalidItemException (0-based) are
documented, and the previous tuple pointer test is corrected: it used the
key 0, which a tuple exception is never given.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U1R7TJejqtBJGe1uPS2p1L
Pull Request #23: Fix off-by-one in the instance pointer of nested tuple errors

10 of 15 new or added lines in 4 files covered. (66.67%)

15 existing lines in 4 files now uncovered.

390 of 1001 relevant lines covered (38.96%)

2.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/src/Exception/ValidationException.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace PHPModelGenerator\Exception;
6

7
use PHPModelGenerator\Attributes\JsonPointer;
8
use Throwable;
9

10
/**
11
 * Class ValidationException
12
 *
13
 * @package PHPModelGeneratorException
14
 */
15
abstract class ValidationException extends JSONModelValidationException
16
{
17
    private string $instancePointerSegment;
18
    private ?ValidationException $instancePointerParent = null;
19

20
    /**
21
     * ValidationException constructor.
22
     *
23
     * @param int $code
24
     */
25
    public function __construct(
39 ✔
26
        string $message,
27
        protected string $propertyName,
28
        protected mixed $providedValue,
29
        protected string $jsonPointer,
30
        $code = 0,
31
        ?Throwable $previous = null
32
    ) {
33
        parent::__construct($message, $code, $previous);
39 ✔
34

35
        $this->instancePointerSegment = '/' . self::escapeInstancePointerSegment($propertyName);
39 ✔
36
    }
37

UNCOV
38
    public function getPropertyName(): string
×
39
    {
UNCOV
40
        return $this->propertyName;
×
41
    }
42

43
    /**
44
     * @return mixed
45
     */
UNCOV
46
    public function getProvidedValue()
×
47
    {
UNCOV
48
        return $this->providedValue;
×
49
    }
50

51
    /**
52
     * The JSON pointer to the schema location of the constraint that rejected the value.
53
     */
UNCOV
54
    public function getJsonPointer(): JsonPointer
×
55
    {
UNCOV
56
        return new JsonPointer($this->jsonPointer);
×
57
    }
58

59
    /**
60
     * The JSON pointer (RFC 6901) to this value's location within the validated data instance
61
     * (e.g. "/goals/0/kind"), as opposed to getJsonPointer() which points into the schema.
62
     *
63
     * Resolved lazily by walking the parent chain set up via setInstancePointerParent(), so a
64
     * wrapper added after this exception was constructed (e.g. an array item later found to be
65
     * nested inside another array) is still reflected correctly.
66
     */
67
    public function getInstancePointer(): JsonPointer
16 ✔
68
    {
69
        return new JsonPointer(
16 ✔
70
            ($this->instancePointerParent?->getInstancePointer()->pointer ?? '') . $this->instancePointerSegment
16 ✔
71
        );
16 ✔
72
    }
73

74
    /**
75
     * Link this exception's instance pointer to continue from an enclosing exception's, as
76
     * exceptions bubble up from the point of failure to the root. Called by wrapping exceptions
77
     * (InvalidItemException, InvalidTupleException, InvalidAdditionalPropertiesException,
78
     * InvalidPatternPropertiesException, NestedObjectException, InvalidComposedValueException,
79
     * ConditionalException) on the nested exceptions they collect.
80
     *
81
     * Pass $segment when this exception's own propertyName is a placeholder shared by every
82
     * instance of an enclosing collection (an array item, an additional/pattern property) rather
83
     * than this value's real position — it replaces the auto-seeded segment with the caller's
84
     * actual position (an array index, the real property key). Omit it when this exception's own
85
     * propertyName is already a real, distinct name (composition branches, conditional branches,
86
     * and nested object properties all keep their own name and only need the parent link).
87
     */
88
    public function setInstancePointerParent(ValidationException $parent, int|string|null $segment = null): void
19 ✔
89
    {
90
        $this->instancePointerParent = $parent;
19 ✔
91

92
        if ($segment !== null) {
19 ✔
93
            $this->instancePointerSegment = '/' . self::escapeInstancePointerSegment((string) $segment);
16 ✔
94
        }
95
    }
96

97
    /**
98
     * Clear this exception's own instance pointer segment, leaving only whatever a parent link
99
     * (set via setInstancePointerParent()) contributes. Used by exceptions whose propertyName is
100
     * a class-name placeholder rather than a real instance-path segment: base validators that
101
     * validate the current object itself (additionalProperties/patternProperties checks) run
102
     * inside the generated class's own code, with no access to the property name their parent
103
     * used to reach this object, so they fall back to the class name purely for message text.
104
     */
105
    protected function suppressOwnInstancePointerSegment(): void
2 ✔
106
    {
107
        $this->instancePointerSegment = '';
2 ✔
108
    }
109

110
    private static function escapeInstancePointerSegment(string $segment): string
39 ✔
111
    {
112
        return str_replace(['~', '/'], ['~0', '~1'], $segment);
39 ✔
113
    }
114
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc