alexa / alexa-skills-kit-sdk-for-java

The Alexa Skills Kit SDK for Java helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
http://developer.amazon.com/ask
Apache License 2.0
815 stars 748 forks source link

Fix issue causing intent without request handler to throw a null poin… #320

Closed prakshai closed 2 years ago

prakshai commented 2 years ago

Fix issue causing intent without request handler to throw a NullPointerException (NPE), was not fixed in previous PR: https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/312. isPresent is not the same as check is the Optional object itself is null. isPresent checks if the value of the Optional is null.

Description

An intent when added to the interaction model may not have a corresponding RequestHandler. While its acceptable that the response is null or empty as a result, without the null check update in the PR, a NullPointerException is thrown by the skill. This is because the Optional have "value" field and not having the RequestHandler causes the Optional object itself to be null. When the value field of a null object is accessed a NPE is thrown.

Motivation and Context

Avoid throwing an NPE in case of missing RequestHandler, instead handle the scenario more gracefully by providing an empty or null response.

Testing

Created a skill with an interaction model with an intent without a corresponding RequestHandler. After the update in this PR, the response from the skill was null whereas before this PR the skill threw a NPE.

Skill behavior before the change:

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.Optional.orElse(java.lang.Object)' on a null object reference
    at com.amazon.ask.CustomSkill.invoke(CustomSkill.java:149)
    at com.amazon.ask.CustomSkill.invoke(CustomSkill.java:38)
    at com.amazon.ask.impl.AbstractSkill.execute(AbstractSkill.java:84)
    at com.amazon.ask.SkillStreamHandler.handleRequest(SkillStreamHandler.java:84)

Response JSON from skill after the change:

{"sessionAttributes":{},"userAgent":"ask-java/2.42.3 Java/0","version":"1.0"}

Note: This check existed before, but was removed in a previous commit due to a misunderstanding in the way the Optional value is handled. Commit: https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/286/commits/73296f63a12e7593dd070d3edaac54e6c9650734. Rest of the changes in the commit have been verified to work, just this minor change seems to have been unnecessary now.

Types of changes

Checklist

License

prakshai commented 2 years ago

Same issue as: https://github.com/alexa/alexa-skills-kit-sdk-for-java/pull/322