Skip to content

[HttpFoundation] Fix TypeError: Argument 1 passed to JsonResponse::setJson() must be of the type string, object given #39271

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

Merged
merged 1 commit into from
Dec 7, 2020

Conversation

sidz
Copy link
Contributor

@sidz sidz commented Dec 1, 2020

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

This PR fixes the type error when user set null as data in the new JsonResponse(null, 200, [], true) and true in the fourth argument to mark that value is json.

TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\JsonResponse::setJson() must be of the type string, object given, called in /home/projects/symfony/src/Symfony/Component/HttpFoundation/JsonResponse.php on line 52

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 5.x branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@derrabus
Copy link
Member

derrabus commented Dec 1, 2020

By passing true as fourth argument, you're telling JsonResponse that the data you pass is already encoded JSON which is by definition a string. We can think about throwing an exception that is more helpful, but apart from that, there's nothing to fix: The call is broken and the error you're receiving is correct.

@sidz
Copy link
Contributor Author

sidz commented Dec 1, 2020

@derrabus I'm okay to throw an InvalidArgument exception but IMO this part of code shouldn't throw TypeError exception. WDYT?

@derrabus
Copy link
Member

derrabus commented Dec 1, 2020

this part of code shouldn't throw TypeError exception

Why not? We're expecting a string in that context and got null. This justifies a TypeError imho.

What I do understand is that the message of the TypeError is confusing because you did not actually call the method that the error you got complains about.

@sidz
Copy link
Contributor Author

sidz commented Dec 1, 2020

@derrabus found that there was a different kind of exception https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/HttpFoundation/Response.php#L403-L412 which was removed with type-hints.

So I've added almost the same exception and message

@derrabus
Copy link
Member

derrabus commented Dec 1, 2020

Right, we used that exception back in the PHP 5 days where we did not have TypeError. I don't have a super-strong opinion on this topic, but I would still prefer TypeError, tbh.

@sidz
Copy link
Contributor Author

sidz commented Dec 2, 2020

BTW I have a strong feeling that the first argument of fromJsonString method shouldn't be optional and it should expect to get a string only:

public static function fromJsonString(string $data, int $status = 200, array $headers = [])

Because it doesn't make a lot of sense to allow pass NULL that which will cause an exception in on 100% cases.

p.s. But I suppose it will be a BC break and shouldn't be in this PR. WDYT @derrabus ?

Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

I like the PR so far, just a minor suggestion.

@derrabus derrabus added the DX DX = Developer eXperience (anything that improves the experience of using Symfony) label Dec 2, 2020
@derrabus
Copy link
Member

derrabus commented Dec 2, 2020

You are right about fromJsonString(). This is broken. Let me check when this broke. This might change everything. 🙈

@derrabus
Copy link
Member

derrabus commented Dec 2, 2020

Okay, so fromJsonString() without arguments or with null kind of has always been broken. I've checked on 3.4 and 4.4 and got the error that you encountered as well. Thus, removing the default value there shouldn't be a breaking change, imho.

But this also tells me that we should apply your change to the 4.4 branch. Can you rebase?

@derrabus derrabus modified the milestones: 5.1, 4.4 Dec 2, 2020
@sidz sidz changed the base branch from 5.1 to 4.4 December 3, 2020 20:05
@sidz sidz requested a review from derrabus December 3, 2020 20:05
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

Looks good. Just two CS issues from fabbot.

@derrabus
Copy link
Member

derrabus commented Dec 6, 2020

I think we need to add this to the require section of the composer.json file of HttpFoundation in order to make the tests pass:

"symfony/polyfill-php80": "^1.15"

Apart from that, the PR looks good to me.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank you for this PR.
I agree, this is something that should be fixed. But a small BC break has snuck in :/

We need to fix it in another way.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Awesome.

I understand now and I like it =)

…tJson() must be of the type string, object given
@derrabus
Copy link
Member

derrabus commented Dec 7, 2020

Thank you @sidz.

@derrabus derrabus merged commit 306914a into symfony:4.4 Dec 7, 2020
This was referenced Dec 18, 2020
@sidz sidz deleted the type-casting-issue branch December 18, 2020 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug DX DX = Developer eXperience (anything that improves the experience of using Symfony) HttpFoundation Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants