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 falsy values (except undefined) as a valid body #1825

Merged
merged 2 commits into from
Aug 30, 2024

Conversation

goce-cz
Copy link
Contributor

@goce-cz goce-cz commented Aug 7, 2024

Changes

Resolves #1695

This PR ensures that all falsy values except undefined i.e. null, false, 0 and "" are treated as a valid body. This means:

  • going through bodySerializer, when specified
  • sending the body to the server

How to Review

  • Check the added unit tests which describe the desired behavior.
  • The change in implementation itself is ridiculously simple.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
    • I didn't find any section describing the behavior.
  • pnpm run update:examples run (only applicable for openapi-typescript)

@goce-cz goce-cz requested a review from a team as a code owner August 7, 2024 09:18
Copy link

changeset-bot bot commented Aug 7, 2024

🦋 Changeset detected

Latest commit: f79fa25

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

fetchOptions: FetchOptions<any>;
}) => {
const client = createClient<any>({ baseUrl, bodySerializer: options.bodySerializer });
const { getRequest } = useMockRequestHandler({
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 assume this is piling one mock over the other. I didn't find any "cleanup" procedure in the existing tests, so piling several more mocks on top 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, missed that. Thanks 👍

@@ -1275,6 +1277,186 @@ describe("client", () => {
});
});

describe("body serialization", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these tests could be better placed next to other body tests

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 originally had them there, but then saw that the whole test group is within "TypeScript checks" and these have nothing to do with TypeScript.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right and it seems that most of the tests in this group are not even about types 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

That’s a good callout. We probably need to clean up our tests by moving TypeScript tests to actual Vitest type tests. This test file has also grown in size (which is a great problem to have) and can probably be broken up. All that to say, not too prescriptive about where this test goes; if it exists somewhere I’m happy 🙂

return { bodyUsed: request.bodyUsed, bodyText };
};

it.each(ALL_METHODS)("missing body (with body serializer) - %s", async (method) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should definitely use more often it.each to have cleaner tests while testing more cases. But in this case isn't it overkill to test every method each time as the logic tested is only about falsy values?

What about a single test it.each([{ body: undefined, except: false }, { body: 1, except: true }, ...])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But in this case isn't it overkill to test every method each time as the logic tested is only about falsy values?

There are differences in how GET and HEAD are used in some of the underlying implementation (not in this lib, hopefully) - some clients and servers disallow bodies for them. This includes the tools used in these tests. Having to think about these differences inspired me to rather have a comprehensive test of all methods. If you guys don't see it valuable, I'm OK with dropping it.

What about a single test it.each([{ body: undefined, except: false }, { body: 1, except: true }, ...])

I usually prefer the assertions to be as explicit as possible, hence the explicit expect().toBe(), but I don't mind pulling the expectations into the test arguments, if that's what you guys are used to. Matter of taste really.

@kerwanp
Copy link
Contributor

kerwanp commented Aug 7, 2024

Thanks for the contrib! I just added some comments regarding the tests.
I'm not experienced in writing tests so I'll let @drwpow review this.

But imo, a unit test must test a single feature, in this case falsy bodies. Testing that GET and POST have different behavior deserve its own test (that we don't have).

@goce-cz
Copy link
Contributor Author

goce-cz commented Aug 7, 2024

Testing that GET and POST have different behavior deserve its own test (that we don't have).

The fact that I'm running the tests over each method independently doesn't have anything to do with testing method behavior. It's just to ensure that the feature I am testing works consistently across all methods. See my reply to your comment above for more clarity.

@kerwanp kerwanp added the openapi-fetch Relevant to the openapi-fetch library label Aug 10, 2024
Copy link
Contributor

@drwpow drwpow 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 the fix! These are great tests.

⚠️ We need a changeset to merge (please see comment)!

Non-blocking suggestion: agree with @kerwanp on some minor suggestions for test cleanup. We could deduplicate a little bit. Agree with your points @goce-cz about being explicit, but a lot of the tests have quite a lot of shared overlap, and very minor differences, that are easy to miss when modifying, and a few could be combined with some more clever iterating as you have

@goce-cz goce-cz force-pushed the 1695-allow-falsy-values-as-body branch from 449d7e8 to e9d8506 Compare August 28, 2024 08:52
@goce-cz
Copy link
Contributor Author

goce-cz commented Aug 30, 2024

I rebased and added the change-set. Hopefully correctly.

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

Great, thank you!

@drwpow drwpow merged commit 6038f8f into openapi-ts:main Aug 30, 2024
8 checks passed
@github-actions github-actions bot mentioned this pull request Aug 30, 2024
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
Copy link
Contributor

Choose a reason for hiding this comment

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

Whoops! Didn’t catch this was for the wrong package. Should be openapi-fetch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh... crap. Sorry. I'll issue a fix PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh no, no worries; that’s easy to fix. It’s actually more important that you created the file in the correct PR, because then the changelog credits you correctly and links to the PR. But just pointing out for next time

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 see it has been already released. 😢

Copy link
Contributor

Choose a reason for hiding this comment

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

Yours was released correctly! 🙂 #1889

The openapi-typescript release was for an actual patch from another PR: #1890

@goce-cz goce-cz deleted the 1695-allow-falsy-values-as-body branch August 30, 2024 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

null and '' (empty string) are serializable to JSON and should therefore be considered a valid body payload
3 participants