Skip to content

Creating attachments ignore the attachment id #2254

@jl-wynen

Description

@jl-wynen

Summary

Using POST datasets/{dataset_id}/attachments with a payload that includes an id for the attachment ignores the id and assigns a new id.

I tested with backend versions 4.20 and 4.24

Steps to Reproduce

The following scitacean code creates a dataset, uploads an attachment, and then uploads a second attachment with the same id as the first.
The result is that the dataset has 2 attachments with different ids.

from pathlib import Path

from scitacean import PID, Attachment, Client, Dataset
from scitacean.testing import backend

docker_file = Path(__file__).resolve().parent / "backend" / "docker-compose.yaml"
docker_file.parent.mkdir(exist_ok=True)

# uncomment these lines to start a local scicat backend:
# backend.configure(docker_file)
# backend.start_backend(docker_file)
# backend.wait_until_backend_is_live(max_time=20, n_tries=20)

# backend.stop_backend(docker_file)

access = backend.config.local_access("user1")
client = Client.from_credentials(
    url=access.url,
    **access.user.credentials,
)
ds = Dataset(
    type="raw",
    name="test",
    contact_email="[email protected]",
    creation_location="here",
    investigator="me",
    owner=access.user.username,
    owner_group=access.user.group,
    source_folder="/hex/source",
)
finalized = client.upload_new_dataset_now(ds)

datset_pid = PID.parse("PID.prefix.a0b1/c846ee90-2bfd-4263-83a4-93529acbe890")
attachment1 = Attachment(
    caption="test attachment",
    owner_group=access.user.group,
)
finalized = client.scicat.create_attachment_for_dataset(
    attachment1.make_upload_model(), dataset_id=datset_pid
)
attachment_id = finalized.id

attachment2 = Attachment(
    caption="test attachment2",
    owner_group=access.user.group,
    id=attachment_id,
)
client.scicat.create_attachment_for_dataset(
    attachment2.make_upload_model(), dataset_id=datset_pid
)

downloaded = client.scicat.get_attachments_for_dataset(datset_pid)
print(downloaded)

ds = client.get_dataset(datset_pid, attachments=True)
print(ds.attachments)

Current Behaviour

The id field seems to be ignore.

Expected Behaviour

Either

  • Use the id for the attachment. If there already is an attachment with that id, fail.
  • Forbid passing an id in upload and don't silently ignore it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions