keploy icon indicating copy to clipboard operation
keploy copied to clipboard

[bug]: contract generator fails for valid top-level JSON array bodies

Open Utkarshpandey0001 opened this issue 3 months ago โ€ข 1 comments

๐Ÿ‘€ Is there an existing issue for this?

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

๐Ÿ‘ Current behavior

The default contract generator fails when an HTTP request or response body is valid JSON but the root value is an array.

For example, this is a valid and common JSON response for list APIs:

[{"id":1,"name":"Utkarsh"}]

But HTTPDocToOpenAPI unmarshals the body into map[string]interface{}. That only works when the JSON root is an object, so array bodies fail with:

json: cannot unmarshal array into Go value of type map[string]interface {}

Expected behavior: Keploy should generate a valid OpenAPI schema with type: array and an items schema instead of failing.

๐Ÿ‘Ÿ Steps to Replicate

  1. Go to the Keploy repository.
cd /Users/utkarshraj/Documents/opensource/keploy
  1. Add this temporary repro test at pkg/service/contract/httpdoc_array_repro_test.go:
package contract

import (
	"testing"

	"go.keploy.io/server/v3/pkg/models"
	"go.uber.org/zap"
)

func TestHTTPDocToOpenAPIAllowsTopLevelArrayResponse_Repro(t *testing.T) {
	svc := &contract{}

	_, err := svc.HTTPDocToOpenAPI(zap.NewNop(), models.HTTPDoc{
		Version: "api.keploy.io/v1beta1",
		Kind:    "Http",
		Name:    "array-response",
		Spec: models.HTTPSchema{
			Request: models.HTTPReq{
				Method: "GET",
				URL:    "http://example.com/users",
			},
			Response: models.HTTPResp{
				StatusCode:    200,
				StatusMessage: "OK",
				Body:          `[{"id":1,"name":"Utkarsh"}]`,
			},
		},
	})

	if err != nil {
		t.Fatalf("top-level JSON array response should be valid OpenAPI input, got error: %v", err)
	}
}
  1. Run the targeted test.
mkdir -p .cache/go-build .cache/go-tmp

GOCACHE="$PWD/.cache/go-build" \
GOTMPDIR="$PWD/.cache/go-tmp" \
go test ./pkg/service/contract -run TestHTTPDocToOpenAPIAllowsTopLevelArrayResponse_Repro -count=1
  1. The test fails with the array unmarshal error.

๐Ÿ“œ Logs (if any)

--- FAIL: TestHTTPDocToOpenAPIAllowsTopLevelArrayResponse_Repro (0.00s)
    httpdoc_array_repro_test.go:31: top-level JSON array response should be valid OpenAPI input, got error: json: cannot unmarshal array into Go value of type map[string]interface {}
FAIL
FAIL    go.keploy.io/server/v3/pkg/service/contract
Image

๐Ÿ’ป Operating system

MacOS

๐Ÿงพ System Info (uname -a)

Darwin Utkarshs-MacBook-Air.local 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:05:14 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T8103 arm64

๐Ÿ“ฆ OS Release Info (cat /etc/os-release)

Not applicable

๐Ÿณ Docker Info (if applicable)

Not applicable. This is a unit-level contract generation issue and does not require Docker.

๐Ÿงฑ Your Environment

Local development machine.

๐ŸŽฒ Version

v2.5.2

๐Ÿ“ฆ Repository

keploy

๐Ÿค” What use case were you trying? (optional)

I was trying to generate an OpenAPI contract from recorded HTTP traffic. Some APIs commonly return top-level JSON arrays for list endpoints such as GET /users, GET /orders, or GET /items. Contract generation should support these valid responses.

Utkarshpandey0001 avatar Aug 19 '26 09:08 Utkarshpandey0001

Hi @slayerjain, I saw your comment on #4450 regarding the getType integer inference bug. The PR conversation is now locked, so I am responding here. I would be happy to work on it. Please open the issue and assign it to me. Thanks

Utkarshpandey0001 avatar Aug 23 '26 18:08 Utkarshpandey0001