From d61942f7959df2dac0c57d9f7d13ea1d314fcf06 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 12 Aug 2019 21:32:47 -0700 Subject: [PATCH] Fix nil panic --- .../rancher/norman/pkg/api/server.go | 19 +++++++++++-------- .../rancher/norman/pkg/api/writer/json.go | 18 ++++++++++-------- .../rancher/norman/pkg/types/server_types.go | 8 ++++++-- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/vendor/github.com/rancher/norman/pkg/api/server.go b/vendor/github.com/rancher/norman/pkg/api/server.go index dabe7e52..0c814fd9 100644 --- a/vendor/github.com/rancher/norman/pkg/api/server.go +++ b/vendor/github.com/rancher/norman/pkg/api/server.go @@ -100,6 +100,10 @@ func (s *Server) setDefaults(ctx *types.APIRequest) { } ctx.AccessControl = s.AccessControl + + if ctx.Schemas == nil { + ctx.Schemas = s.Schemas + } } func (s *Server) AddSchemas(schemas *types.Schemas) error { @@ -162,14 +166,15 @@ func (s *Server) Handle(apiOp *types.APIRequest) { } func (s *Server) handle(apiOp *types.APIRequest, rw http.ResponseWriter, req *http.Request, parser parse.Parser) { - if apiOp.Schemas == nil { - apiOp.Schemas = s.Schemas - } if err := parser(apiOp, parse.MuxURLParser); err != nil { + // ensure defaults set so writer is assigned + s.setDefaults(apiOp) s.handleError(apiOp, err) return } + s.setDefaults(apiOp) + if code, data, err := s.handleOp(apiOp); err != nil { s.handleError(apiOp, err) } else { @@ -200,8 +205,6 @@ func determineVerb(apiOp *types.APIRequest) Verb { } func (s *Server) handleOp(apiOp *types.APIRequest) (int, interface{}, error) { - s.setDefaults(apiOp) - if err := CheckCSRF(apiOp); err != nil { return 0, nil, err } @@ -261,10 +264,10 @@ func handleAction(action *types.Action, context *types.APIRequest) error { } func (s *Server) handleError(apiOp *types.APIRequest, err error) { - if apiOp.Schema == nil { - s.Defaults.ErrorHandler(apiOp, err) - } else if apiOp.Schema.ErrorHandler != nil { + if apiOp.Schema != nil && apiOp.Schema.ErrorHandler != nil { apiOp.Schema.ErrorHandler(apiOp, err) + } else if s.Defaults.ErrorHandler != nil { + s.Defaults.ErrorHandler(apiOp, err) } } diff --git a/vendor/github.com/rancher/norman/pkg/api/writer/json.go b/vendor/github.com/rancher/norman/pkg/api/writer/json.go index 068ab49e..5d46bab6 100644 --- a/vendor/github.com/rancher/norman/pkg/api/writer/json.go +++ b/vendor/github.com/rancher/norman/pkg/api/writer/json.go @@ -62,15 +62,17 @@ func (j *EncodingResponseWriter) VersionBody(apiOp *types.APIRequest, writer io. case types.RawResource: output = v default: - mapData, err := convert.EncodeToMap(obj) - if err != nil { - return err + if v != nil { + mapData, err := convert.EncodeToMap(obj) + if err != nil { + return err + } + schema := apiOp.Schemas.SchemaFor(reflect.TypeOf(obj)) + if schema != nil && mapData != nil { + mapData["type"] = schema.ID + } + output = j.convert(builder, apiOp, mapData) } - schema := apiOp.Schemas.SchemaFor(reflect.TypeOf(obj)) - if schema != nil && mapData != nil { - mapData["type"] = schema.ID - } - output = j.convert(builder, apiOp, mapData) } if list, ok := output.(*types.GenericCollection); ok && revision != "" { diff --git a/vendor/github.com/rancher/norman/pkg/types/server_types.go b/vendor/github.com/rancher/norman/pkg/types/server_types.go index 3790e562..ec2b2a7b 100644 --- a/vendor/github.com/rancher/norman/pkg/types/server_types.go +++ b/vendor/github.com/rancher/norman/pkg/types/server_types.go @@ -238,10 +238,14 @@ func (a *APIObject) Raw() interface{} { } func (a *APIObject) Map() data.Object { - if a == nil { + if a == nil || a.IsNil() { return nil } - return convert.ToMapInterface(a.Object) + data, err := convert.EncodeToMap(a.Object) + if err != nil { + return convert.ToMapInterface(a.Object) + } + return data } func (a APIObject) IsNil() bool {