There is 1 comment.
 
 
XWiki Platform / cid:jira-generated-image-avatar-dbe44040-e58a-4fc4-9433-dfd06bec787b XWIKI-24546 Open

Images inserted by uploading in Annotations become broken after save

 
View issue   ·   Add comment
 

1 comment

 
cid:jira-generated-image-avatar-7c17ce8b-b3f9-4abe-bfd7-892f8ace5135 Vincent Massol on 06/Jul/26 10:57
 
Analysis of XWIKI-24546                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                            
  Symptom: An image uploaded inside an annotation (CKEditor in the annotation popup) is broken after save — only the filename shows. Plain comments and images from existing pages work fine.                                                                                                                               
                                                                                                                                                                                                                                                                                                                            
  Root cause — a broken assumption about cached-document mutation:                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                            
  The annotation save flow (unchanged since XWIKI-18989, 2022) persists uploaded images like this:                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                            
  1. AbstractAnnotationsRESTResource.postAnnotation → handleTemporaryUploadedFiles(docRef, metadata) (AbstractAnnotationRESTResource:417) does document = getWiki().getDocument(docRef) then attachTemporaryAttachmentsInDocument(document, uploadedFiles). It never saves this document.                                   
  2. attachTemporaryAttachmentsInDocument → document.setAttachment(...) → XWikiAttachmentList.updateList() → document.setMetaDataDirty(true) (XWikiAttachmentList.java).                                                                                                                                                    
  3. annotationService.addAnnotation(...) → DefaultIOService.addAnnotation does its own getDocument(), document.clone(), adds the annotation xobject, and saves that instance (DefaultIOService.java:126,137,162).                                                                                                          
                                                                                                                                                                                                                                                                                                                            
  Historically this worked purely by side effect: step 1's getDocument() returned the shared cached instance, setAttachment mutated it in place, and step 3's getDocument() returned that same mutated instance (so the clone carried the attachment).                                                                      
                                                                                                                                                                                                                                                                                                                            
  The regression: XWIKI-22892 — "In loadXWikiDoc, automatically reload the cached document if it has been modified" (commit 6642dc59f50, first released in 17.2.0). It changed XWikiCacheStore.loadXWikiDoc to return the cached document only if !cachedoc.isMetaDataDirty() — otherwise it reloads a fresh copy from the  
  database. Because step 2 marks the cached doc dirty, step 3's getDocument() now reloads a clean DB copy without the attachment, clones that, and saves the annotation. The uploaded attachment is never persisted → broken image.                                                                                         
                                                                                                                                                                                                                                                                                                                            
  Version corroboration:                                                                                                                                                                                                                                                                                                    
  - XWIKI-22892 is in 17.2.0+ (verified via git tag --contains). The reported broken versions 17.10.9 and 18.4.1 contain it; the fix versions (17.10.10, 18.4.3, 18.6.0‑rc‑1 — no 16.10.x) match this exactly.                                                                                                              
  - On the 16.10.x branch loadXWikiDoc still returns the cached instance as-is (diff 16.10.10→16.10.18 for XWikiCacheStore.java is empty, isMetaDataDirty absent) — so per the code, 16.10.18 should not be affected. The "affects 16.10.18 / works on 16.10.10" note in the ticket is inconsistent with the code and worth 
  flagging to the reporter.                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                            
  The same defect also affects the annotation edit path (SingleAnnotationRESTResource → updateAnnotations, same clone+save pattern).                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                            
  The correct pattern is the one comments and normal edits use: attach the temporary files to the very document instance that gets saved (e.g. CommentAddAction attaches to doc then saves doc; XWikiDocument.readTemporaryUploadedFiles attaches to this).                                                                 
                                                                                                                                                                                                                                                                                                                            
  Let me confirm the fix approach with you, since it's a design choice with backport implications.                                    

Andreea Tarata Are you sure about the 16.10.18 affects version (did you try it)? I'm asking because my LLM said:

[...] so per the code, 16.10.18 should not be affected. The "affects 16.10.18 / works on 16.10.10" note in the ticket is inconsistent with the code and worth flagging to the reporter.

Thx