[bug]: contract generator fails for valid top-level JSON array bodies
๐ 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
- Go to the Keploy repository.
cd /Users/utkarshraj/Documents/opensource/keploy
- 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)
}
}
- 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
- 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
๐ป 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.
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