-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi_hidden_test.go
More file actions
135 lines (110 loc) · 4.34 KB
/
openapi_hidden_test.go
File metadata and controls
135 lines (110 loc) · 4.34 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package fiberoapi
import (
"encoding/json"
"io"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Structs for testing openapi:"-" tag
type HiddenFieldInput struct {
Name string `json:"name" validate:"required"`
Internal string `json:"internal" openapi:"-"`
}
type HiddenFieldOutput struct {
ID int `json:"id"`
Name string `json:"name"`
Secret string `json:"secret" openapi:"-"`
}
type HiddenFieldError struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
}
type HiddenQueryInput struct {
Name string `query:"name"`
Hidden string `query:"hidden" openapi:"-"`
}
func TestOpenAPIHiddenField_ExcludedFromBodySchema(t *testing.T) {
app := fiber.New()
oapi := New(app)
Post(oapi, "/items", func(c *fiber.Ctx, input *HiddenFieldInput) (*HiddenFieldOutput, *HiddenFieldError) {
return &HiddenFieldOutput{ID: 1, Name: input.Name}, nil
}, OpenAPIOptions{
OperationID: "createItem",
Summary: "Create item",
Tags: []string{"items"},
})
oapi.SetupDocs()
req := httptest.NewRequest("GET", "/openapi.json", nil)
resp, err := app.Test(req)
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, 200, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var spec map[string]interface{}
require.NoError(t, json.Unmarshal(body, &spec))
components, ok := spec["components"].(map[string]interface{})
require.True(t, ok, "expected components section in OpenAPI spec")
schemas, ok := components["schemas"].(map[string]interface{})
require.True(t, ok, "expected schemas section in components")
// Check input schema — "internal" field should be hidden
inputSchema, ok := schemas["HiddenFieldInput"].(map[string]interface{})
require.True(t, ok, "expected HiddenFieldInput schema")
inputProps, ok := inputSchema["properties"].(map[string]interface{})
require.True(t, ok, "expected properties in HiddenFieldInput schema")
_, hasName := inputProps["name"]
_, hasInternal := inputProps["internal"]
assert.True(t, hasName, "visible field 'name' should be in schema")
assert.False(t, hasInternal, "hidden field 'internal' should NOT be in schema")
// Check output schema — "secret" field should be hidden
outputSchema, ok := schemas["HiddenFieldOutput"].(map[string]interface{})
require.True(t, ok, "expected HiddenFieldOutput schema")
outputProps, ok := outputSchema["properties"].(map[string]interface{})
require.True(t, ok, "expected properties in HiddenFieldOutput schema")
_, hasID := outputProps["id"]
_, hasSecret := outputProps["secret"]
assert.True(t, hasID, "visible field 'id' should be in schema")
assert.False(t, hasSecret, "hidden field 'secret' should NOT be in schema")
}
func TestOpenAPIHiddenField_ExcludedFromQueryParams(t *testing.T) {
app := fiber.New()
oapi := New(app)
Get(oapi, "/search", func(c *fiber.Ctx, input HiddenQueryInput) (*HiddenFieldOutput, *HiddenFieldError) {
return &HiddenFieldOutput{ID: 1, Name: input.Name}, nil
}, OpenAPIOptions{
OperationID: "searchItems",
Summary: "Search items",
})
oapi.SetupDocs()
req := httptest.NewRequest("GET", "/openapi.json", nil)
resp, err := app.Test(req)
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, 200, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
var spec map[string]interface{}
require.NoError(t, json.Unmarshal(body, &spec))
paths, ok := spec["paths"].(map[string]interface{})
require.True(t, ok, "expected paths section in OpenAPI spec")
searchPath, ok := paths["/search"].(map[string]interface{})
require.True(t, ok, "expected /search path in spec")
getOp, ok := searchPath["get"].(map[string]interface{})
require.True(t, ok, "expected get operation on /search")
params, ok := getOp["parameters"].([]interface{})
require.True(t, ok, "expected parameters array on /search get operation")
// Verify "name" is present and "hidden" is absent
foundName := false
for _, p := range params {
param, ok := p.(map[string]interface{})
require.True(t, ok, "expected parameter to be a map")
if param["name"] == "name" {
foundName = true
}
assert.NotEqual(t, "hidden", param["name"], "hidden query param should NOT appear in OpenAPI parameters")
}
assert.True(t, foundName, "visible query param 'name' should appear in OpenAPI parameters")
}