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

Db Connected Rest service empty response on http 200. #70

Open
michalbundyra opened this issue Jan 10, 2020 · 11 comments
Open

Db Connected Rest service empty response on http 200. #70

michalbundyra opened this issue Jan 10, 2020 · 11 comments

Comments

@michalbundyra
Copy link
Member

I've created 3 database tables. City, State, Country. Inserted one row for each of them. Created those 3 db connected rest services, but when i try to use the get method for State, doesn't matter if using the id param or not, it returns empty response with http 200.

Didn't modify anything to any of the services, and the other ones are working. Also tried to delete it and create again. My guess is that its some bug with the service name (State might be a reserved word or something).

Apart from that, couldn't figure out what is going on. Tried to var_dump $collection before the return of RestController and it actually returns the expected collection with one result in it, but couldn't figure out to where this object is returned.

@edit

Figured it out.

json_encode is failing in https://github.com/zendframework/zf2/blob/master/library/Zend/Json/Json.php#L119
with message:
Malformed UTF-8 characters, possibly incorrectly encoded.

which then i figured out that was because my incorrect database adapter charset configuration.

Anyway, it would be very nice to get this json_encode error as a ApiProblem instead of a blank response with http 200.


Originally posted by @fabiocarneiro at zfcampus/zf-apigility#42

@michalbundyra
Copy link
Member Author

Turn on display_errors, and set your error_reporting to -1. PHP returns a 200 status when it has a fatal error; making these changes should give you some debugging information.


Originally posted by @weierophinney at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

Are you sure there is an error output for json_encode? There are two error functions json_​last_​error_​msg and json_​last_​error. Even with php errors disabled, the api should return an apiproblem if there is a apiproblem, am i wrong?


Originally posted by @fabiocarneiro at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

In zf-content-negotiation, we have a custom JsonModel, which is the one used by default with services created by Apigility. We use Zend\Json\Json::encode() to encode JSON; if this returns a boolean false, we throw an exception based on the results of json_last_error(). You can see the logic in the source code.

Technically, this should be a rendering error, which should trigger an EVENT_RENDER_ERROR -- which ApiProblem listens to, and thus should mean you should get an application/problem+json response. Any chance you can send me some of the data that caused problems for you so I can try and recreate the situation locally and see if I observe the lack of problem details?


Originally posted by @weierophinney at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

I just created a utf8 database, and then added data with some utf8 characters like ã, õ, ... then created a database driver and didn't specified the charset as utf8. I guess it tries to use iso-8859-1 as default, and then json_encode fails with encoding. Adding utf-8 characters is very important, because without it, it will properly encode the json even with wrong chaset.


Originally posted by @fabiocarneiro at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

@michalbundyra
Copy link
Member Author

👍
Home / Database Adapters / Charset <- utf8


Originally posted by @olavocneto at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

@olavocneto this is a very old issue. I actually knew i could configure it, but the error isn't very clarifying. The problem was the error reporting itself. That happens because how json_encode works and this behavior being ignored by apigility.


Originally posted by @fabiocarneiro at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

@fabiocarneiro Yes, I agree. In fact, you helped me. Thank u!


Originally posted by @olavocneto at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

even this is an old issue and I could fix my error with the help of this issue, I have to answer it:

@weierophinney: You will get no error if you have active HalJson, no logging no output
only a blank 200 page. If you switch only to json for content negotiation
you will the error with the malformated character. Does the HalJson have different error handling.

possibly apigiity can show the same error even HalJson ist active by default.

Thank you


Originally posted by @Bemazo at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

Bump


Originally posted by @gsomoza at zfcampus/zf-apigility#42 (comment)

@michalbundyra
Copy link
Member Author

@weierophinney I actually found out my code doesn't use JsonModel. I'm using Apigility + Doctrine (using the api-skeleton libs). By the time the payload gets to the JsonRenderer (Zend View) it's just a plain PHP array, and that piece of code does NOT handle JSON errors whatsoever, which is kind of suprising. Source here: https://github.com/zendframework/zend-view/blob/master/src/Renderer/JsonRenderer.php#L154

Should the system be hydrating a JsonModel instead of using a plain PHP array? Or should the code in JsonRenderer be fixed to throw errors? I lean towards the latter but let me know what you think..


Originally posted by @gsomoza at zfcampus/zf-apigility#42 (comment)

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

No branches or pull requests

1 participant