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

feat: encoding > allowReserved support #630

Merged
merged 16 commits into from
Sep 26, 2019
Merged

Conversation

karol-maciaszek
Copy link
Contributor

@karol-maciaszek karol-maciaszek commented Sep 20, 2019

Closes #497

WIP: needs code style refinement

Checklist

  • Tests have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce?

  1. support for encoding.allowReserved flag
  2. encoding.style is only applied for application/application/x-www-form-urlencoded
  3. encoding.style is applied to all uri parts (before it was only applied to first one)
  4. i'm using qs package to decode UrlEncoded payload. It was used internally by fastify's form-body plugin

Does this PR introduce a breaking change?

No

@karol-maciaszek karol-maciaszek added the WIP Work In Progress label Sep 20, 2019
@karol-maciaszek karol-maciaszek changed the title feat: encoding > allowReserved support [WIP] feat: encoding > allowReserved support Sep 20, 2019
@karol-maciaszek karol-maciaszek changed the title [WIP] feat: encoding > allowReserved support feat: encoding > allowReserved support Sep 24, 2019
@karol-maciaszek karol-maciaszek removed the WIP Work In Progress label Sep 24, 2019
Copy link
Contributor

@XVincentX XVincentX left a comment

Choose a reason for hiding this comment

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

I've dropped some comments, but the direction is overall ok!

Can you add a changelog line?

packages/http/src/validator/validators/body.ts Outdated Show resolved Hide resolved
packages/http/package.json Outdated Show resolved Hide resolved
for (const encoding of encodings) {
const allowReserved = get(encoding, 'allowReserved', false);
const property = encoding.property;
const value = shallowParsedTarget[property];
Copy link
Contributor

Choose a reason for hiding this comment

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

What does shallow specifically mean here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had trouble naming that.

Basically it should mean that URL is split by & and the key-val pairs are extracted. But percent-encoded entities hadn't been decoded yet.

We need it in that form because:

  1. we need key-val pairs in order to match key names with relevant encoding definitions
  2. we need before-percent-decoding values in order to look for forbidden characters

Copy link
Contributor

Choose a reason for hiding this comment

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

Path and queryParams?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quite unsure what do you mean by path and queryParams. We are dealing here with body payload.

@@ -27,7 +27,7 @@ paths:
====server====
mock -p 4010
====command====
curl -i -X POST http://localhost:4010/path -H "Content-Type: application/x-www-form-urlencoded" --data "status=ooopsie!"
curl -i -X POST http://localhost:4010/path -H "Content-Type: application/x-www-form-urlencoded" --data "status=ooopsie%21"
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to make sure I understand this correctly — since ! is a reserved character and allowReserved is false by default, you have effectively encoded it so that it passes the validation and goes through the following validation step, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly. And this is the place where I doubt in OpenApi 3 spec. I mean having allowReserved=false by default sounds reasonable. However, I think it will end with lots of people complaining that prism has a bug.

Copy link
Contributor

Choose a reason for hiding this comment

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

It depends, usually the clients handle the encoding automatically and even curl has an option
--data-urlencode.

//cc @philsturgeon for awareness but I do not see this as a problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use --data-urlencode 👍

@karol-maciaszek
Copy link
Contributor Author

I've dropped some comments, but the direction is overall ok!

Can you add a changelog line?

Changelog line added 👍

@karol-maciaszek karol-maciaszek self-assigned this Sep 25, 2019
Copy link
Contributor

@XVincentX XVincentX 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; unfortunately we had to move some of the logic in the http package but I guess there's no really other way.

@philsturgeon do you want to have a quick look here just to make sure it's ok from a feature standpoint?

If we could NOT support this we'd keep the things a little bit better in the place they should be and be like man respect the standard

BTW — personal opinion, this OAS flag is just…non sense and I would love ❤️ to know the rationale behind this.

test-harness/specs/parameters-ac7.oas2.txt Outdated Show resolved Hide resolved
@@ -27,7 +27,7 @@ paths:
====server====
mock -p 4010
====command====
curl -i -X POST http://localhost:4010/path -H "Content-Type: application/x-www-form-urlencoded" --data "status=ooopsie!"
curl -i -X POST http://localhost:4010/path -H "Content-Type: application/x-www-form-urlencoded" --data "status=ooopsie%21"
Copy link
Contributor

Choose a reason for hiding this comment

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

It depends, usually the clients handle the encoding automatically and even curl has an option
--data-urlencode.

//cc @philsturgeon for awareness but I do not see this as a problem

@philsturgeon
Copy link
Contributor

Looks great feature wise!

@XVincentX XVincentX merged commit 6407716 into master Sep 26, 2019
@XVincentX XVincentX deleted the feat/allow-reserved branch September 26, 2019 09:50
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.

Support 'allowReserved' property when validating form-data body
3 participants