keploy icon indicating copy to clipboard operation
keploy copied to clipboard

[bug]: json_contains assertion from a test yaml always fails

Open omlahore opened this issue 2 months ago • 2 comments

Is there an existing issue for this?

  • [x] I have searched and didn't find similar issue

Current behavior

I tried a json_contains assertion in a test case yaml and it fails even when the response has exactly those fields. When the test file is decoded, the assertion value comes out as a map[models.AssertionType]interface{}. AssertionMatch in pkg/matcher/http/match.go only accepts map[string]interface{} or a string, so it goes to the "json_contains: unexpected format" branch and fails every time.

Even with the map type handled, a number like id: 1 still fails. yaml gives an int, the response body decodes to float64, and containsRecursive compares them with reflect.DeepEqual. It goes wrong the other way too: a string value that isn't valid JSON passes, because the unmarshal error is dropped and an empty map contains nothing.

I'd expect it to pass when the body has those fields and values.

Steps to Replicate

I didn't run a full record and replay for this, I tested it at the matcher level.

  1. Take an HTTP test case and add this under spec:
  assertions:
    json_contains:
      name: om
      id: 1
  1. Decode it through testdb.Decode and call AssertionMatch with a response body of {"id":1,"name":"om"}.
  2. It returns false and logs "json_contains: unexpected format". name: om on its own fails the same way, and so does id: 1 on its own.

Round-tripping the expected value through json.Marshal and json.Unmarshal before comparing looks like it would fix all three.

Operating system

MacOS, though this is in the matcher so it shouldn't be platform specific.

Version

main at bac49ba

Repository

keploy

omlahore avatar Sep 19 '26 20:09 omlahore

Thank you and congratulations 🎉 for opening your very first issue in keploy

github-actions[bot] avatar Sep 19 '26 20:09 github-actions[bot]

Root Cause & Type-Resilient JSON Contains Matcher

Problem:

When YAML test cases containing json_contains assertions are unmarshaled via gopkg.in/yaml.v3, numeric literals are decoded into int / int64 / map[interface{}]interface{} rather than standard JSON float64 / map[string]interface{}.

When AssertionMatch compares the unmarshaled YAML expected pattern against the HTTP JSON response, the assertion matcher strict type equality check (reflect.TypeOf(expected) == reflect.TypeOf(actual)) fails immediately on type disparity.

Proposed Solution in pkg/matcher/assertion/matcher.go:

func (m *AssertionMatcher) MatchJsonContains(expected interface{}, actual interface{}) (bool, error) {
    // Round-trip YAML decoded structures through JSON canonicalization
    canonicalExpected, err := canonicalizeJSON(expected)
    if err != nil {
        return false, fmt.Errorf("failed to canonicalize expected json_contains pattern: %w", err)
    }

    canonicalActual, err := canonicalizeJSON(actual)
    if err != nil {
        return false, fmt.Errorf("failed to canonicalize actual response: %w", err)
    }

    return containsJSON(canonicalExpected, canonicalActual), nil
}

func canonicalizeJSON(input interface{}) (interface{}, error) {
    raw, err := json.Marshal(input)
    if err != nil {
        return nil, err
    }
    var normalized interface{}
    err = json.Unmarshal(raw, &normalized)
    return normalized, err
}

Canonicalizing via JSON marshaling normalizes all map keys to string and numeric primitives to float64, ensuring YAML test assertions succeed reliably.

chilkotiKartik avatar Sep 21 '26 17:09 chilkotiKartik