Skip to content

Set a file field through its field manager, not the raw mutator - #118

Open
ramonski wants to merge 1 commit into
2.xfrom
fix/named-file-fields-via-the-field-manager
Open

ramonski wants to merge 1 commit into
2.xfrom
fix/named-file-fields-via-the-field-manager

Conversation

@ramonski

Copy link
Copy Markdown
Contributor

Description of the issue/feature this PR addresses

A NamedFile or NamedImage field on a Dexterity type that also carries a BBB set<Name> mutator is written through the mutator, which stores the value exactly as it arrives. Over the API a file arrives as base64 text, so the field ends up holding the text rather than the file, and the object then fails its own validation on that field.

The data manager already goes through the field manager for the managers that normalize their value (UID references, durations, decimals). NamedFileFieldManager does precisely that: it base64-decodes the payload and builds the NamedFile or NamedImage the field is declared to hold, taking the filename and content type from the payload. It belongs in that tuple, and NamedImageFieldManager comes with it as a subclass.

Current behavior before PR

POST /senaite/@@API/senaite/v1/update
{"uid": "<laboratory uid>",
 "accreditation_body_logo": "<base64>",
 "filename": "seal.svg"}

HTTP/1.1 400 Bad Request
{"message": "{\"accreditation_body_logo\": \"wrong type\"}", ...}

The raw base64 string is written onto the field by setAccreditationBodyLogo, and the following full-object validation refuses it.

Desired behavior after PR is merged

The value goes through NamedFileFieldManager, which decodes it and stores a NamedBlobImage. The file is then served as usual:

GET /senaite/setup/laboratory/@@download/accreditation_body_logo/seal.svg
HTTP/1.1 200 OK
Content-Type: image/svg+xml

Verification

  • Set accreditation_body_logo on a Laboratory over the API with a base64 payload and a filename, and fetch it back through @@download and @@images.
  • A file field on a type without a BBB mutator (which already worked) is unchanged: it reached the field manager through the no-setter fallback before and reaches it one branch earlier now.

A NamedFile or NamedImage field on a Dexterity type that also carries a
BBB set<Name> mutator was written through the mutator, which stores the
value exactly as it arrives. Over the API a file arrives as base64 text,
so the field ended up holding the text rather than the file, and the
object then failed its own validation with "wrong type" on that field.

The data manager already goes through the field manager for the managers
that normalize their value. NamedFileFieldManager does precisely that: it
base64-decodes the payload and builds the NamedFile or NamedImage the
field is declared to hold, taking the filename and content type from the
payload. It belongs in that tuple, and NamedImageFieldManager comes with
it as a subclass.

Laboratory.accreditation_body_logo is the case that surfaced it: a
NamedBlobImage with a setAccreditationBodyLogo mutator kept from the
Archetypes schema. A field without such a mutator was never affected,
which is why file fields have always been settable on the types that do
not carry the BBB properties.
@ramonski
ramonski requested a review from xispa October 10, 2026 08:24
@ramonski ramonski added the Bug 🐞 Something is broken label Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug 🐞 Something is broken

Development

Successfully merging this pull request may close these issues.

1 participant