(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)
| 208 | } |
| 209 | |
| 210 | func 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 | |
| 252 | func printAndRenderLogs(logs string, newLogger *pp.PrettyPrinter, logDiffs matcher.DiffsPrinter, logger *zap.Logger) error { |
| 253 | if _, err := newLogger.Printf(logs); err != nil { |
no test coverage detected