Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misuse of omitempty in jsonschema #1136

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use active json schema vendor
  • Loading branch information
xxx7xxxx committed Nov 13, 2023
commit 9361200db8e7c9c4580a513fa3174513d85b46cc
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/google/uuid v1.4.0
github.com/hashicorp/consul/api v1.26.1
github.com/hashicorp/golang-lru v1.0.2
github.com/invopop/jsonschema v0.12.0
github.com/invopop/yaml v0.2.0
github.com/jtblin/go-ldap-client v0.0.0-20170223121919-b73f66626b33
github.com/libdns/alidns v1.0.3
Expand All @@ -36,7 +37,6 @@ require (
github.com/libdns/vultr v1.0.0
github.com/megaease/easemesh-api v1.4.4
github.com/megaease/grace v1.0.0
github.com/megaease/jsonschema v0.5.1
github.com/megaease/yaml v0.0.0-20220804061446-4f18d6510aed
github.com/mitchellh/mapstructure v1.5.0
github.com/nacos-group/nacos-sdk-go v1.1.4
Expand All @@ -50,6 +50,7 @@ require (
github.com/quic-go/quic-go v0.40.0
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/rs/cors v1.10.1
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
Expand Down Expand Up @@ -94,6 +95,7 @@ require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.19.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect
Expand Down Expand Up @@ -125,6 +127,7 @@ require (
github.com/stretchr/objx v0.5.1 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/vultr/govultr/v3 v3.3.4 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.uber.org/mock v0.3.0 // indirect
Expand Down Expand Up @@ -207,7 +210,6 @@ require (
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
Expand Down
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.25.0/go.mod h1:S/LOQUeYDfJeJpFCIJDMj
github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/aws/smithy-go v1.16.0 h1:gJZEH/Fqh+RsvlJ1Zt4tVAtV6bKkp3cC+R6FCZMNzik=
github.com/aws/smithy-go v1.16.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down Expand Up @@ -459,15 +461,14 @@ github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
Expand Down Expand Up @@ -581,8 +582,6 @@ github.com/megaease/easemesh-api v1.4.4 h1:E18mtLfj8ffuPTeN7MqZeakJgT/tJ92JNIZsY
github.com/megaease/easemesh-api v1.4.4/go.mod h1:GuAE5DwqK6lI/ovoRKjyPxBCSoMhj0NLp9PRejj0Hnw=
github.com/megaease/grace v1.0.0 h1:b44R3j6e/iaN62F4ZUnru9nzL1VaIcxxUZjSPVtTVzI=
github.com/megaease/grace v1.0.0/go.mod h1:mOR6MVYQ6zGyuz9Y2or/VJ6QWueTL3erxWfIwyCmiIg=
github.com/megaease/jsonschema v0.5.1 h1:uTzdFo0YbRasSr/FW4pW4mjjzUkKuPHsj8Do4fGwycs=
github.com/megaease/jsonschema v0.5.1/go.mod h1:yZf5f6xi1re5otY+sx2liXGTzEK72UGDKe3Rg5O4cVE=
github.com/megaease/yaml v0.0.0-20220804061446-4f18d6510aed h1:e7bvqcldNRNTYg5rvnUkNZXt8/93wvdDVtDiyU+4YIY=
github.com/megaease/yaml v0.0.0-20220804061446-4f18d6510aed/go.mod h1:N67rkx57qPgnZ9Rvi5nf3SOLUe77vWiixhVbe2QdK+o=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
Expand Down Expand Up @@ -717,6 +716,8 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
Expand Down Expand Up @@ -753,7 +754,6 @@ github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -786,6 +786,8 @@ github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/vultr/govultr/v3 v3.3.4 h1:aj1eX0sRPVgEjNH/LzQwpuC5dA277kMLeGNw2GeJmoM=
github.com/vultr/govultr/v3 v3.3.4/go.mod h1:7NjuHeQv5vgUWR2H1sPc9D+xffrT5ql+kNi6R3yuwzo=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down
12 changes: 6 additions & 6 deletions pkg/object/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ type (

// Spec describes the Pipeline.
Spec struct {
Flow []FlowNode `json:"flow"`
Flow []FlowNode `json:"flow,omitempty"`
Filters []map[string]interface{} `json:"filters" jsonschema:"required"`
Resilience []map[string]interface{} `json:"resilience"`
Data map[string]interface{} `json:"data"`
Resilience []map[string]interface{} `json:"resilience,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}

// FlowNode describes one node of the pipeline flow.
FlowNode struct {
FilterName string `json:"filter" jsonschema:"required,format=urlname"`
FilterAlias string `json:"alias"`
Namespace string `json:"namespace"`
JumpIf map[string]string `json:"jumpIf"`
FilterAlias string `json:"alias,omitempty"`
Namespace string `json:"namespace,omitempty"`
JumpIf map[string]string `json:"jumpIf,omitempty"`
filter filters.Filter
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/supervisor/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type (
MetaSpec struct {
Name string `json:"name" jsonschema:"required,format=urlname"`
Kind string `json:"kind" jsonschema:"required"`
Version string `json:"version" jsonschema:"required"`
Version string `json:"version,omitempty"`

// RFC3339 format
CreatedAt string `json:"createdAt,omitempty"`
Expand Down
21 changes: 11 additions & 10 deletions pkg/v/v.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import (
"reflect"
"sync"

genjs "github.com/megaease/jsonschema"
loadjs "github.com/xeipuuv/gojsonschema"
genjs "github.com/invopop/jsonschema"
loadjs "github.com/santhosh-tekuri/jsonschema/v5"

"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/util/codectool"
)

Expand Down Expand Up @@ -75,19 +74,21 @@ func Validate(v interface{}) *ValidateRecorder {
vr.recordSystem(fmt.Errorf("marshal %#v to json failed: %v", v, err))
return vr
}
var rawValue interface{}
err = codectool.Unmarshal(jsonBuff, &rawValue)
if err != nil {
vr.recordSystem(fmt.Errorf("unmarshal json %s failed: %v", jsonBuff, err))
return vr
}

sm, err := getSchemaMeta(reflect.TypeOf(v))
if err != nil {
vr.recordSystem(fmt.Errorf("get schema meta for %T failed: %v", v, err))
return vr
}

docLoader := loadjs.NewBytesLoader(jsonBuff)
result, err := sm.loadSchema.Validate(docLoader)
if err != nil {
logger.Errorf("BUG: invalid schema: %v", err)
}
vr.recordJSONSchema(result)
err = sm.loadSchema.Validate(rawValue)
vr.recordJSONSchema(err)

val := reflect.ValueOf(v)
traverseGo(&val, nil, vr.record)
Expand Down Expand Up @@ -122,7 +123,7 @@ func getSchemaMeta(t reflect.Type) (*schemaMeta, error) {
return nil, fmt.Errorf("marshal %#v to json failed: %v", sm.loadSchema, err)
}

sm.loadSchema, err = loadjs.NewSchema(loadjs.NewBytesLoader(sm.jsonFormat))
sm.loadSchema, err = loadjs.CompileString(loadjs.Draft2020.String(), string(sm.jsonFormat))
if err != nil {
return nil, fmt.Errorf("new schema from %s failed: %v", sm.jsonFormat, err)
}
Expand Down
32 changes: 12 additions & 20 deletions pkg/v/validaterecorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"runtime/debug"
"strings"

loadjs "github.com/xeipuuv/gojsonschema"

"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/util/codectool"
)
Expand Down Expand Up @@ -76,9 +74,9 @@ type (
}
)

func (vr *ValidateRecorder) recordJSONSchema(result *loadjs.Result) {
for _, err := range result.Errors() {
vr.JSONSchemaErrs = append(vr.JSONSchemaErrs, err.String())
func (vr *ValidateRecorder) recordJSONSchema(err error) {
if err != nil {
vr.JSONSchemaErrs = append(vr.JSONSchemaErrs, err.Error())
}
}

Expand All @@ -93,22 +91,16 @@ func getFieldJSONName(field *reflect.StructField) string {
return fieldName
}

func requiredFromField(field *reflect.StructField) bool {
tags := strings.Split(field.Tag.Get("jsonschema"), ",")
switch {
case len(tags) == 0:
return false
case tags[0] == "-":
return false
default:
for _, tag := range tags {
if tag == "omitempty" {
return false
}
func IsOmitemptyField(field *reflect.StructField) bool {
tags := strings.Split(field.Tag.Get("json"), ",")

for _, tag := range tags {
if tag == "omitempty" {
return true
}
// NOTICE: Required by default.
return true
}

return false
}

func (vr *ValidateRecorder) record(val *reflect.Value, field *reflect.StructField) {
Expand All @@ -121,7 +113,7 @@ func (vr *ValidateRecorder) recordFormat(val *reflect.Value, field *reflect.Stru
return
}

if !requiredFromField(field) && val.IsZero() {
if IsOmitemptyField(field) {
return
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/v/validaterecorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func TestRequiredFromField(t *testing.T) {
typeOfT := val.Type()

idField, _ := typeOfT.FieldByName("ID")
assert.False(requiredFromField(&idField))
assert.True(IsOmitemptyField(&idField))

nameField, _ := typeOfT.FieldByName("Name")
assert.False(requiredFromField(&nameField))
assert.True(IsOmitemptyField(&nameField))

addrField, _ := typeOfT.FieldByName("Addr")
assert.True(requiredFromField(&addrField))
assert.True(IsOmitemptyField(&addrField))
}

func TestValidateRecorder(t *testing.T) {
Expand Down