feat: add option to ignore unknown fields in gateway request parsing (#5058)

This commit is contained in:
guonaihong
2025-08-09 12:40:07 +08:00
committed by GitHub
parent 9fd1f29845
commit 00e67b9d20
4 changed files with 165 additions and 29 deletions

View File

@@ -30,11 +30,12 @@ type (
// Server is a gateway server.
Server struct {
*rest.Server
upstreams []Upstream
conns []zrpc.Client
processHeader func(http.Header) []string
dialer func(conf zrpc.RpcClientConf) zrpc.Client
middlewares []rest.Middleware
upstreams []Upstream
conns []zrpc.Client
processHeader func(http.Header) []string
dialer func(conf zrpc.RpcClientConf) zrpc.Client
middlewares []rest.Middleware
ignoreUnknownFields bool
}
// Option defines the method to customize Server.
@@ -44,8 +45,9 @@ type (
// MustNewServer creates a new gateway server.
func MustNewServer(c GatewayConf, opts ...Option) *Server {
svr := &Server{
upstreams: c.Upstreams,
Server: rest.MustNewServer(c.RestConf),
upstreams: c.Upstreams,
Server: rest.MustNewServer(c.RestConf),
ignoreUnknownFields: c.IgnoreUnknownFields,
}
for _, opt := range opts {
opt(svr)
@@ -114,7 +116,7 @@ func (s *Server) buildChainHandler(handler http.HandlerFunc) http.HandlerFunc {
func (s *Server) buildGrpcHandler(source grpcurl.DescriptorSource, resolver jsonpb.AnyResolver,
cli zrpc.Client, rpcPath string) func(http.ResponseWriter, *http.Request) {
handler := func(w http.ResponseWriter, r *http.Request) {
parser, err := internal.NewRequestParser(r, resolver)
parser, err := internal.NewRequestParser(r, resolver, s.ignoreUnknownFields)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
@@ -223,7 +225,6 @@ func (s *Server) buildHttpHandler(target *HttpClientConf) http.HandlerFunc {
logc.Error(r.Context(), err)
}
}
return s.buildChainHandler(handler)
}