-
Notifications
You must be signed in to change notification settings - Fork 611
feat: support get plugin schema based on schema_type #651
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
Changes from all commits
3b27564
a08dc39
33eeaf3
203d28d
921a6dc
f1d47ee
1741b19
4c985ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "regexp" | ||
|
|
||
| "github.com/xeipuuv/gojsonschema" | ||
| "go.uber.org/zap/buffer" | ||
|
|
@@ -87,19 +88,19 @@ func NewAPISIXJsonSchemaValidator(jsonPath string) (Validator, error) { | |
| }, nil | ||
| } | ||
|
|
||
| func getPlugins(reqBody interface{}) map[string]interface{} { | ||
| func getPlugins(reqBody interface{}) (map[string]interface{}, string) { | ||
| switch reqBody.(type) { | ||
| case *entity.Route: | ||
| route := reqBody.(*entity.Route) | ||
| return route.Plugins | ||
| return route.Plugins, "schema" | ||
| case *entity.Service: | ||
| service := reqBody.(*entity.Service) | ||
| return service.Plugins | ||
| return service.Plugins, "schema" | ||
| case *entity.Consumer: | ||
| consumer := reqBody.(*entity.Consumer) | ||
| return consumer.Plugins | ||
| return consumer.Plugins, "consumer_schema" | ||
| } | ||
| return nil | ||
| return nil, "" | ||
| } | ||
|
|
||
| func cHashKeySchemaCheck(upstream *entity.UpstreamDef) error { | ||
|
|
@@ -230,14 +231,21 @@ func (v *APISIXJsonSchemaValidator) Validate(obj interface{}) error { | |
| } | ||
|
|
||
| //check plugin json schema | ||
| plugins := getPlugins(obj) | ||
| plugins, schemaType := getPlugins(obj) | ||
| //fix lua json.encode transform lua{properties={}} to json{"properties":[]} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can avoid this hack, Lua @nic-chen here is the API, take a look: https://github.com/openresty/lua-cjson/#encode_empty_table_as_object you can help @liuxiran to fix this |
||
| reg := regexp.MustCompile(`\"properties\":\[\]`) | ||
| if plugins != nil { | ||
| for pluginName, pluginConf := range plugins { | ||
| schemaDef := conf.Schema.Get("plugins." + pluginName).String() | ||
| var schemaDef string | ||
| schemaDef = conf.Schema.Get("plugins." + pluginName + "." + schemaType).String() | ||
| if (schemaDef == "" && schemaType == "consumer_schema") { | ||
| schemaDef = conf.Schema.Get("plugins." + pluginName + ".schema").String() | ||
| } | ||
| if schemaDef == "" { | ||
| return fmt.Errorf("scheme validate failed: schema not found, path: %s", "plugins."+pluginName) | ||
| } | ||
|
|
||
| schemaDef = reg.ReplaceAllString(schemaDef, `"properties":{}`) | ||
| s, err := gojsonschema.NewSchema(gojsonschema.NewStringLoader(schemaDef)) | ||
| if err != nil { | ||
| return fmt.Errorf("scheme validate failed: %w", err) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use
__indexto mock all of the share dicts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have submitted an issue: #671, we can fix this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it~!