Skylm808 commented on code in PR #948:
URL: https://github.com/apache/dubbo-go-pixiu/pull/948#discussion_r3342246833


##########
pkg/filter/http/openapi/openapi.go:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package openapi
+
+import (
+       "net/http"
+       "os"
+       "path/filepath"
+       "strings"
+)
+
+import (
+       "github.com/pb33f/libopenapi"
+       openapiValidator "github.com/pb33f/libopenapi-validator"
+       validatorConfig "github.com/pb33f/libopenapi-validator/config"
+       validatorErrors "github.com/pb33f/libopenapi-validator/errors"
+       validatorPaths "github.com/pb33f/libopenapi-validator/paths"
+       "github.com/pb33f/libopenapi/datamodel"
+       v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
+
+       "github.com/pkg/errors"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+       "github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
+       contexthttp "github.com/apache/dubbo-go-pixiu/pkg/context/http"
+       "github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+       // Kind is the kind of OpenAPI validation filter.
+       Kind = constant.HTTPOpenAPIFilter
+)
+
+func init() {
+       filter.RegisterHttpFilter(&Plugin{})
+}
+
+type (
+       Plugin struct {
+       }
+
+       FilterFactory struct {
+               cfg       *Config
+               validator openapiValidator.Validator
+               model     *v3.Document
+       }
+
+       Filter struct {
+               validator openapiValidator.Validator
+               model     *v3.Document
+       }
+
+       Config struct {
+               Path string `yaml:"path" json:"path,omitempty"`
+       }
+)
+
+func (p *Plugin) Kind() string {
+       return Kind
+}
+
+func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) {
+       return &FilterFactory{cfg: &Config{}}, nil
+}
+
+func (factory *FilterFactory) Config() any {
+       return factory.cfg
+}
+
+func (factory *FilterFactory) Apply() error {
+       path := strings.TrimSpace(factory.cfg.Path)
+       if path == "" {
+               return errors.New("openapi path is required")
+       }
+       factory.cfg.Path = path
+
+       validator, model, err := loadValidatorFromFile(path)
+       if err != nil {
+               return err
+       }
+       factory.validator = validator
+       factory.model = model
+       return nil
+}
+
+func (factory *FilterFactory) PrepareFilterChain(ctx *contexthttp.HttpContext, 
chain filter.FilterChain) error {
+       f := &Filter{
+               validator: factory.validator,
+               model:     factory.model,
+       }
+       chain.AppendDecodeFilters(f)
+       return nil
+}
+
+func (f *Filter) Decode(ctx *contexthttp.HttpContext) filter.FilterStatus {
+       if f.validator == nil || f.model == nil {
+               return filter.Continue
+       }
+
+       req := ctx.Request
+       pathItem, foundPath, ok := f.findRequestOperation(req)
+       if !ok {
+               return filter.Continue
+       }
+
+       if valid, validationErrs := 
f.validator.ValidateHttpRequestSyncWithPathItem(req, pathItem, foundPath); 
!valid {
+               errResp := 
contexthttp.BadRequest.WithError(errors.New(formatValidationErrors(validationErrs)))
+               ctx.SendLocalReply(errResp.Status, errResp.ToJSON())
+               logger.Debug(errResp.Error())
+               return filter.Stop
+       }
+       return filter.Continue
+}
+
+func (f *Filter) findRequestOperation(req *http.Request) (*v3.PathItem, 
string, bool) {
+       pathItem, validationErrs, foundPath := validatorPaths.FindPath(req, 
f.model, nil)
+       if len(validationErrs) > 0 || pathItem == nil {

Review Comment:
   同上,这个问题已在同一处修复:BuildV3Model() 返回 error 时会中止初始化,不再使用 partial model,并补了 
invalid $ref 回归测试。



##########
pkg/filter/http/openapi/openapi.go:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package openapi
+
+import (
+       "net/http"
+       "os"
+       "path/filepath"
+       "strings"
+)
+
+import (
+       "github.com/pb33f/libopenapi"
+       openapiValidator "github.com/pb33f/libopenapi-validator"
+       validatorConfig "github.com/pb33f/libopenapi-validator/config"
+       validatorErrors "github.com/pb33f/libopenapi-validator/errors"
+       validatorPaths "github.com/pb33f/libopenapi-validator/paths"
+       "github.com/pb33f/libopenapi/datamodel"
+       v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
+
+       "github.com/pkg/errors"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+       "github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter"
+       contexthttp "github.com/apache/dubbo-go-pixiu/pkg/context/http"
+       "github.com/apache/dubbo-go-pixiu/pkg/logger"
+)
+
+const (
+       // Kind is the kind of OpenAPI validation filter.
+       Kind = constant.HTTPOpenAPIFilter
+)
+
+func init() {
+       filter.RegisterHttpFilter(&Plugin{})
+}
+
+type (
+       Plugin struct {
+       }
+
+       FilterFactory struct {
+               cfg       *Config
+               validator openapiValidator.Validator
+               model     *v3.Document
+       }
+
+       Filter struct {
+               validator openapiValidator.Validator
+               model     *v3.Document
+       }
+
+       Config struct {
+               Path string `yaml:"path" json:"path,omitempty"`
+       }
+)
+
+func (p *Plugin) Kind() string {
+       return Kind
+}
+
+func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) {
+       return &FilterFactory{cfg: &Config{}}, nil
+}
+
+func (factory *FilterFactory) Config() any {
+       return factory.cfg
+}
+
+func (factory *FilterFactory) Apply() error {
+       path := strings.TrimSpace(factory.cfg.Path)
+       if path == "" {
+               return errors.New("openapi path is required")
+       }
+       factory.cfg.Path = path
+
+       validator, model, err := loadValidatorFromFile(path)
+       if err != nil {
+               return err
+       }
+       factory.validator = validator
+       factory.model = model
+       return nil
+}
+
+func (factory *FilterFactory) PrepareFilterChain(ctx *contexthttp.HttpContext, 
chain filter.FilterChain) error {
+       f := &Filter{
+               validator: factory.validator,
+               model:     factory.model,
+       }
+       chain.AppendDecodeFilters(f)
+       return nil
+}
+
+func (f *Filter) Decode(ctx *contexthttp.HttpContext) filter.FilterStatus {
+       if f.validator == nil || f.model == nil {
+               return filter.Continue
+       }
+
+       req := ctx.Request
+       pathItem, foundPath, ok := f.findRequestOperation(req)
+       if !ok {
+               return filter.Continue
+       }
+
+       if valid, validationErrs := 
f.validator.ValidateHttpRequestSyncWithPathItem(req, pathItem, foundPath); 
!valid {
+               errResp := 
contexthttp.BadRequest.WithError(errors.New(formatValidationErrors(validationErrs)))
+               ctx.SendLocalReply(errResp.Status, errResp.ToJSON())
+               logger.Debug(errResp.Error())
+               return filter.Stop
+       }
+       return filter.Continue
+}
+
+func (f *Filter) findRequestOperation(req *http.Request) (*v3.PathItem, 
string, bool) {
+       pathItem, validationErrs, foundPath := validatorPaths.FindPath(req, 
f.model, nil)
+       if len(validationErrs) > 0 || pathItem == nil {

Review Comment:
   已处理。现在 FindPath 返回 validationErrs 时会先记录 Debug 日志,包含请求 method/path 
和错误内容,再跳过校验;pathItem 为空或 operation 未声明时仍按未声明路径放行。



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to