{"record":{"id":"eb7ce26fe60b57a6","repo":"microsoft/aspire","slug":"json-patch-remove-path-refers-to-missing-property","errorCode":null,"errorMessage":"JSON Patch remove path refers to missing property '{propertyName}'.","messagePattern":"JSON Patch remove path refers to missing property '(.+?)'\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/Dcp/JsonPatch.cs","lineNumber":247,"sourceCode":"                    ?? 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                }\n                break;\n            case \"replace\":\n                if (!target.ContainsKey(propertyName))\n                {\n                    throw new JsonException($\"JSON Patch replace path refers to missing property '{propertyName}'.\");\n                }\n                target[propertyName] = value?.DeepClone();\n                break;\n        }\n    }\n\n    private static void ApplyToArray(JsonArray target, string operation, string indexText, JsonNode? value)\n    {\n        if (operation == \"add\" && indexText == \"-\")\n        {\n            target.Add(value?.DeepClone());\n            return;","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/Dcp/JsonPatch.cs#L229-L265","documentation":"ApplyToObject implements the 'remove' operation for JsonObject targets. RFC 6902 requires that 'remove' only targets an existing member, so when the property is absent the library throws this JsonException instead of silently succeeding. This surfaces bugs in patch composition rather than masking them.","triggerScenarios":"Applying a remove operation with path '/annotations/foo' when the 'annotations' object has no 'foo' property (or 'annotations' resolution itself succeeded but the key is absent).","commonSituations":"Removing an annotation/label that was never set; idempotency mistakes when replaying patches; removing properties after a schema rename.","solutions":["Check the property exists (target.ContainsKey) or catch the JsonException and treat it as a no-op if removal is optional","Use a 'replace'/'add' with null or an empty value instead of remove when the property may not exist","Fix the path to the correct property name"],"exampleFix":"// before\nnew JsonPatchOperation(\"remove\", \"/metadata/annotations/debug\") // may not exist\n// after\nif (((JsonObject)doc.Root[\"metadata\"]![\"annotations\"]!).ContainsKey(\"debug\"))\n    new JsonPatchOperation(\"remove\", \"/metadata/annotations/debug\");","handlingStrategy":"validation","validationCode":"static bool CanRemove(JsonObject target, string propertyName) => target.ContainsKey(propertyName);","typeGuard":null,"tryCatchPattern":"try { patcher.Apply(doc, operations); }\ncatch (JsonException ex) when (ex.Message.Contains(\"remove path refers to missing property\")) {\n    logger.LogDebug(ex, \"Property already absent; treating remove as no-op\");\n}","preventionTips":["Check ContainsKey before issuing a remove","Treat 'remove of missing property' as idempotent success in retry loops","Confirm the property name against the resource schema"],"tags":["json-patch","remove-operation","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"}