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

Added support for JSON query parameters #43

Merged
merged 3 commits into from
Dec 30, 2019
Merged

Added support for JSON query parameters #43

merged 3 commits into from
Dec 30, 2019

Conversation

samuelnogueira
Copy link
Contributor

This is part of the effort required for #2

Only adds support for JSON serialized query parameters, no support for other forms of serialization (ex. application/xml) were added, or other types of parameters (ex. cookies).

A use case for this type of parameters is used as example in OpenAPI documentation:
https://swagger.io/docs/specification/describing-parameters/#schema-vs-content

Copy link
Member

@scaytrase scaytrase left a comment

Choose a reason for hiding this comment

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

Great work @samuelnogueira ! Some minor questions addressed

$parameter = $specs[$name];
$schema = $parameter->schema;
if ($schema === null) {
// There should be one and only one entry in 'content'.
Copy link
Member

Choose a reason for hiding this comment

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

It sounds that we rely on some kind of docs here. Can you share a link to it? What happens if we have zero entries? two entries?

Copy link
Contributor Author

@samuelnogueira samuelnogueira Dec 20, 2019

Choose a reason for hiding this comment

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

It sounds that we rely on some kind of docs here. Can you share a link to it?

I found strict rules on how to use schema/content in Swagger Editor. By the magic of testing all possible combinations, I found these are considered structural errors:

  1. Having both schema and content
  2. Not having schema or content
  3. Having content with no properties
  4. Having content with more than 1 properties

The documentation in Swagger website is a bit vague here, I could only find this relevant quote:
https://swagger.io/docs/specification/describing-parameters/#schema-vs-content

To describe the parameter contents, you can use either the schema or content keyword. They are mutually exclusive (...)

It looks good enough to me, so I'll add a link to it in the comments.,

What happens if we have zero entries? two entries?

I think we should throw an \League\OpenAPIValidation\Schema\Exception\InvalidSchema for these cases. Will change the implementation accordingly, please let me know afterwards if you agree @scaytrase .

Copy link
Member

Choose a reason for hiding this comment

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

Yea, I've checked editor too, we should throw invalid schema for count !== 1

if (preg_match('#^application/.*json$#', $contentType)) {
$value = json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw InvalidQueryArgs::becauseValueIsNotValidJson(json_last_error_msg(), $argumentName, $addr);
Copy link
Member

Choose a reason for hiding this comment

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

How about more generic becauseDoesNotMatchContentType or something like this? We can reuse it later for XML or other types


declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\FromCommunity;
Copy link
Member

Choose a reason for hiding this comment

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

I hope "FromCommunity" namespace exist to pin reproducing cases from different issues discussions into test flow. Since this is a general feature implementation, I suggest you to relocate this code to namespace related to new featuers

Samuel Nogueira added 2 commits December 20, 2019 17:59
with the intent of having less code changes to add deserialization support for cookies, headers and path params.
+ Ambiguous/invalid content/schema specification now throws InvalidSchema exception
Copy link
Member

@scaytrase scaytrase left a comment

Choose a reason for hiding this comment

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

LGTM 👍

return $value;
}

private function isJsonContentType() : bool
Copy link
Member

Choose a reason for hiding this comment

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

As a future improvement (not required for this PR) we can implement typed extensions for this class, like JsonRequestParameter, PlainRequestParameter instead of passing content-type and checking it on each deserialization

@scaytrase
Copy link
Member

Sorry for late review, great work @samuelnogueira !

@scaytrase scaytrase merged commit 7c5fbc7 into thephpleague:master Dec 30, 2019
@samuelnogueira
Copy link
Contributor Author

@scaytrase it's holiday season, the review wasn't late at all! Thank you for taking the time to review and merge the PR, it's much appreciated.

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

Successfully merging this pull request may close these issues.

2 participants