[MAGNOLIA-8108] RestorePreviousVersionCommand throws nullPointer exception Created: 31/May/21  Updated: 22/Jun/21  Resolved: 08/Jun/21

Status: Closed
Project: Magnolia
Component/s: None
Affects Version/s: 6.2.8
Fix Version/s: 6.2.10

Type: Bug Priority: Neutral
Reporter: Jonathan Ayala Assignee: Oanh Thai Hoang
Resolution: Fixed Votes: 0
Labels: VN-Implementation, maintenance
Remaining Estimate: 0d
Time Spent: 0.25d
Original Estimate: Not Specified

Attachments: XML File dam.tours.shark_brian_warrick_0824..JPG.test.xml    
Issue Links:
Cloners
is cloned by MGNLUI-6734 Can't send notification to view "ui-a... Closed
Relates
relates to MAGNOLIA-8048 Remove hasVersion mixin when there is... Closed
causality
Template:
Acceptance criteria:
Empty
Task DoD:
[X]* Doc/release notes changes? Comment present?
[X]* Downstream builds green?
[X]* Solution information and context easily available?
[X]* Tests
[X]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[X]* Steps to reproduce, expected, and actual results filled
[X]* Affected version filled
Release notes required:
Yes
Date of First Response:
Sprint: Maintenance 61
Story Points: 2

 Description   

Steps to reproduce 

In order to quickly reproduce the issue we are going to force assets to have subnodes

  1. Go to JCR app and navigate to /tours/shark_brian_warrick_0824.JPG and import the attached XML dam.tours.shark_brian_warrick_0824..JPG.test.xml
  2. Add restorePreviousVersion to the action bar in dam assets app: edit /dam-app-jcr/decorations/dam-assets-app/apps/dam.subApps.yaml to add it as follows: 
    versions:
                items:
                  - name: showVersions
                  - name: restoreVersion
                  - name: restorePreviousVersion
    
  3. Open assets app and go to /tours/shark_brian_warrick_0824.JPG. Publish it to make sure there are versions available
  4. Click on restore previous action in the action bar

Expected results

Previous version is restored ok

Actual results

An error is thrown with no detailed information of the cause

Workaround

Using restore version action

Development notes

This error happens only when there are subnodes in the item. When debugging RestorePreviousVersionCommand you can see that the cause is a nullPointerException which is thrown in info.magnolia.commands.impl.RestorePreviousVersionCommand.getPreviousVersion(Node) method, specifically here:

VersionIterator versionIterator = versionManager.getAllVersions(node);

        while (versionIterator.hasNext()) {
            previousVersion = versionIterator.nextVersion();
        }

versionManager.getAllVersions can return null in two scenarios, if versionedNode is null and if versionHistory is null. Therefore, if one of these situations occurs, the next instruction, versionIterator.hasNext(), will throw a null pointer exception.
SOLUTION: add a null control before the while loop:

proposed solution
 VersionIterator versionIterator = versionManager.getAllVersions(node);
if(versionIterator!=null){
        while (versionIterator.hasNext()) {
            previousVersion = versionIterator.nextVersion();
        }
}


 Comments   
Comment by Oanh Thai Hoang [ 22/Jun/21 ]

Hi akhamis, here is the thing changes: Check null for version node before restoring previous version

Generated at Mon Feb 12 04:29:43 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.