@@ -71,24 +71,32 @@ func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
71
71
ctx = NewContextWithRequest (ctx , req )
72
72
73
73
// 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 {
76
76
return Errored (http .StatusBadRequest , err )
77
77
}
78
78
79
79
// 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 {
81
82
var apiStatus apierrors.APIStatus
82
83
if errors .As (err , & apiStatus ) {
83
84
return validationResponseFromStatus (false , apiStatus .Status ())
84
85
}
85
86
return Denied (err .Error ())
86
87
}
87
88
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 )
90
94
if err != nil {
91
95
return Errored (http .StatusInternalServerError , err )
92
96
}
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 )
94
102
}
0 commit comments