MCPcopy Index your code
hub / github.com/keploy/keploy / DetectJSONDrift

Function DetectJSONDrift

pkg/agent/proxy/integrations/schemanoise/schemanoise.go:235–250  ·  view source on GitHub ↗

DetectJSONDrift compares a recorded request body against the live one and returns the "body."-prefixed field paths that drifted and are NOT already covered by known noise (root-relative: global/user ∪ already-learned, prefix stripped). isRecordedNoise, when non-nil, drops fields whose recorded value

(recordedBody, liveBody []byte, known map[string][]string, isRecordedNoise func(string) bool)

Source from the content-addressed store, hash-verified

233// real, non-learnable mismatch. This is the single JSON-diff kernel behind both
234// the HTTP and Pulsar schema-noise paths.
235func DetectJSONDrift(recordedBody, liveBody []byte, known map[string][]string, isRecordedNoise func(string) bool) (drift map[string][]string, comparable bool) {
236 if !json.Valid(recordedBody) || !json.Valid(liveBody) {
237 return nil, false
238 }
239 paths := matcher.ChangedJSONFieldPaths(string(recordedBody), string(liveBody), known, isRecordedNoise)
240 if len(paths) == 0 {
241 return nil, true
242 }
243 out := make(map[string][]string, len(paths))
244 for _, p := range paths {
245 // Empty regex list == "ignore this whole field", the same semantics as
246 // HTTPReq.ReqBodyNoise and TestCase.Noise.
247 out["body."+p] = []string{}
248 }
249 return out, true
250}
251
252// MergeLearned returns a fresh map combining already-recorded noise with newly
253// detected noise. Existing entries win on key collision (learned noise is

Callers 3

DiffMethod · 0.92
DiffMethod · 0.85
TestDetectJSONDriftFunction · 0.85

Calls 2

ChangedJSONFieldPathsFunction · 0.92
ValidMethod · 0.80

Tested by 1

TestDetectJSONDriftFunction · 0.68