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

Compare content type parameters separately #1159

Merged
merged 40 commits into from
May 12, 2020
Merged

Conversation

XVincentX
Copy link
Contributor

@XVincentX XVincentX commented May 8, 2020

This pull request implements a backup strategy to ignore parameters in content type when is possible. Since they're not standard (apart for the q value) we won't assume anything about it and drop them in some cases

Closes #1152

Copy link
Contributor

@marcelltoth marcelltoth left a comment

Choose a reason for hiding this comment

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

You could add a changelog if this is something that is user-visible. I'm not sure, TBH 😄

packages/http/src/mocker/negotiator/NegotiatorHelpers.ts Outdated Show resolved Hide resolved
packages/http/src/validator/index.ts Outdated Show resolved Hide resolved
@@ -1 +1 @@
* @XVincentX @marcelltoth @karol-maciaszek
* @XVincentX @karol-maciaszek
Copy link
Contributor Author

Choose a reason for hiding this comment

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

😢

Copy link
Contributor

Choose a reason for hiding this comment

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

💔

Copy link
Contributor

Choose a reason for hiding this comment

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

😩

Comment on lines 41 to 47
pipe(
mediaTypes
.map(mt => contentType.parse(mt))
.filter(mt => Object.keys(mt.parameters).length > 0)
.map(mt => mt.type),
fromArray,
O.chain(mediaTypesWithNoParameters => findBestHttpContentByMediaType(contents, mediaTypesWithNoParameters))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is basically a "retry with only the Q parameter"

exampleKey,
},
response
return pipe(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This diff is huge but all I did here for real was to move the if(mediatypes) in the pipe

@@ -289,19 +291,19 @@ const helpers = {
pipe(
previous,
O.alt(() => {
logger.trace(`Unable to find a ${statusCodes[index]} response definition`);
logger.debug(`Unable to find a ${statusCodes[index]} response definition`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

trace is not caught by our signals logger, so I moved it back to debug as it should be

findEmptyResponse(
response,
response.headers || [],
get(contentWithExamples, 'mediaType', get(responseWithSchema, 'schema')) || ['*/*']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

contentWithExamples is always undefined in this branch, that's why I've removed it

@XVincentX XVincentX requested a review from pytlesk4 May 12, 2020 00:11
@XVincentX XVincentX force-pushed the fix/ct-parameters branch 3 times, most recently from 7e37d17 to 2ab18ef Compare May 12, 2020 00:36
Copy link
Contributor

@karol-maciaszek karol-maciaszek left a comment

Choose a reason for hiding this comment

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

Good work and nice package of improvements. I dropped a few comments.

pipe(
mediaTypes
.map(mt => contentType.parse(mt))
.filter(mt => Object.keys(mt.parameters).filter(k => k !== 'q').length > 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.filter(mt => Object.keys(mt.parameters).filter(k => k !== 'q').length > 0)
.filter(({ parameters }) => Object.keys(parameters).find(k => k !== 'q'))

or for more clarity:

Suggested change
.filter(mt => Object.keys(mt.parameters).filter(k => k !== 'q').length > 0)
.filter(({ parameters }) => !!Object.keys(parameters).find(k => k !== 'q'))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah good point. @karol-maciaszek how about this: bbb4c73

@@ -10,7 +10,7 @@ describe('InternalHelpers', () => {
};

it('should respect the q parameter', () => {
const possibleResponse = findBestHttpContentByMediaType(avaiableResponses, [
const possibleResponse = findBestHttpContentByMediaType(avaiableResponses.contents, [
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const possibleResponse = findBestHttpContentByMediaType(avaiableResponses.contents, [
const possibleResponse = findBestHttpContentByMediaType(availableResponses.contents, [

Will need updates in the code above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

);
});

describe('multiple media types avaiable', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
describe('multiple media types avaiable', () => {
describe('multiple media types available', () => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 776 to 777
{ mediaType: 'application/json; version=1; q=1' },
{ mediaType: 'application/json; version=1; q=0.6' },
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
{ mediaType: 'application/json; version=1; q=1' },
{ mediaType: 'application/json; version=1; q=0.6' },
{ mediaType: 'application/json; version=1; q=0.6' },
{ mediaType: 'application/json; version=1; q=1' },

Copy link
Contributor Author

Choose a reason for hiding this comment

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

};

describe('and the requeste media type matches, but has parameter', () => {
const result = validator.validateMediaType([content], 'application/vnd.archa.api+json; version=1');
Copy link
Contributor

Choose a reason for hiding this comment

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

I have bad experience in tracking errors outside it() functions. Anything against moving to it()?

@XVincentX
Copy link
Contributor Author

You could add a changelog if this is something that is user-visible.

@marcelltoth I added the changelog line indeed.

XVincentX and others added 2 commits May 12, 2020 08:44
Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>
Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>
Copy link
Contributor

@karol-maciaszek karol-maciaszek left a comment

Choose a reason for hiding this comment

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

Ship it!

@XVincentX XVincentX merged commit 7f8fe02 into master May 12, 2020
@XVincentX XVincentX deleted the fix/ct-parameters branch May 12, 2020 13:50
XVincentX added a commit that referenced this pull request May 26, 2020
* compare parameters separately

* tmp: mediaType from nullable

* use isMatch

* Parse media type only once

* tmp changes

* keep the chain as much as possible

* add launch.json instructions

* remove incorrect assumption

* you can alwasy create an empty response

* avoid nesting

* create empty response cannot fail

* do not bloat with stuff we do not need

* restore stuff

* restore

* chore: restrict surface

* reduce stuff

* pipe all together

* replace with debug

* bye bye marcell

* test: add validator test

* use realish media types

* remove parameters and try again

* eeek

* docs: changelog

* test: add

* test: refactor and add

* test: harness

* remove only

* keep the q parameter

* add yet another test

* refactor: use some

* spell error

* spell error again

* reverse order to make sure test is not passing by chance

* move data retr in it block

* move tests in internal helpers where they should be2

* test fix

* Update packages/http/src/validator/__tests__/index.spec.ts

Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>

* Update packages/http/src/validator/__tests__/index.spec.ts

Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>

Co-authored-by: Karol Maciaszek <karol@maciaszek.pl>
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.

Issue in findBestHttpContentByMediaType due to inconsistent behaviour between accepts and type-is libraries
3 participants