Skip to content

[rest] Fix GET /rest/persistence/items returns null entries - #5387

Closed
florian-h05 wants to merge 3 commits into
openhab:mainfrom
florian-h05:persistence-itemlist-null
Closed

[rest] Fix GET /rest/persistence/items returns null entries#5387
florian-h05 wants to merge 3 commits into
openhab:mainfrom
florian-h05:persistence-itemlist-null

Conversation

@florian-h05

@florian-h05 florian-h05 commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

When querying Items from InMemory, I noticed that the return value from GET /rest/persistence/items?serviceid=inmemory is an array of null values.
This was caused by a silent serialization failure, where Gson serialized anonymous implementations of PersistenceItemInfo to null due to missing fields. Serialization was error-prone in general, as the serialization results depended on the field names of PersistenceItemInfo implementations from the persistence services.

This PR fixes that issue by introducing a proper DTO and mapping to that. It also adds unit tests.

…ntries

When querying Items from InMemory, I noticed that the return is an array of null values.
This was caused by a silent serialization failure, where Gson serialized anonymous implementations of PersistenceItemInfo to null due to missing fields.
Serialization was error-prone in general, as the serialization results depended on the field names of PersistenceItemInfo implementations from the persistence services.

This PR fixes that issue by introducing a proper DTO and mapping to that.
It also adds unit tests.

Signed-off-by: Florian Hotze <dev@florianhotze.com>
@florian-h05
florian-h05 requested a review from a team as a code owner February 24, 2026 12:18
Signed-off-by: Florian Hotze <dev@florianhotze.com>
@florian-h05 florian-h05 changed the title [rest] Fix GET /rest/persistence/items?serviceid=... returns null entries [rest] Fix GET /rest/persistence/items returns null entries Feb 24, 2026
@mherwege

mherwege commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

@florian-h05 I had seen similar issues of fields not being included, but had not seen the consequence of having items without item name. That's most likely specific to InMemoryPeristence because it creates the item name through a method call and doesn't have a field for it. The services I tested have a field that gets serialized.

Anyway, I also solved it in #5336, in a very similar way, creating a record PersistenceItemInfoDTO, embedded in the PersistenceResource class. As it is only used there, I didn't extract it in a separate class.

@florian-h05

Copy link
Copy Markdown
Contributor Author

Sorry, didn't notice it fixed the issue, only read the PR description and wanted to look at the code later.
I think the benefit of fixing it in this PR here is it only fixes the issue and breaks nothing, so it can be easily backported.

@florian-h05

Copy link
Copy Markdown
Contributor Author

BTW, when applying aliases, I think the issue would have occurred for all services as the PersistenceResource then used an anonymous implementation of the PersistenceItemInfo interface as well.

@mherwege

Copy link
Copy Markdown
Contributor

BTW, when applying aliases, I think the issue would have occurred for all services as the PersistenceResource then used an anonymous implementation of the PersistenceItemInfo interface as well.

Yes, correct. That would have lead to the same issue.

Signed-off-by: Florian Hotze <dev@florianhotze.com>
@florian-h05

Copy link
Copy Markdown
Contributor Author

Marked as draft in favor of merging #5336, which also includes this fix.

}

public PersistenceItemInfoDTO(PersistenceItemInfo info, String alias) {
this.name = alias;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think you should do this. When using getPersistenceItemInfo, it should return the item names as stored in the storage, and all of them.
If you map back what you get from the storage to the configured alias, you will never be able to see what is in the persistence storage that does not map with an item name or configured alias.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've only transferred the logic as it was previously in PersistenceResource, but having it here can be misleading. Anyway, I want to close this PR in favor of your PR, so what's here doesn't really matter.

PersistenceServiceConfiguration config = persistenceServiceConfigurationRegistry.get(serviceId);
Map<String, String> aliases = config != null ? config.getAliases() : Map.of();
return service.getItemInfo().stream().map(info -> {
String alias = aliases.get(info.getName());

@mherwege mherwege Feb 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Don't try to map back to aliases here. Only the caller should do that.

This was another bug in this code by the way, which made the API inconsistent with the documentation.

@florian-h05
florian-h05 deleted the persistence-itemlist-null branch March 25, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants