You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When generating a Pact from a Golang Struct that contains a UUID field, I expect the resulting Pact to contain a String version of the UUID.
Actual behaviour
The resulting Pact contains an array of bytes, which is the UUID fields underlying representation.
Steps to reproduce
The following code is an example on how to reproduce, and the resulting Pact file is below (note the body - ID field)
import (
"fmt"
"github.com/google/uuid"
"github.com/pact-foundation/pact-go/dsl"
"log"
"net/http"
"testing"
)
type Foo struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
func TestGet(t *testing.T) {
// Create Pact connecting to local Daemon
pact := &dsl.Pact{
Consumer: "my-consumer",
Provider: "my-provider",
Host: "localhost",
}
defer pact.Teardown()
// Pass in test case. This is the component that makes the external HTTP call
var test = func() (err error) {
u := fmt.Sprintf("http://localhost:%d/foobar", pact.Server.Port)
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return
}
// NOTE: by default, request bodies are expected to be sent with a Content-Type
// of application/json. If you don't explicitly set the content-type, you
// will get a mismatch during Verification.
req.Header.Set("Content-Type", "application/json")
_, err = http.DefaultClient.Do(req)
return
}
// Set up our expected interactions.
pact.
AddInteraction().
Given("Data exists").
UponReceiving("A request to get").
WithRequest(dsl.Request{
Method: "get",
Path: dsl.String("/foobar"),
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
}).
WillRespondWith(dsl.Response{
Status: 200,
Headers: dsl.MapMatcher{"Content-Type": dsl.String("application/json")},
Body: dsl.Match(&Foo{}),
})
// Run the test, verify it did what we expected and capture the contract
if err := pact.Verify(test); err != nil {
log.Fatalf("Error on Verify: %v", err)
}
}
Thanks. I don't think it's appropriate to support the UUID directly, but I think the ability to override the default serialisation for non primitives would be better e.g.
type Foo struct {
ID uuid.UUID `json:"id", pact:"match=type,example=27cacc3f-a6ca-4b6d-98f9-c9f7e4e78c07`
Name string `json:"name"`
Description string `json:"description"`
}
Would that work?
In the meantime, you can just fall back to standard matchers, e.g. something like the below:
...
Body: map[string]interface{}{
"uuid": dsl.Like("27cacc3f-a6ca-4b6d-98f9-c9f7e4e78c07"),
"name": dsl.Like("Baz"),
"description": dsl.Like("some user called Baz"),
}
@mefellows Yep totally fine with that option, makes it usable for other non-primitive types that people might be deserializing to.
Ideally just want something where I can re-use the same struct in both my Pact tests and my core code, but will use the workaround in the meantime.
Software versions
Expected behaviour
When generating a Pact from a Golang Struct that contains a UUID field, I expect the resulting Pact to contain a String version of the UUID.
Actual behaviour
The resulting Pact contains an array of bytes, which is the UUID fields underlying representation.
Steps to reproduce
The following code is an example on how to reproduce, and the resulting Pact file is below (note the body - ID field)
The text was updated successfully, but these errors were encountered: