Skip to content

Commit 4b932bc

Browse files
committed
test
1 parent 23b952f commit 4b932bc

File tree

7 files changed

+66
-86
lines changed

7 files changed

+66
-86
lines changed

openapi.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,3 +1018,24 @@ components:
10181018
signing_secret:
10191019
type: string
10201020
minLength: 1
1021+
1022+
JsonschemaValidatorPluginConfiguration:
1023+
type: object
1024+
properties:
1025+
draft:
1026+
type: string
1027+
enum: ["6"]
1028+
default: "6"
1029+
default_schema:
1030+
type: string
1031+
format: jsonschema
1032+
maxLength: 1048576
1033+
schemas:
1034+
type: object
1035+
additionalProperties:
1036+
type: object
1037+
properties:
1038+
schema:
1039+
type: string
1040+
format: jsonschema
1041+
maxLength: 1048576

pkg/cache/redis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type RedisCache struct {
1616
func NewRedisCache(client *redis.Client) *RedisCache {
1717
return &RedisCache{
1818
c: client,
19-
s: serializer.Gob,
19+
s: serializer.JSON, // FIXME: breaking change (test)
2020
}
2121
}
2222

pkg/openapi/openapi.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package openapi
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"github.com/getkin/kin-openapi/openapi3"
78
"github.com/webhookx-io/webhookx/pkg/errs"
89
"strconv"
910
)
1011

12+
type FormatValidatorFunc[T any] func(T) error
13+
14+
func (fn FormatValidatorFunc[T]) Validate(value T) error { return fn(value) }
15+
16+
func init() {
17+
openapi3.DefineStringFormatValidator("jsonschema", FormatValidatorFunc[string](func(s string) error {
18+
schema := &openapi3.Schema{}
19+
if err := schema.UnmarshalJSON([]byte(s)); err != nil {
20+
return err
21+
}
22+
// compare to doc.Validat in LoadOpenAPI() ?
23+
if err := schema.Validate(context.TODO(), openapi3.EnableSchemaFormatValidation()); err != nil {
24+
return err
25+
}
26+
return nil
27+
}))
28+
}
29+
1130
func SetDefaults(schema *openapi3.Schema, defaults map[string]interface{}) error {
1231
data := make(map[string]interface{})
1332
_ = schema.VisitJSON(data,
@@ -126,9 +145,9 @@ func insertError(current map[string]interface{}, i int, paths []string, err *ope
126145
arr[index] = formatError(err)
127146
}
128147
} else {
129-
if err.Origin == nil {
130-
current[key] = formatError(err)
131-
}
148+
//if err.Origin == nil { // TODO???
149+
current[key] = formatError(err)
150+
//}
132151
}
133152
return
134153
}

plugins/jsonschema_validator/plugin.go

Lines changed: 12 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,37 @@ package jsonschema_validator
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
7-
"fmt"
86
"github.com/getkin/kin-openapi/openapi3"
9-
"github.com/webhookx-io/webhookx/pkg/errs"
7+
"github.com/webhookx-io/webhookx/db/entities"
108
"github.com/webhookx-io/webhookx/pkg/http/response"
119
"github.com/webhookx-io/webhookx/pkg/plugin"
1210
"github.com/webhookx-io/webhookx/pkg/types"
1311
"github.com/webhookx-io/webhookx/plugins/jsonschema_validator/jsonschema"
14-
"github.com/webhookx-io/webhookx/utils"
1512
)
1613

1714
type Config struct {
18-
Draft string `json:"draft" validate:"required,oneof=6 default:6"`
19-
DefaultSchema string `json:"default_schema" validate:"omitempty,json,max=1048576"`
20-
Schemas map[string]*Schema `json:"schemas" validate:"dive"`
15+
Draft string `json:"draft"`
16+
DefaultSchema string `json:"default_schema"`
17+
Schemas map[string]*Schema `json:"schemas"`
18+
}
19+
20+
func (c Config) Schema() *openapi3.Schema {
21+
return entities.LookupSchema("JsonschemaValidatorPluginConfiguration")
2122
}
2223

2324
type Schema struct {
24-
Schema string `json:"schema" validate:"omitempty,json,max=1048576"`
25+
Schema string `json:"schema"`
2526
}
2627

2728
type SchemaValidatorPlugin struct {
2829
plugin.BasePlugin[Config]
2930
}
3031

31-
func New(config []byte) (plugin.Plugin, error) {
32-
p := &SchemaValidatorPlugin{}
33-
p.Name = "jsonschema-validator"
34-
35-
if config != nil {
36-
if err := p.UnmarshalConfig(config); err != nil {
37-
return nil, err
38-
}
39-
}
40-
return p, nil
41-
}
42-
43-
func unmarshalAndValidateSchema(schema string) (*openapi3.Schema, error) {
44-
openapiSchema := &openapi3.Schema{}
45-
err := openapiSchema.UnmarshalJSON([]byte(schema))
46-
if err != nil {
47-
return nil, fmt.Errorf("value must be a valid jsonschema")
48-
}
49-
err = openapiSchema.Validate(context.Background(), openapi3.EnableSchemaFormatValidation())
50-
if err != nil {
51-
return openapiSchema, err
52-
}
53-
return openapiSchema, nil
54-
}
55-
56-
func (p *SchemaValidatorPlugin) ValidateConfig() error {
57-
err := utils.Validate(p.Config)
58-
if err != nil {
59-
return err
60-
}
61-
62-
e := errs.NewValidateError(errors.New("request validation"))
63-
64-
var defaultErr error
65-
if p.Config.DefaultSchema != "" {
66-
_, err := unmarshalAndValidateSchema(p.Config.DefaultSchema)
67-
if err != nil {
68-
defaultErr = err
69-
e.Fields = map[string]interface{}{
70-
"default_schema": err.Error(),
71-
}
72-
}
73-
}
74-
75-
for event, schema := range p.Config.Schemas {
76-
field := fmt.Sprintf("schemas[%s]", event)
77-
if schema == nil || schema.Schema == "" {
78-
if defaultErr != nil {
79-
e.Fields[field] = map[string]string{
80-
"schema": "invalid due to reusing the default_schema definition",
81-
}
82-
}
83-
} else {
84-
_, err = unmarshalAndValidateSchema(schema.Schema)
85-
if err != nil {
86-
e.Fields[field] = map[string]string{
87-
"schema": err.Error(),
88-
}
89-
}
90-
}
91-
}
92-
if len(e.Fields) > 0 {
93-
return e
94-
}
95-
return nil
32+
func (p *SchemaValidatorPlugin) Name() string {
33+
return "jsonschema-validator"
9634
}
9735

98-
func (p *SchemaValidatorPlugin) ExecuteInbound(inbound *plugin.Inbound) (res plugin.InboundResult, err error) {
36+
func (p *SchemaValidatorPlugin) ExecuteInbound(ctx context.Context, inbound *plugin.Inbound) (res plugin.InboundResult, err error) {
9937
var event map[string]any
10038
body := inbound.RawBody
10139
if err = json.Unmarshal(body, &event); err != nil {

test/cmd/admin_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
. "github.com/onsi/ginkgo/v2"
78
"github.com/stretchr/testify/assert"
@@ -107,7 +108,9 @@ var _ = Describe("admin", Ordered, func() {
107108
assert.EqualValues(GinkgoT(), map[string]interface{}{"function": "function handle() {}"}, plugins[0].Config)
108109
assert.Equal(GinkgoT(), `jsonschema-validator`, plugins[1].Name)
109110
assert.Equal(GinkgoT(), true, plugins[1].Enabled)
110-
assert.Equal(GinkgoT(), `{"draft": "6", "schemas": {"charge.succeeded": {"schema": "{\n \"type\": \"object\",\n \"properties\": {\n \"id\": { \"type\": \"string\" },\n \"amount\": { \"type\": \"integer\", \"minimum\": 1 },\n \"currency\": { \"type\": \"string\", \"minLength\": 3, \"maxLength\": 6 }\n },\n \"required\": [\"id\", \"amount\", \"currency\"]\n}\n"}}, "default_schema": "{\n \"type\": \"object\",\n \"properties\": {\n \"id\": { \"type\": \"string\" }\n },\n \"required\": [\"id\"]\n}\n"}`, string(plugins[1].Config))
111+
assert.JSONEq(GinkgoT(),
112+
`{"draft": "6", "schemas": {"charge.succeeded": {"schema": "{\n \"type\": \"object\",\n \"properties\": {\n \"id\": { \"type\": \"string\" },\n \"amount\": { \"type\": \"integer\", \"minimum\": 1 },\n \"currency\": { \"type\": \"string\", \"minLength\": 3, \"maxLength\": 6 }\n },\n \"required\": [\"id\", \"amount\", \"currency\"]\n}\n"}}, "default_schema": "{\n \"type\": \"object\",\n \"properties\": {\n \"id\": { \"type\": \"string\" }\n },\n \"required\": [\"id\"]\n}\n"}`,
113+
string(utils.Must(json.Marshal(plugins[1].Config))))
111114
})
112115

113116
It("entities not defined in the declarative configuration should be deleted", func() {

test/declarative/declarative_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ var _ = Describe("Declarative", Ordered, func() {
146146
assert.Nil(GinkgoT(), err)
147147
assert.Equal(GinkgoT(), 400, resp.StatusCode())
148148
assert.Equal(GinkgoT(),
149-
`{"message":"Request Validation","error":{"message":"request validation","fields":{"config":{"default_schema":"value must be a valid json string","draft":"required field missing","schemas[charge.succeed]":{"schema":"value must be a valid json string"}}}}}`,
149+
`{"message":"Request Validation","error":{"message":"request validation","fields":{"config":{"default_schema":"string doesn't match the format \"jsonschema\" (invalid character 'i' looking for beginning of value)","schemas":{"charge.succeed":{"schema":"string doesn't match the format \"jsonschema\" (invalid character 'i' looking for beginning of value)"}}}}}}`,
150150
string(resp.Body()))
151151
})
152152

@@ -159,7 +159,7 @@ var _ = Describe("Declarative", Ordered, func() {
159159
assert.Nil(GinkgoT(), err)
160160
assert.Equal(GinkgoT(), 400, resp.StatusCode())
161161
assert.Equal(GinkgoT(),
162-
`{"message":"Request Validation","error":{"message":"request validation","fields":{"config":{"default_schema":"unsupported 'type' value \"invlidObject\"","schemas[charge.succeed]":{"schema":"unsupported 'format' value \"invalid\""},"schemas[reuse.default_schema]":{"schema":"invalid due to reusing the default_schema definition"}}}}}`,
162+
`{"message":"Request Validation","error":{"message":"request validation","fields":{"config":{"default_schema":"string doesn't match the format \"jsonschema\" (unsupported 'type' value \"invlidObject\")","schemas":{"charge.succeed":{"schema":"string doesn't match the format \"jsonschema\" (unsupported 'format' value \"invalid\")"},"reuse.default_schema":"Value is not nullable"}}}}}`,
163163
string(resp.Body()))
164164
})
165165
})

test/plugins/jsonschema_validator_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ var _ = Describe("jsonschema-validator", Ordered, func() {
6969
db = helper.InitDB(true, &entitiesConfig)
7070
proxyClient = helper.ProxyClient()
7171

72-
app = utils.Must(helper.Start(map[string]string{
73-
"WEBHOOKX_ADMIN_LISTEN": "0.0.0.0:8080",
74-
"WEBHOOKX_PROXY_LISTEN": "0.0.0.0:8081",
75-
"WEBHOOKX_WORKER_ENABLED": "true",
76-
}))
72+
app = utils.Must(helper.Start(map[string]string{}))
73+
74+
err := helper.WaitForServer(helper.ProxyHttpURL, time.Second)
75+
assert.NoError(GinkgoT(), err)
7776
})
7877

7978
AfterAll(func() {

0 commit comments

Comments
 (0)