MCPcopy
hub / github.com/keploy/keploy / handleJSONDiff

Function handleJSONDiff

pkg/matcher/schema/match.go:210–250  ·  view source on GitHub ↗
(validatedJSON matcher.ValidatedJSON, logDiffs matcher.DiffsPrinter, newLogger *pp.PrettyPrinter, logger *zap.Logger, _ string, _ string, _ string, _ string, mockBodyStr string, testBodyStr string, diffType string, mode models.SchemaMatchMode)

Source from the content-addressed store, hash-verified

208}
209
210func handleJSONDiff(validatedJSON matcher.ValidatedJSON, logDiffs matcher.DiffsPrinter, newLogger *pp.PrettyPrinter, logger *zap.Logger, _ string, _ string, _ string, _ string, mockBodyStr string, testBodyStr string, diffType string, mode models.SchemaMatchMode) (float64, bool, error) {
211 pass := true
212 differencesCount := 0.0
213 jsonComparisonResult, err := matcher.JSONDiffWithNoiseControl(validatedJSON, nil, false)
214 if err != nil {
215 return differencesCount, false, err
216 }
217 if !jsonComparisonResult.IsExact() {
218 pass = false
219 // logs := newLogger.Sprintf("Contract Check failed for test: %s (%s) / mock: %s (%s) \n\n--------------------------------------------------------------------\n\n", testName, testSetID, mockName, mockSetID)
220 if json.Valid([]byte(mockBodyStr)) {
221 patch, err := jsondiff.Compare(testBodyStr, mockBodyStr)
222 if err != nil {
223 logger.Debug("failed to compute json diff", zap.Error(err))
224 return differencesCount, false, err
225 }
226 differencesCount = float64(len(patch))
227 if diffType == "request" && differencesCount > 1 {
228 return -1.0, false, nil
229 }
230 if diffType == "response" {
231 for _, op := range patch {
232 if jsonComparisonResult.Matches() {
233 logDiffs.SetHasarrayIndexMismatch(true)
234 logDiffs.PushFooterDiff(strings.Join(jsonComparisonResult.Differences(), ", "))
235 }
236
237 logDiffs.PushBodyDiff(fmt.Sprint(op.OldValue), fmt.Sprint(op.Value), nil)
238
239 }
240 }
241 }
242 if diffType == "response" && mode == models.CompareMode {
243 if err := printAndRenderLogs("", newLogger, logDiffs, logger); err != nil {
244 return differencesCount, false, err
245 }
246
247 }
248 }
249 return differencesCount, pass, nil
250}
251
252func printAndRenderLogs(logs string, newLogger *pp.PrettyPrinter, logDiffs matcher.DiffsPrinter, logger *zap.Logger) error {
253 if _, err := newLogger.Printf(logs); err != nil {

Callers 2

compareRequestBodiesFunction · 0.85
compareResponseBodiesFunction · 0.85

Calls 12

JSONDiffWithNoiseControlFunction · 0.92
printAndRenderLogsFunction · 0.85
IsExactMethod · 0.80
ValidMethod · 0.80
MatchesMethod · 0.80
PushFooterDiffMethod · 0.80
DifferencesMethod · 0.80
PushBodyDiffMethod · 0.80
CompareMethod · 0.65
DebugMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected