Skip to content

Commit

Permalink
NPE in lib-app.get and lib-app.getDescriptor for missing app key #10280
Browse files Browse the repository at this point in the history
  • Loading branch information
pmi committed Oct 4, 2023
1 parent c9ce353 commit 89a345f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.enonic.xp.lib.app;

import java.util.Optional;
import java.util.function.Supplier;

import com.enonic.xp.app.ApplicationDescriptorService;
Expand All @@ -18,7 +19,9 @@ public final class GetApplicationDescriptorHandler

public ApplicationDescriptorMapper execute()
{
return new ApplicationDescriptorMapper( applicationDescriptorServiceSupplier.get().get( ApplicationKey.from( key ) ) );
return Optional.ofNullable( applicationDescriptorServiceSupplier.get().get( ApplicationKey.from( key ) ) )
.map( ApplicationDescriptorMapper::new )
.orElse( null );
}

public void setKey( final String key )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.enonic.xp.lib.app;

import java.util.Optional;
import java.util.function.Supplier;

import com.enonic.xp.app.ApplicationKey;
Expand All @@ -18,7 +19,9 @@ public final class GetApplicationHandler

public ApplicationMapper execute()
{
return new ApplicationMapper( applicationServiceSupplier.get().get( ApplicationKey.from( key ) ) );
return Optional.ofNullable( applicationServiceSupplier.get().get( ApplicationKey.from( key ) ) )
.map( ApplicationMapper::new )
.orElse( null );
}

public void setKey( final String key )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public void testWithoutIcon()

runFunction( "/test/GetApplicationDescriptorHandlerTest.js", "getWithoutIcon" );
}

@Test
public void testMissing()
{

when( applicationDescriptorService.get( isA( ApplicationKey.class ) ) ).thenAnswer( params -> null );

runFunction( "/test/GetApplicationDescriptorHandlerTest.js", "getMissing" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public void testExample()

runScript( "/lib/xp/examples/app/get.js" );
}

@Test
public void testMissing()
{

when( applicationService.get( isA( ApplicationKey.class ) ) ).thenAnswer( params -> null );

runFunction( "/test/GetApplicationHandlerTest.js", "getMissing" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ var assert = require('/lib/xp/testing');

exports.getWithoutIcon = function () {
var result = appLib.getDescriptor({
key: '123456'
key: '123456',
});

assert.assertJsonEquals({
'key': '123456', 'description': 'my app description'
'key': '123456', 'description': 'my app description',
}, result);
};

exports.getMissing = function () {
var result = appLib.getDescriptor({
key: 'missing',
});

assert.assertNull(result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var appLib = require('/lib/xp/app');
var assert = require('/lib/xp/testing');

exports.getMissing = function () {
var result = appLib.getDescriptor({
key: 'missing',
});

assert.assertNull(result);
}

0 comments on commit 89a345f

Please sign in to comment.