1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
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(),
)
})
}
|