Skip to content

use substring for content type parsing - #1372

Merged
pmcelhaney merged 5 commits into
counterfact:mainfrom
ihor-rud:main
Aug 20, 2025
Merged

use substring for content type parsing#1372
pmcelhaney merged 5 commits into
counterfact:mainfrom
ihor-rud:main

Conversation

@ihor-rud

Copy link
Copy Markdown
Contributor

fix for #1370

@changeset-bot

changeset-bot Bot commented Aug 18, 2025

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5a0393b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@ihor-rud

Copy link
Copy Markdown
Contributor Author

I tried to create a test for that issue in test/typescript-generator but I got completely lost. You can point me to the right direction or add missing parts yourself. Both will work for me

@dethell
dethell requested a review from Copilot August 18, 2025 19:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

Updates TypeScript utility types to use substring matching instead of exact key matching for content type parsing, addressing issue #1370 where content type parsing failed for media types with additional parameters.

  • Modified OmitAll type to use substring pattern matching instead of exact key comparison
  • Updated IfHasKey type to check for substring matches in object keys rather than exact key presence

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Comment thread src/server/types.ts
? Extract<keyof SomeObject, `${string}${FirstKey}${string}`> extends never
? IfHasKey<SomeObject, RestKeys, Yes, No>
: Yes
: No;

Copilot AI Aug 18, 2025

Copy link

Choose a reason for hiding this comment

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

The logic appears inverted. When a key is found (Extract returns non-never), the condition should return Yes, but currently it returns No via the IfHasKey recursive call. Consider changing to: Extract<keyof SomeObject, \${string}${FirstKey}${string}`> extends never ? IfHasKey<SomeObject, RestKeys, Yes, No> : Yes`

Suggested change
: No;
: No
: Yes;

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@ihor-rud I requested the Copilot review, but I'm uncertain about its suggestion regarding the inverted logic. Do you have any comment about this? I guess this is a great reason to have a test. If you have access to CoPilot I'd go ahead and ask it to write a test around this logic change as a first start on tests. If I'm able to free up time later this week I'll take a shot at writing a test if you (or Copilot) haven't gotten there first.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I requested the Copilot review, but I'm uncertain about its suggestion regarding the inverted logic. Do you have any comment about this?

Copilot is wrong here. Extract<keyof SomeObject, ${string}${FirstKey}${string}> returns never when there is no overlap between 1 and 2 arguments, so we check rest of keys.

I guess this is a great reason to have a test

I can try to write a type level test using tsd. I have no idea how to test it with jest in runtime because types will be gone at this point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made code more straightforward in last commit. Tests turnout to be more complex then I expected. The biggest issue is where to put them. OmitAll and IfHasKey are not part of public api. anyway here is how tsd tests could look like
https://gist.github.com/ihor-rud/5baaf3b4f3bac4166753c04111059ef1

@pmcelhaney pmcelhaney Aug 19, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We don't have tests for types yet. I would really like to get that going with something like tsd.

Once we get it going I believe an LLM could write most of the tests for us. We wouldn't need to check the LLM's work that closely, because if the tests pass, they're by definition correct.

With test coverage in place we will have the opportunity to refactor the types and make them easier to understand.

@ihor-rud ihor-rud Aug 19, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@pmcelhaney I just added tsd tests. you can run them using this command

yarn build && yarn test:tsd

@pmcelhaney
pmcelhaney enabled auto-merge August 20, 2025 17:47
@pmcelhaney

Copy link
Copy Markdown
Collaborator

Great contribution, thank you very much! Merging now and then I'll get a release out.

@pmcelhaney
pmcelhaney merged commit efe8c3e into counterfact:main Aug 20, 2025
5 checks passed
pmcelhaney added a commit that referenced this pull request Jan 13, 2026
pmcelhaney added a commit that referenced this pull request Jan 14, 2026
pmcelhaney added a commit that referenced this pull request Jan 14, 2026
changeset for revert change in #1372 which broke type shortcuts like .json(), .html(), etc.
@pmcelhaney pmcelhaney mentioned this pull request Jan 14, 2026
@pmcelhaney

Copy link
Copy Markdown
Collaborator

Hey @ihor-rud, it looks like this change actually broke the shortcuts. Instead of detecting application/json; charset=utf-8, it looks like it stopped detecting application/json and the other variants. I'm reverting the change for now, but I think we can figure it out. I've started breaking out the types into separate files so that we can more easily add unit tests.

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.

5 participants