4 Commits

Author SHA1 Message Date
Rob Murray
22c037982c API compat: replace nil values when adding fields
Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-10-30 22:48:32 +00:00
Sebastiaan van Stijn
e204ba1dca daemon/internal/compat: add extra fields recursively, and don't replace
This was a bit of an oversight; when setting additional fields to add,
the compat package currently replaces fields unconditionally. This may
have sounded like an OK idea, but it makes it more complicated to augment
responses where current versions use an "omitempty", but older API versions
should return default / zero-values.

This patch:

- Changes the meaning of "extra fields"; extra fields are only used if
  the field is not present in the response.
- Makes the merging of "extra fields" recursive; this makes it easier
  to patch responses where extra fields must be added to nested structs.
  Previously, this would require the nested struct to be wrapped with a
  `compat.Wrap` and replaced as a whole; lacking a "replace" option made
  that more complicated, so making the extra fields recursive.
- Comment-out a test that tested the old behavior of replacing fields;
  we currently have no cases where we must _replace_ fields or structs,
  so I did not yet implement such an option, but we can implement a
  `WithReplaceFields` (e.g.) once there's a need.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-04 15:09:22 +02:00
Albin Kerouanton
d5d3716705 daemon/i/compat: disable HTML encoding
The compat package marshals the base value into JSON to convert it into
a map[string]any, then applies the compat options, and finally
re-encodes the map into JSON.

`json.Marshal` is used for both marshalling operations. However, this
function encodes special HTML characters by default. The API responses
aren't meant to be embedded into HTML, so this behavior is not desired.

Replace `json.Marshal` with a `json.Encoder` with HTML escaping
disabled. Extra care is taken to drop the trailing LF added by
`(*json.Encoder).Encode()` (see [1]).

[1]: https://pkg.go.dev/encoding/json#Encoder.Encode

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-09-11 15:43:36 +02:00
Sebastiaan van Stijn
33a05ac344 daemon/internal: add "compat" package for legacy responses
Add a package to help augmenting API responses with additional fields,
replacing fields, or to remove fields from the response to allow for
legacy API responses to be produced without having to keep deprecated
fields in the API type definitions.

Co-authored-by: Albin Kerouanton <albinker@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-11 00:13:05 +02:00