{"record":{"id":"eaa24c9cf9c5fa1c","repo":"microsoft/aspire","slug":"json-patch-path-segment-segment-does-not-exist","errorCode":null,"errorMessage":"JSON Patch path segment '{segment}' does not exist.","messagePattern":"JSON Patch path segment '(.+?)' does not exist\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/Dcp/JsonPatch.cs","lineNumber":230,"sourceCode":"                throw new JsonException(\"A JSON Patch path can only traverse objects and arrays.\");\n        }\n\n        return current;\n    }\n\n    private static JsonNode GetParent(JsonNode? current, string[] segments)\n    {\n        var parent = current ?? throw new JsonException(\"A JSON Patch path cannot traverse a null value.\");\n\n        for (var index = 0; index < segments.Length - 1; index++)\n        {\n            var segment = segments[index];\n            parent = parent switch\n            {\n                JsonObject jsonObject when jsonObject.TryGetPropertyValue(segment, out var child) && child is not null => child,\n                JsonArray jsonArray => jsonArray[ParseArrayIndex(segment, jsonArray.Count, allowEnd: false)]\n                    ?? throw new JsonException($\"JSON Patch path segment '{segment}' refers to a null value.\"),\n                _ => throw new JsonException($\"JSON Patch path segment '{segment}' does not exist.\"),\n            };\n        }\n\n        return parent;\n    }\n\n    private static void ApplyToObject(JsonObject target, string operation, string propertyName, JsonNode? value)\n    {\n        switch (operation)\n        {\n            case \"add\":\n                target[propertyName] = value?.DeepClone();\n                break;\n            case \"remove\":\n                if (!target.Remove(propertyName))\n                {\n                    throw new JsonException($\"JSON Patch remove path refers to missing property '{propertyName}'.\");\n                }","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/Dcp/JsonPatch.cs#L212-L248","documentation":"GetParent resolves each intermediate segment of a JSON Patch path. When a segment names a property that does not exist on the current JsonObject (or is not a valid array index), traversal stops and this JsonException identifies the first missing segment. Per RFC 6902, intermediate path segments must exist.","triggerScenarios":"Calling Apply with a path like '/spec/template/metadata' where 'spec' or 'template' is absent from the document.","commonSituations":"Typos in patch paths; patching a resource whose schema lacks the expected section (e.g. no 'annotations' map yet); copying patch paths between resource kinds with different schemas.","solutions":["Add the missing intermediate object with an 'add' operation before the failing operation","Correct the segment spelling/path to match the document's actual shape","Verify the property exists with TryGetPropertyValue before patching"],"exampleFix":"// before\nnew JsonPatchOperation(\"replace\", \"/metadata/labels/app\", \"web\") // 'labels' missing\n// after\nnew JsonPatchOperation(\"add\", \"/metadata/labels\", new JsonObject()),\nnew JsonPatchOperation(\"add\", \"/metadata/labels/app\", \"web\")","handlingStrategy":"validation","validationCode":"static bool SegmentExists(JsonNode node, string segment) => node switch {\n    JsonObject o => o.TryGetPropertyValue(segment, out _),\n    JsonArray a => int.TryParse(segment, out var i) && i >= 0 && i < a.Count,\n    _ => false\n};","typeGuard":null,"tryCatchPattern":"try { patcher.Apply(doc, operations); }\ncatch (JsonException ex) when (ex.Message.Contains(\"does not exist\")) {\n    logger.LogWarning(ex, \"Patch path segment missing; add the intermediate object first\");\n}","preventionTips":["Validate patch paths against the document before applying","Prefix deep patches with 'add' operations that create intermediate objects","Keep a schema reference for each resource kind to avoid path typos"],"tags":["json-patch","missing-property","json"],"backgroundTag":"record-not-found","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}