ShiningRush commented on a change in pull request #904:
URL: https://github.com/apache/apisix-dashboard/pull/904#discussion_r533846077



##########
File path: api/internal/core/store/validate.go
##########
@@ -238,22 +238,37 @@ func (v *APISIXJsonSchemaValidator) Validate(obj 
interface{}) error {
                return err
        }
 
+       pluginDisableSchema := map[string]map[string]interface{}{
+               "disable": {"type": "boolean"},
+       }
        plugins, schemaType := getPlugins(obj)
-       //fix lua json.encode transform lua{properties={}} to 
json{"properties":[]}
-       reg := regexp.MustCompile(`\"properties\":\[\]`)
        for pluginName, pluginConf := range plugins {
-               var schemaDef string
-               schemaDef = conf.Schema.Get("plugins." + pluginName + "." + 
schemaType).String()
-               if schemaDef == "" && schemaType == "consumer_schema" {
-                       schemaDef = conf.Schema.Get("plugins." + pluginName + 
".schema").String()
+               schemaValue := conf.Schema.Get("plugins." + pluginName + "." + 
schemaType).Value()
+               if schemaValue == nil && schemaType == "consumer_schema" {
+                       schemaValue = conf.Schema.Get("plugins." + pluginName + 
".schema").Value()
                }
-               if schemaDef == "" {
-                       log.Warnf("schema validate failed: schema not found, 
path: %s", "plugins."+pluginName)
+               if schemaValue == nil {
+                       log.Warnf("schema validate failed: schema not found,  
%s, %s", "plugins."+pluginName, schemaType)
                        return fmt.Errorf("schema validate failed: schema not 
found, path: %s", "plugins."+pluginName)
                }
+               schemaMap := schemaValue.(map[string]interface{})
+               if schemaMap["type"] != nil && schemaMap["type"].(string) == 
"object" {
+                       if properties, ok := schemaMap["properties"]; ok {
+                               propertiesMap := 
properties.(map[string]interface{})
+                               if len(propertiesMap) == 0 {
+                                       schemaMap["properties"] = 
pluginDisableSchema
+                               }
+                       } else {
+                               schemaMap["properties"] = pluginDisableSchema
+                       }
+               }
+               schemaByte, err := json.Marshal(schemaMap)
+               if err != nil {
+                       log.Warnf("scheme validate failed: schema not found, 
path: %s, %w", "plugins."+pluginName, err)
+                       return fmt.Errorf("scheme validate failed: schema not 
found, path: %s, %w", "plugins."+pluginName, err)

Review comment:
       error info is wrong, here is a json.Marshal action.

##########
File path: api/internal/core/store/validate.go
##########
@@ -238,22 +238,37 @@ func (v *APISIXJsonSchemaValidator) Validate(obj 
interface{}) error {
                return err
        }
 
+       pluginDisableSchema := map[string]map[string]interface{}{
+               "disable": {"type": "boolean"},
+       }
        plugins, schemaType := getPlugins(obj)
-       //fix lua json.encode transform lua{properties={}} to 
json{"properties":[]}
-       reg := regexp.MustCompile(`\"properties\":\[\]`)
        for pluginName, pluginConf := range plugins {
-               var schemaDef string
-               schemaDef = conf.Schema.Get("plugins." + pluginName + "." + 
schemaType).String()
-               if schemaDef == "" && schemaType == "consumer_schema" {
-                       schemaDef = conf.Schema.Get("plugins." + pluginName + 
".schema").String()
+               schemaValue := conf.Schema.Get("plugins." + pluginName + "." + 
schemaType).Value()
+               if schemaValue == nil && schemaType == "consumer_schema" {
+                       schemaValue = conf.Schema.Get("plugins." + pluginName + 
".schema").Value()
                }
-               if schemaDef == "" {
-                       log.Warnf("schema validate failed: schema not found, 
path: %s", "plugins."+pluginName)
+               if schemaValue == nil {
+                       log.Warnf("schema validate failed: schema not found,  
%s, %s", "plugins."+pluginName, schemaType)
                        return fmt.Errorf("schema validate failed: schema not 
found, path: %s", "plugins."+pluginName)
                }
+               schemaMap := schemaValue.(map[string]interface{})
+               if schemaMap["type"] != nil && schemaMap["type"].(string) == 
"object" {
+                       if properties, ok := schemaMap["properties"]; ok {

Review comment:
       Too much branch this logic, we can refactor to
   ```go
   // guard code
   if schemaMap["properties"] == nil {
       schemaMap["properties"]  = make(map[string]interface{})
   }
   // ensure
   if len(schemaMap["properties"].(map[string]interface{})) == 0 {
     schemaMap["properties"] = pluginDisableSchema
   }
   ```
   BTW: I am curious if it is expected logic that we do nothing when user 
specified `properties` even it does not contain `disable`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to