package builder import ( "fmt" "net/http" "testing" "time" "github.com/google/jsonapi" "github.com/invopop/jsonschema" "github.com/stretchr/testify/assert" "github.com/xlgmokha/openapi/test" ) type User struct { ID string `json:"id" jsonapi:"primary,users"` Name string `json:"name" jsonapi:"attr,name"` CreatedAt time.Time `json:"createdAt" jsonapi:"attr,createdAt,iso8601"` UpdatedAt time.Time `json:"updatedAt" jsonapi:"attr,updatedAt,iso8601"` } func TestBuilder(t *testing.T) { t.Run("builds a minimal spec", func(t *testing.T) { spec := New("The API", "0.1.0").Build() assert.Equal(t, test.Fixture("minimal.json"), spec.ToJSON(), ) }) t.Run("adds a path", func(t *testing.T) { userSchema := jsonschema.Reflect([]User{}) userJsonAPISchema := jsonschema.Reflect(&jsonapi.OnePayload{}) querySchema := &jsonschema.Schema{ Maximum: 256, Minimum: 2, Type: "string", } builder := New("The API", "0.1.0") builder.AddPath("/users").Get( "List all the users", "List all the users", WithParameter("query", "q", querySchema), WithResponse(http.StatusOK, jsonapi.MediaType, userJsonAPISchema), WithResponse(http.StatusOK, "application/json", userSchema), ) spec := builder.Build() fmt.Printf("%v\n", spec.ToJSON()) assert.Equal(t, test.Fixture("users.json"), spec.ToJSON(), ) }) }