@@ -71,24 +71,32 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
7171ctx = NewContextWithRequest (ctx , req )
7272
7373// Get the object in the request
74- obj := h .object .DeepCopyObject ()
75- if err := h .decoder .Decode (req , obj ); err != nil {
74+ original := h .object .DeepCopyObject ()
75+ if err := h .decoder .Decode (req , original ); err != nil {
7676return Errored (http .StatusBadRequest , err )
7777}
7878
7979// Default the object
80- if err := h .defaulter .Default (ctx , obj ); err != nil {
80+ updated := original .DeepCopyObject ()
81+ if err := h .defaulter .Default (ctx , updated ); err != nil {
8182var apiStatus apierrors.APIStatus
8283if errors .As (err , & apiStatus ) {
8384return validationResponseFromStatus (false , apiStatus .Status ())
8485}
8586return Denied (err .Error ())
8687}
8788
88- // Create the patch
89- marshalled , err := json .Marshal (obj )
89+ // Create the patch.
90+ // We need to decode and marshall the original because the type registered in the
91+ // decoder might not match the latest version of the API.
92+ // Creating a diff from the raw object might cause new fields to be dropped.
93+ marshalledOriginal , err := json .Marshal (original )
9094if err != nil {
9195return Errored (http .StatusInternalServerError , err )
9296}
93- return PatchResponseFromRaw (req .Object .Raw , marshalled )
97+ marshalledUpdated , err := json .Marshal (updated )
98+ if err != nil {
99+ return Errored (http .StatusInternalServerError , err )
100+ }
101+ return PatchResponseFromRaw (marshalledOriginal , marshalledUpdated )
94102}
0 commit comments