66016[TASK] Allow more types as payload in JsonResponse
Commit message
Review
-
Code review CR 0
-
Verified V 0
Issues 1
Files 1
Review log 1416
-
Georg Ringer uploaded patch set 1 10/05/20, 17:40
-
Georg Ringer commented on patch set 1 10/05/20, 17:40
This change is ready for review. -
TYPO3com voted on patch set 1 V +1 10/05/20, 17:53 Completed build in 13m 09s on Mon, 5 Oct, 05:53 PM Test Summary: 73743 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-6246
-
Alexander Schnitzler commented on patch set 1 CR -1 10/06/20, 14:09
JsonResponse.php:78I really dislike going back to mixed here. I would rather keep setPayload as is and maybe give the user direct access to $this->body or similar. -
eisbehr commented on patch set 1 V +1 CR +1 10/29/20, 11:59
I understand Alexanders concerns. But as `setPayload` directly delivers the data to `jsonEncode`, it's just wrong to have a strict type. because you can directly encode many types to json, like strings, integers, boolean, arrays, some objects and everything implementing json serializable. So why would you limit it to `array`? There is even a error check in `jsonEncode` if the data could be encoded. So I don't see any downside on going back to mixed here, as json is mixed too. -
Alexander Schnitzler commented on patch set 1 CR 0 10/29/20, 12:04
I will remove my -1 but still think having such an open api is not a good idea. I would rather opt for having the JsonResponse not doing the encoding and just accepting the json string instead because it's not the responsibility of the response to manipulate your data. -
Aggy voted on patch set 1 V +1 CR +1 10/29/20, 14:04
-
Frank Nägler commented on patch set 1 CR -1 11/22/20, 19:25
JsonResponse.php:78ReplyI agree, this is not an option from my point of view. I would opt for adding a new method like setSerializablePayload(JsonSerializable $object, $encodingOptions = self::DEFAULT_JSON_FLAGS): JsonResponse this way we make the code stronger and not weaker. -
eisbehr commented on patch set 1 11/22/20, 19:35
JsonResponse.php:78ReplyGreat idea. And so please add the following functions too: - setStringPayload - setBooleanPayload - setIntegerPayload - setFloatPayload - setDoublePayload - ... Oh, and how to use this functions then? Maybe a switch by variable type, because for every data type we need to call a different function? Sorry for that bit of sarcasm here. ;) Php's "json_encode" already accept "mixed", so why should the TYPO3 Core limit this possibilities? It is a JSON response, so it should be able to respond all types that JSON can have. And that are more than just "array" and "JsonSerializable". -
Alexander Schnitzler commented on patch set 1 11/22/20, 19:43
JsonResponse.php:78ReplyFollowing that sarcasm: Guess what, json is always a STRING. ever thought about that? So coming back to my former statement: Why let the JsonResponse do the transformation to a JSON string in the first place? The HtmlResponse also just accepts a string and doesn't provide functionality to transform arbitrary data to html and frankly, we wouldn't do so for xml or any other data type either. Currently we are trading type safety for a oneliner shortcut. How smart is that? -
eisbehr commented on patch set 1 11/22/20, 19:59
JsonResponse.php:78ReplyAs JSON is always a string, the data coming out of (or going into) isn't. And nor is it always an array. And what type of safety are you losing? If the data can't be converted, you will be getting an exception. But everything that can be converted will be. Actually I really don't understand what your safety point is here. Please clear that up for me. Thanks But I understand your point, don't wrapping anything around the jsonResponse. But removing the "json_encode" from there and just letting output the string has two problem imo: 1. You loose backward compatibility and have to do some kind of deprecation, much more work. 2. You then will truly loose safety. Because now the functions will check if the content can be converted to JSON and throw an exception otherwise. You will loose this completely if you let just input the JSON string and respond it. So for that reason I personally would stick with encoding there like it this. -
Alexander Schnitzler commented on patch set 1 11/23/20, 11:13
JsonResponse.php:78Reply> And what type of safety are you losing? If the data can't be converted, you will be getting an exception. Following that logic we don't need type hints at all because if we screw up, php throws a TypeError... The thing is: I want the core to follow the single responsibility pattern. Especially in those areas where we have PSR's and follow them. Responses have a well defined api, so do streams. And they handle strings only. How you end up with a string in userland code is your responsibility and if you make the application throw an exception it's your problem. But as soon as we widen the accepted types, people expect the core to not throw an exception and do whatever possible to make json_encode not fail. > But my object implements __toString() and that returns json, can't the core handle my edge case as well? It can but it shouldn't. Next argument: With type hints, we support static code analysis which tells people when to expect errors. Without type hints and support for edge cases we are blind. > 1. You loose backward compatibility and have to do some kind of deprecation, much more work. Yes, that's bad but we know ways around it. > 2. You then will truly loose safety. Because now the functions will check if the content can be converted to JSON and throw an exception otherwise. You will loose this completely if you let just input the JSON string and respond it. So for that reason I personally would stick with encoding there like it this. I don't follow that argument. json_encode does no checks at all. It simply fails if you input malformed data and throws an exception. A check would be to validate against a schema or such. It's even worse than that. There are around 10 different possible errors (https://www.php.net/manual/en/function.json-last-error.php) and the core currently doesn't let you know the exact issue. Well, you can add JSON_THROW_ON_ERROR and change the behaviour of this method and have it throw another exception than stated in the annotation. But to me that's just one more argument for removing this code from the core and let the user do it according to his/her needs. -
Alexander Schnitzler commented on patch set 1 11/23/20, 11:21
JsonResponse.php:78ReplyComing back to a more productive approach. I would rather implement a new JsonStream object which implements the StreamInterface and create a JsonStreamFactory with additional factory methods for several data types. Then we can simply use the PSR api and deprecate this method. See https://www.php-fig.org/psr/psr-7/#34-psrhttpmessagestreaminterface and https://www.php-fig.org/psr/psr-17/#24-streamfactoryinterface. -
TYPO3com commented on patch set 1 11/23/20, 14:14 Completed build in 17m 08s on Mon, 23 Nov, 02:14 PM Test Summary: 32391 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-7516
-
Oli Bartsch commented on patch set 1 11/24/20, 10:00
JsonResponse.php:78Reply UnresolvedI'm totally with Alexander. The response object should not handle such transformation. This must be the responsibility of a factory, like Alexander mentioned in his last comment. Therefore +1 for creating such factory and deprecating / removing? (anyone noticed that JsonResponse is actually @internal?) this method from the response object. -
Georg Ringer abandoned the change 10/19/21, 18:48
abandoned, no interest