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

Allow a POST request to have an empty body #1786

Conversation

gorghoa
Copy link
Contributor

@gorghoa gorghoa commented Mar 21, 2018

Q A
Bug fix? no
New feature? somehow
BC breaks? maybe?
Deprecations? no
Tests pass? dunno yet
Fixed tickets none
License MIT
Doc PR none

Hi,

A POST request with an empty body and a content-type set to json throws a syntax error exception at deserialization.

This PR propose to allow empty content for POST request (just like empty content for PUT is already ok).

Didn’t find any strong indication to forbid empty post request by a quick search.

see also: http://lists.w3.org/Archives/Public/ietf-http-wg/2010JulSep/0272.html

(note, will write test if the idea is accepted ;) )

{
    "@context": "/api/v2/contexts/Error",
    "@type": "hydra:Error",
    "hydra:title": "An error occurred",
    "hydra:description": "Syntax error",
    "trace": [
        {
            "namespace": "",
            "short_class": "",
            "class": "",
            "type": "",
            "function": "",
            "file": "/var/www/w2c/api/vendor/symfony/serializer/Encoder/JsonDecode.php",
            "line": 78,
            "args": []
        },
        
    ]
}```

@soyuka
Copy link
Member

soyuka commented Mar 21, 2018

There's already an issue (maybe even a PR) on that IIRC

@gorghoa
Copy link
Contributor Author

gorghoa commented Mar 21, 2018

thanks @soyuka: this one #1282? If so, we should probably keep the discussion in this issue then 👍 )

(see also #805 💪)

@dunglas
Copy link
Member

dunglas commented Apr 1, 2018

👍 on my side, when the tests will be green.

|| ('' === ($requestContent = $request->getContent()) && $request->isMethod('PUT'))
|| (
'' === ($requestContent = $request->getContent())
&& in_array($request->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT])
Copy link
Member

Choose a reason for hiding this comment

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

Can you use two === instead please (it's faster).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no problem.

@gorghoa gorghoa force-pushed the allow-empty-request-content-on-body-method branch from 9fdd5f9 to 8d40983 Compare April 2, 2018 19:48
@gorghoa
Copy link
Contributor Author

gorghoa commented Apr 2, 2018

Thanks @dunglas, will add some tests.

|| (
'' === ($requestContent = $request->getContent())
&& (Request::METHOD_POST === $request->getMethod() || Request::METHOD_PUT === $request->getMethod())
)
Copy link
Member

Choose a reason for hiding this comment

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

May we add some behat tests?

Copy link
Contributor Author

@gorghoa gorghoa Apr 3, 2018

Choose a reason for hiding this comment

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

Sure, Didn’t do it at first because I wasn’t sure if the mere idea of an empty POST would be accepted.

@soyuka, any idea what .feature file would be the coziest home for those tests?

this one: https://github.com/api-platform/core/blob/master/features/main/operation.feature ?

Copy link
Member

Choose a reason for hiding this comment

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

yes looks totally appropriate :)

|| ('' === ($requestContent = $request->getContent()) && $request->isMethod('PUT'))
|| (
'' === ($requestContent = $request->getContent())
&& (Request::METHOD_POST === $request->getMethod() || Request::METHOD_PUT === $request->getMethod())
Copy link
Member

Choose a reason for hiding this comment

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

Can you store the result of getMethod() in a variable to avoid an unnecessary function call? As it's in a listener it can be performance sensitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thanks for the feedback

if (
$request->isMethodSafe(false)
|| $request->isMethod('DELETE')
|| Request::METHOD_DELETE === $method
Copy link
Member

Choose a reason for hiding this comment

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

Please, don't use Request::METHOD_* constants!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thanks :)

Copy link
Member

@meyerbaptiste meyerbaptiste left a comment

Choose a reason for hiding this comment

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

Write a test and then it will be good!

@gorghoa
Copy link
Contributor Author

gorghoa commented Apr 3, 2018

Unit tests updated.

Let me know if a behat test is mandatory.

@Simperfit
Copy link
Contributor

Could you please rebase with master ?

I think a behat test could be handy too!

@gorghoa gorghoa force-pushed the allow-empty-request-content-on-body-method branch from 2d89ecf to a449fb2 Compare April 6, 2018 09:10
@gorghoa gorghoa force-pushed the allow-empty-request-content-on-body-method branch from a449fb2 to 6d39e76 Compare April 6, 2018 09:13
@soyuka
Copy link
Member

soyuka commented Apr 6, 2018

Let me know if a behat test is mandatory.

I'd like one 😄

@gorghoa
Copy link
Contributor Author

gorghoa commented Apr 6, 2018

thanks @Simperfit, rebased.

Regarding the behat test: I think this PR promptly fixes an inconsistent behavior. It is unit tested. It’s a fast and sufficient test IMHO.

I think the main issue – with more work/design to do – remains open, discussed here : #1282

@Simperfit Simperfit merged commit dd03d9d into api-platform:master Apr 6, 2018
@Simperfit
Copy link
Contributor

Thanks @gorghoa.

@gorghoa gorghoa deleted the allow-empty-request-content-on-body-method branch April 6, 2018 09:44
@Taluu
Copy link
Contributor

Taluu commented Jun 1, 2018

Looks like this wasn't applied to 2.2.x ? Anyway to have this merged in 2.2.x ? Because IMO this is more a bug than a new feature...

@teohhanhui
Copy link
Contributor

It was considered a new feature, so not merged in 2.2.

@teohhanhui
Copy link
Contributor

Hmm... I've been bitten by this. Seems like this was a bad decision.

@teohhanhui
Copy link
Contributor

See #2757

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants