Background: Some properties will throw EntityMetaDataWrapperException when empty, but this need not be fatal in many cases. This is frequently handled by existing code with a try{} and an empty catch{}, for example, in RestWSBaseFormat::getData.

It looks like we need this same technique in restws_property_access_filter().

Steps to reproduce the bug:

1.) Have some nodes, or create one.
2.) Enable Book module and restws.
2.) As a user with sufficient permission, request the node that is NOT assigned to a book. (which should be any node assuming you just enabled book module for the first time.

Results:
Fatal EntityMetaDataWrapperException.

Expected Result:
Node should be returned as without book module enabled.

Patch provided.

CommentFileSizeAuthor
empty-property.patch663 bytesmatt2000

Comments

matt2000’s picture

bump. Can I get a maintainer response? Thanks.

klausi’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Makes sense. Can we get a test case for that?

joachim’s picture

I would actually say that is a bug in entity_metadata_book_get_properties(). Entity API is adding the 'book' property to all nodes, irrespective of whether they are within books. Hence the callback it defines for that property should tolerate that not all nodes have it.

klausi’s picture

Not the callback should be fixed but entity_metadata_book_entity_property_info_alter() should register the property under the "bundles" key, so that it only appears on the book node type.

Should we still keep this exception handler in RESTWS for robustness?

wodenx’s picture

@klausi: entity_api can't register the property under the bundles key, because any node type can conceivably be a book page.

@joachim: what would you suggest as the appropriate behavior if the node doesn't belong to a book?

I still think it makes sense for the services to ignore properties that "cause problems". As the OP said, there's no reason for this to be a fatal error. Any module could add properties to an entity, and throw exceptions if those properties are somehow invalid.

joachim’s picture

> As the OP said, there's no reason for this to be a fatal error. Any module could add properties to an entity, and throw exceptions if those properties are somehow invalid.

Is that really the way the Entity Metadata API is meant to work? Wrappers throw exceptions for properties that are not defined, but if entity_property_info has said that property foo exists for this entity type and bundle, then the metadata system should honour that contacts and not throw an exception for it, surely!

> @joachim: what would you suggest as the appropriate behavior if the node doesn't belong to a book?

NULL?

wodenx’s picture

if entity_property_info has said that property foo exists for this entity type and bundle, then the metadata system should honour that contacts and not throw an exception for it, surely!

I agree with you, but what do we lose by safeguarding against a violation of this contract? If trying to fetch a property raises an exception, isn't it better for the service not to fail utterly?

NULL?

The problem with NULL is that Entity API interprets it as "not yet cached" - and tries to get it from the parent. Consider

$wrapper = entity_metadata_wrapper('node', $non_book_node);
$wrapper->book->author->value();

which will throw an exception "Unable to get the data property author as the parent data structure is not set."

In general, I think Entity API's handling of missing data could be more robust, and return NULL in a number of places where it throws exceptions, but that's a fairly major change in behavior. In the meanwhile, why not protect ourselves?

wodenx’s picture

Upon consideration, i think that @joachim proposes the correct solution in #6. Please see #1330086: wrapper throws exceptions for properties that are declared in the info.

wodenx’s picture

Issue summary: View changes

Trying to clarify the background info

klausi’s picture

Issue summary: View changes
Status: Needs work » Closed (duplicate)