Problem/Motivation
The TypedData Any defines this type as:
The "any" data type does not implement a list or complex data interface, nor is it mappable to any primitive type. Thus, it may contain any PHP data for which no further metadata is available.
It explicitly forbids the value it stores "implement a list or complex data interface, nor is it mappable to any primitive type". However, its code implementation does not check those conditions above.
Proposed resolution
In the implementation, override the setValue(), then check the conditions, and throw exceptions when the conditions are not met.
Update the docs to make it clear
Remaining tasks
1. Write the patch above, and see how much impact this change will have in Core
2. Decide if we can make the change for D8, if not, maybe deprecate Any, and create "AnyBut" type?
Test documentation with a few people, and if possible, provide an example
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 2915052-11.patch | 878 bytes | wim leers |
| #9 | interdiff-2915052-5-8.txt | 1.11 KB | lawxen |
| #9 | 2915052-8.patch | 600 bytes | lawxen |
| #5 | interdiff-2915052-4-5.txt | 1.44 KB | lawxen |
| #5 | 2915052-5.patch | 911 bytes | lawxen |
Comments
Comment #2
lawxen commentedComment #3
lawxen commented@skyredwang
The goal may be not ideal:
Reason:
Because no other type data can be or should be used to store object.
Conclusion
possible situation:
Comment #4
lawxen commentedMake class Any overwrite setValue to add conditions
Comment #5
lawxen commentedList:
core/core.api.php#n994
core/lib/Drupal/Core/TypedData/Annotation/DataType.php#n21
Map:
core/core.api.php#n984
We can see that both *map* and *list* represents a diferent subset of Array
So all map and list's setValue() check whether the value is an array is correct(the father of data that map and list should reprents)
core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php#n58
core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php#n75
Come to Any
Any:
core/lib/Drupal/Core/TypedData/Plugin/DataType/Any.php#n10
Let's fouced on: any PHP data for "which no further metadata is available"
The core/core.api.php#n941 describe the metadata here:
Conclusion
The list or complex data interface corresponding to the typed data, not the value stored by typed data
So we shouldn't forbid any typed data store data which implement ComplexDataInterface,ListInterface or PrimiteInterface
So If we add condition on any.php's setValue() according to doc(Not sure whether we should add condition)
we should forbid the data which implements DataDefinitionInterface
If the 'any' doc is the formal specification,The above is the right formal implementation
Above formal implementation make a restrictions on 'any'
we can't store a typed data to a typed data of 'any'
Comment #6
dawehnerIsn't the usecase of any when you simply don't know upfront? Just imagine storing arbitrary JSON, which could be all of the different variants you listed above ...
Comment #7
tedbowI think agree with @dawehner here
The doc in
Anymight not be very clear.
I wonder the intention of the last sentence is actually
Meaning you can store values have metaData but you if you have metadata beforehand about what type of data you will be storing you should not use Any.
Comment #8
lawxen commented@tedbow
I agree with you and @dawehner
I add the word 'beforehand' @tedbow write to the doc on 2915052-8.patch, Please forgive my directing use of what you write, I want to accelerate the fixing of the issue, then hope @fago can review the patch, he wrote the origin comment on https://www.drupal.org/node/1913328
Comment #9
lawxen commentedComment #10
skyredwangWhy don't we get rid off the first sentence, and change the documentation to:
It may contain any PHP data for which no further metadata is available beforehand.Any really means any.
Comment #11
wim leersAPI-First issue #2915705: TypedData 'Any' can't be normalized to array if it stores an object triggered this.
I agree with #10. But I think we can still include the relevant bit of the first sentence, in a much clearer way (thanks to #10). Since I didn't do literally what #10 suggested, I can't RTBC this.
Comment #12
wim leersComment #13
borisson_This makes it a lot cleaner, thanks Wim!
Comment #14
xjmHm, so I'm not sure just swapping the order of the sentences makes it any clearer...
How about adding an example?
Comment #15
xjmAlso the summary claims this is a data model change but the issue title says it's a docs fix?
Thanks!
Comment #16
skyredwangComment #27
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
Tagging for IS per #14 asking for an example.
Comment #28
lawxen commented