Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hdpowerview] Fix SAT warnings #12032

Merged
merged 2 commits into from
Jan 14, 2022

Conversation

jlaur
Copy link
Contributor

@jlaur jlaur commented Jan 11, 2022

Signed-off-by: Jacob Laursen [email protected]

Fixes #12027

Changes done:

  1. Fix SAT warnings about missing @NonNullByDefault annotations on classes.

Next steps (in context of new PR):

  1. Refactor error handling: Throw exception if contract is breached.
  2. Consider separating deserialization classes so they are not exposed directly to handlers (with @Nullable when contract doesn't allow this). Or find other way to make sure handlers are not bloated with null handling which shouldn't be needed for valid responses.

@jlaur jlaur added enhancement An enhancement or new feature for an existing add-on work in progress A PR that is not yet ready to be merged labels Jan 11, 2022
@andrewfg
Copy link
Contributor

andrewfg commented Jan 12, 2022

I will look at you code tomorrow, but here is some quick feedback..

Personally I would not bother with your proposal 3. above, since what you call “bloat” is actually “good practice” (against the possibility of some device in future not honouring the contract).

In addition, while you are refactoring your classes, I would also refactor the Shade fields position0 and positionKind0 from int to @nullable Integer, and add the respective null checks on those, at the next higher level. There may be other similar cases too..

@jlaur
Copy link
Contributor Author

jlaur commented Jan 12, 2022

I will look at you code tomorrow, but here is some quick feedback..

Thanks, no rush. This is so far just some initial steps. Will probably await #11853 and #12002 before doing anything major, since I'll be changing all methods in HDPowerViewWebTargets as well as all callers, so will collide with that work. Hopefully they can be merged soon. I was even just giving getShadeSurvey() same treatment as getFirmwareVersions(), but realized that I was on collision course on caller side with one of those PR's. Conflicts are part of life, but I would rather avoid having to do everything all over if I can.

Personally I would not bother with your proposal 3. above, since what you call “bloat” is actually “good practice” (against the possibility of some device in future not honouring the contract).

One of the problems, as I see it, is that if something is actually required in a response for the response to make sense at all, this should be verified and caller shouldn't have to deal with this, but just provide generic error handling, i.e. catch some exception.

Example: For the firmware response to make sense, mainProcessor is required. radio, on the other hand, is optional as it was added in Hub v2, apparently.

So let's say we did this validation in getFirmwareVersions:

if (firmwareVersions.mainProcessor == null) {
    throw new JsonParseException("Missing 'mainProcessor' element");
}

Unfortunately this wouldn't help. Caller would still have to perform the exact same check, since everything is by definition @Nullable:

@Nullable
public Firmware mainProcessor;

That's why I was thinking about possibly introducing a class with stricter annotation adhering to a contract instead of just returning objects of FirmwareVersions where everything is nullable because of reflection/gson deserialization.

This would introduce some overhead with maintaining additional model classes, but on the other hand it would simplify the handlers/logic because they wouldn't have to check for nulls everywhere only because of undesired nullness annotation from deserialization classes. That's what I consider bloat, or at least cluttering. Logic is harder to read IMHO when entangled with checks needed because of nullness annotations.

Maybe there are some JSON deserialization alternatives that supports annotations and/or have runtime checks. I'll look into other bindings also as this must be a common challenge. :-)

In addition, while you are refactoring your classes, I would also refactor the Shade fields position0 and positionKind0 from int to @nullable Integer, and add the respective null checks on those, at the next higher level. There may be other similar cases too..

Sure, plan would be to refactor all of them when having found a reasonable solution. The concrete example you are mentioning would have the same problem as described above. Since they would be nullable, we would have to provide duplicate checks to avoid warnings, I think. :-(

@andrewfg
Copy link
Contributor

andrewfg commented Jan 13, 2022

The OH developers / maintainers decided on GSON as the standard JSON (de)serialization librarary. And this library does not (yet) support Required or Optional annotations (google/gson#61) so it seems to me that this discussion about replacing or overwriting the standard OH library should not be specific to this binding, but rather should be addressed to OH developers / maintainers for the whole OH eco-system.

Copy link
Contributor

@lolodomo lolodomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lolodomo
Copy link
Contributor

lolodomo commented Jan 14, 2022

Is it ready for a merge? In this case, please remove draft/WIP?

@andrewfg
Copy link
Contributor

Is it ready for a merge? In this case, please remove draft/WIP?

@lolodomo this PR is still a draft, and not ready to merge; you are probably confusing it with #11853 which is indeed ready.

@jlaur
Copy link
Contributor Author

jlaur commented Jan 14, 2022

Is it ready for a merge? In this case, please remove draft/WIP?

No, it's only getting started, but now a little bit stuck because of these other PR's that would need to go first to avoid major conflicts and rework:

@lolodomo
Copy link
Contributor

No, I am not confusing it with another PR, I reviewed the code for this PR and it looks good to me.
Let me know when this is finished.

@andrewfg
Copy link
Contributor

No, I am not confusing it with another PR, I reviewed the code for this PR and it looks good to me. Let me know when this is finished.

@lolodomo, IMHO this is far from finished, so perhaps your "LGTM" is premature (??) - please look at my comment a few posts above this #12032 (comment)

@lolodomo
Copy link
Contributor

lolodomo commented Jan 14, 2022

If you can have several small PRs rather one very big PR, that is really better for reviewing.

@jlaur jlaur marked this pull request as ready for review January 14, 2022 18:11
@jlaur
Copy link
Contributor Author

jlaur commented Jan 14, 2022

If you can have several small PRs rather one very big PR, that is really better for reviewing.

Sure, I'll split it. This one can be merged, I'll update the title and description.

@jlaur jlaur removed the work in progress A PR that is not yet ready to be merged label Jan 14, 2022
@jlaur jlaur changed the title [hdpowerview] Refactor error handling for webservice contracts [hdpowerview] Fix SAT warnings Jan 14, 2022
@lolodomo lolodomo merged commit d07348b into openhab:main Jan 14, 2022
@lolodomo lolodomo added this to the 3.3 milestone Jan 14, 2022
moesterheld pushed a commit to moesterheld/openhab-addons that referenced this pull request Jan 18, 2022
* Fix SAT warnings about missing @NonNullByDefault.
* Move part of firmware JSON response validation to HDPowerViewWebTargets.

Signed-off-by: Jacob Laursen <[email protected]>
nemerdaud pushed a commit to nemerdaud/openhab-addons that referenced this pull request Jan 28, 2022
* Fix SAT warnings about missing @NonNullByDefault.
* Move part of firmware JSON response validation to HDPowerViewWebTargets.

Signed-off-by: Jacob Laursen <[email protected]>
NickWaterton pushed a commit to NickWaterton/openhab-addons that referenced this pull request Apr 27, 2022
* Fix SAT warnings about missing @NonNullByDefault.
* Move part of firmware JSON response validation to HDPowerViewWebTargets.

Signed-off-by: Jacob Laursen <[email protected]>
Signed-off-by: Nick Waterton <[email protected]>
@jlaur jlaur deleted the 12027-hdpowerview-null-handling branch June 13, 2022 18:41
andan67 pushed a commit to andan67/openhab-addons that referenced this pull request Nov 6, 2022
* Fix SAT warnings about missing @NonNullByDefault.
* Move part of firmware JSON response validation to HDPowerViewWebTargets.

Signed-off-by: Jacob Laursen <[email protected]>
andrasU pushed a commit to andrasU/openhab-addons that referenced this pull request Nov 12, 2022
* Fix SAT warnings about missing @NonNullByDefault.
* Move part of firmware JSON response validation to HDPowerViewWebTargets.

Signed-off-by: Jacob Laursen <[email protected]>
Signed-off-by: Andras Uhrin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature for an existing add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[hdpowerview] Fix SAT missing null annotation warnings
3 participants