feat: complete contract-driven business development and MySQL integration
This commit is contained in:
55
contract/documentation_test.go
Normal file
55
contract/documentation_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type documentationScalar string
|
||||
|
||||
var documentationDecodeCalls int
|
||||
|
||||
func (v documentationScalar) MarshalJSON() ([]byte, error) { return json.Marshal(string(v)) }
|
||||
func (v *documentationScalar) UnmarshalJSON(raw []byte) error {
|
||||
documentationDecodeCalls++
|
||||
var s string
|
||||
err := json.Unmarshal(raw, &s)
|
||||
*v = documentationScalar(s)
|
||||
return err
|
||||
}
|
||||
func TestDocumentationNeverRunsNestedScalarDecoders(t *testing.T) {
|
||||
type nested struct {
|
||||
ID documentationScalar `json:"id"`
|
||||
}
|
||||
type input struct {
|
||||
Value nested `json:"value" example:"{\"id\":\"123\"}"`
|
||||
}
|
||||
documentationDecodeCalls = 0
|
||||
scalar := ScalarOf[documentationScalar]("string", "string", false)
|
||||
if _, err := New[input](scalar); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Describe(reflect.TypeOf(input{}), scalar); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if documentationDecodeCalls != 0 {
|
||||
t.Fatal("metadata invoked business decoder")
|
||||
}
|
||||
type bad struct {
|
||||
Value nested `json:"value" example:"{\"id\":123}"`
|
||||
}
|
||||
if _, err := Describe(reflect.TypeOf(bad{}), scalar); err == nil {
|
||||
t.Fatal("invalid scalar wire example accepted")
|
||||
}
|
||||
type constrained struct {
|
||||
ID documentationScalar `json:"id"`
|
||||
Count int `json:"count,range=[1:3]"`
|
||||
}
|
||||
type invalidConstraint struct {
|
||||
Value constrained `json:"value" example:"{\"id\":\"123\",\"count\":9}"`
|
||||
}
|
||||
if _, err := Describe(reflect.TypeOf(invalidConstraint{}), scalar); err == nil {
|
||||
t.Fatal("nested scalar bypassed another field's example constraints")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user