in api/internal/handler/data_loader/route_export.go [326:429]
func ParseRoutePlugins(route *entity.Route, paramsRefs []*openapi3.ParameterRef, plugins map[string]interface{}, path openapi3.Operation, servicePlugins map[string]interface{}, secSchemas openapi3.SecuritySchemes, requestBody *openapi3.RequestBody) (openapi3.Operation, openapi3.SecuritySchemes, []*openapi3.ParameterRef, map[string]interface{}, error) {
if route.Plugins != nil {
param := &openapi3.Parameter{}
secReq := &openapi3.SecurityRequirements{}
// analysis plugins
for key, value := range route.Plugins {
// analysis request-validation plugin
if key == "request-validation" {
if valueMap, ok := value.(map[string]interface{}); ok {
if hsVal, ok := valueMap["header_schema"]; ok {
param.In = "header"
requestValidation := &entity.RequestValidation{}
reqBytes, _ := json.Marshal(&hsVal)
err := json.Unmarshal(reqBytes, requestValidation)
if err != nil {
log.Errorf("json marshal failed: %s", err)
}
for key1, value1 := range requestValidation.Properties.(map[string]interface{}) {
for _, arr := range requestValidation.Required {
if arr == key1 {
param.Required = true
}
}
param.Name = key1
typeStr := value1.(map[string]interface{})
schema := &openapi3.Schema{Type: typeStr["type"].(string)}
param.Schema = &openapi3.SchemaRef{Value: schema}
paramsRefs = append(paramsRefs, &openapi3.ParameterRef{Value: param})
}
}
if bsVal, ok := valueMap["body_schema"]; ok {
m := map[string]*openapi3.MediaType{}
reqBytes, _ := json.Marshal(&bsVal)
schema := &openapi3.Schema{}
err := json.Unmarshal(reqBytes, schema)
if err != nil {
log.Errorf("json marshal failed: %s", err)
}
// In the swagger format conversion, there are many cases of content type data format
// Such as (application/json, application/xml, text/xml) and more.
// There are many matching methods, such as equal, inclusive and so on.
// Therefore, the current processing method is to use "*/*" to match all
m["*/*"] = &openapi3.MediaType{Schema: &openapi3.SchemaRef{Value: schema}}
requestBody.Content = m
}
}
continue
}
// analysis security plugins
securityEnv := &openapi3.SecurityRequirement{}
switch key {
case string(KeyAuth):
secSchemas["api_key"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewCSRFSecurityScheme()}
securityEnv.Authenticate("api_key", " ")
secReq.With(*securityEnv)
continue
case string(BasicAuth):
secSchemas["basicAuth"] = &openapi3.SecuritySchemeRef{Value: &openapi3.SecurityScheme{
Type: "basicAuth",
Name: "basicAuth",
In: "header",
}}
securityEnv.Authenticate("basicAuth", " ")
secReq.With(*securityEnv)
continue
case string(JWTAuth):
secSchemas["bearerAuth"] = &openapi3.SecuritySchemeRef{Value: openapi3.NewJWTSecurityScheme()}
securityEnv.Authenticate("bearerAuth", " ")
secReq.With(*securityEnv)
continue
}
plugins[key] = value
}
path.Security = secReq
if route.ServiceID != nil && servicePlugins != nil {
_servicePlugins, err := json.Marshal(servicePlugins)
if err != nil {
log.Errorf("MapToJson err: ", err)
return path, nil, nil, nil, err
}
_plugins, err := json.Marshal(plugins)
if err != nil {
log.Errorf("MapToJson err: ", err)
return path, nil, nil, nil, err
}
bytePlugins, err := utils.MergeJson(_servicePlugins, _plugins)
if err != nil {
log.Errorf("Plugins MergeJson err: ", err)
return path, nil, nil, nil, err
}
err = json.Unmarshal(bytePlugins, &plugins)
if err != nil {
log.Errorf("JsonToMapDemo err: ", err)
return path, nil, nil, nil, err
}
}
} else if route.Plugins == nil && route.ServiceID != nil {
plugins = servicePlugins
}
return path, secSchemas, paramsRefs, plugins, nil
}