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

[config] Enable config validation for updates by handler #2712

Merged

Conversation

J-N-K
Copy link
Member

@J-N-K J-N-K commented Jan 29, 2022

See #2682 (comment)

It was assumed that thing handlers only update valid configuration. After merging #2682 it was discovered that this is not true. This PR enables the same validation for updates through the handler. If an invalid configuration or otherwise illegal (not-initializable) thing is detected, the changes are discarded and a warning logged.

Signed-off-by: Jan N. Klug [email protected]

@J-N-K J-N-K requested a review from a team as a code owner January 29, 2022 13:04
Copy link
Contributor

@cweitkamp cweitkamp left a comment

Choose a reason for hiding this comment

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

I have the slight feeling that this will it work because the BaseThingHandler:: update configuration () methods replaces the configuration properties before calling the updateThing() methods on the callback.

this.thing.getConfiguration().setProperties(configuration.getProperties());
synchronized (this) {
if (this.callback != null) {
this.callback.thingUpdated(thing);
} else {
logger.warn(
"Handler {} tried updating its configuration although the handler was already disposed.",
this.getClass().getSimpleName());
}
}

Meaning the wrong parameters will be applied even if you discard the changes later.

@J-N-K
Copy link
Member Author

J-N-K commented Jan 29, 2022

I reworked that. Now the BaseThingHandler validates the configuration before they are applied or updated (both in updateThing and updateConfiguration). There is a slight chance that an invalid configuration is applied anyway if the implementation of the ThingHandler overrides these methods and does not do a validation.

Do you think updateProperties should also validate its parameters?

Signed-off-by: Jan N. Klug <[email protected]>
Signed-off-by: Jan N. Klug <[email protected]>
@cweitkamp cweitkamp added the enhancement An enhancement or new feature of the Core label Jan 30, 2022
@cweitkamp
Copy link
Contributor

Do you think updateProperties should also validate its parameters?

No, I don't think so. Do you see a specific reason for doing it?

@J-N-K
Copy link
Member Author

J-N-K commented Jan 30, 2022

After checking the code: no. I was wondering if it is possible to change configuration with updateProperties, but it seems that the only place where properties and configuration are mixed is when building a discovery result. After the thing is added from the INBOX, they are separated.

@cweitkamp cweitkamp changed the title enable config validation for updates by handler [config] Enable config validation for updates by handler Jan 30, 2022
@cweitkamp
Copy link
Contributor

If you are afraid that we loose this safety check by overridden methods we can try to move it deeper into the framework. E.g. into the ThingHandlerCallback::updateThing() method.

@J-N-K
Copy link
Member Author

J-N-K commented Jan 30, 2022

But then we are back where we were before: the wrong config is applied before it is validated. I think that overriding updateThing or updateConfiguration is no very common and if it is sone should be secured by review.

@cweitkamp
Copy link
Contributor

Let's add a note about that to the JavaDoc. This way people know what the method is doing and what to take care of if they override it.

Signed-off-by: Jan N. Klug <[email protected]>
@J-N-K
Copy link
Member Author

J-N-K commented Jan 30, 2022

Done

J-N-K added 2 commits January 31, 2022 12:30
Signed-off-by: Jan N. Klug <[email protected]>
Copy link
Contributor

@cweitkamp cweitkamp left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks.

@cweitkamp cweitkamp merged commit 61f5e7f into openhab:main Feb 1, 2022
@cweitkamp cweitkamp added this to the 3.3 milestone Feb 1, 2022
@J-N-K J-N-K deleted the feature-enable-internalupdate-configvalidation branch February 1, 2022 16:36
splatch pushed a commit to ConnectorIO/copybara-hab-core that referenced this pull request Jul 12, 2023
* Enable config validation for updates by handler

Signed-off-by: Jan N. Klug <[email protected]>
GitOrigin-RevId: 61f5e7f
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 of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants