Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion api/build-tools/schema-sync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ ngx.timer = {}
ngx.location = {}
ngx.socket = {}
ngx.re.gmatch = empty_function
ngx.shared = {
["plugin-api-breaker"] = {}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use __index to mock all of the share dicts.

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it~!

}

-- additional define for management
local time_def = {
Expand Down Expand Up @@ -127,7 +130,12 @@ local plugins = get_plugin_list()
for idx, plugin_name in pairs(plugins) do
local plugin = require("apisix.plugins." .. plugin_name)
if plugin and type(plugin) == "table" and plugin.schema then
schema_all.plugins[plugin_name] = plugin.schema
schema_all.plugins[plugin_name]= {
['schema'] = plugin.schema
}
end
if plugin and type(plugin) == "table" and plugin.consumer_schema then
schema_all.plugins[plugin_name]['consumer_schema'] = plugin.consumer_schema
end
end

Expand Down
2 changes: 1 addition & 1 deletion api/conf/schema.json

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions api/internal/core/store/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"regexp"

"github.com/xeipuuv/gojsonschema"
"go.uber.org/zap/buffer"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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":[]}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can avoid this hack, Lua json.encode can do this in a good way.

@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)
Expand Down
12 changes: 11 additions & 1 deletion api/internal/handler/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ func (h *Handler) ApplyRoute(r *gin.Engine) {

type GetInput struct {
Name string `auto_read:"name,path" validate:"required"`
SchemaType string `auto_read:"schema_type,query"`
}

func (h *Handler) Schema(c droplet.Context) (interface{}, error) {
input := c.Input().(*GetInput)
ret := conf.Schema.Get("plugins." + input.Name).Value()

var ret interface{}
if input.SchemaType == "consumer" {
ret = conf.Schema.Get("plugins." + input.Name + ".consumer_schema").Value()
if ret == nil {
ret = conf.Schema.Get("plugins." + input.Name + ".schema").Value()
}
} else {
ret = conf.Schema.Get("plugins." + input.Name + ".schema").Value()
}
return ret, nil
}

Expand Down
54 changes: 47 additions & 7 deletions api/internal/handler/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,60 @@ func TestPlugin(t *testing.T) {
//schema
input := &GetInput{}
reqBody := `{
"name": "limit-count"
}`
"name": "limit-count"
}`
json.Unmarshal([]byte(reqBody), input)
ctx.SetInput(input)
val, _ := handler.Schema(ctx)
assert.NotNil(t, val)

//not exists
input2 := &GetInput{}
reqBody = `{
"name": "not-exists"
}`
json.Unmarshal([]byte(reqBody), input2)
ctx.SetInput(input2)
"name": "not-exists"
}`
json.Unmarshal([]byte(reqBody), input)
ctx.SetInput(input)
val, _ = handler.Schema(ctx)
assert.Nil(t, val)

/*
get plugin schema with schema_type: consumer
plugin has consumer_schema
return plugin`s consumer_schema
*/
reqBody = `{
"name": "jwt-auth",
"schema_type": "consumer"
}`
json.Unmarshal([]byte(reqBody), input)
ctx.SetInput(input)
val, _ = handler.Schema(ctx)
assert.NotNil(t, val)

/*
get plugin schema with schema_type: consumer
plugin does not have consumer_schema
return plugin`s schema
*/
reqBody = `{
"name": "limit-count",
"schema_type": "consumer"
}`
json.Unmarshal([]byte(reqBody), input)
ctx.SetInput(input)
val, _ = handler.Schema(ctx)
assert.NotNil(t, val)

/*
get plugin schema with wrong schema_type: type,
return plugin`s schema
*/
reqBody = `{
"name": "jwt-auth",
"schema_type": "type"
}`
json.Unmarshal([]byte(reqBody), input)
ctx.SetInput(input)
val, _ = handler.Schema(ctx)
assert.NotNil(t, val)
}
4 changes: 3 additions & 1 deletion web/src/pages/Consumer/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ const Page: React.FC = (props) => {
</Steps>

{step === 1 && <Step1 form={form1} />}
{step === 2 && <PluginPage initialData={plugins} onChange={setPlugins} />}
{step === 2 && (
<PluginPage initialData={plugins} onChange={setPlugins} schemaType="consumer" />
)}
{step === 3 && <Preview form1={form1} plugins={plugins} />}
</Card>
</PageContainer>
Expand Down