[typescript-*]: Improve literal regexp for JSON MIME-type#23319
Open
sjoubert wants to merge 1 commit intoOpenAPITools:masterfrom
Open
[typescript-*]: Improve literal regexp for JSON MIME-type#23319sjoubert wants to merge 1 commit intoOpenAPITools:masterfrom
sjoubert wants to merge 1 commit intoOpenAPITools:masterfrom
Conversation
- Use a literal regex as suggested by SonarQube analysis typescript:S6325 - Remove the explicit test against json-patch that is already included in the regexp
Contributor
There was a problem hiding this comment.
No issues found across 55 files
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
macjohnny
reviewed
Mar 23, 2026
Member
macjohnny
left a comment
There was a problem hiding this comment.
thanks for your contribution!
i suggest to escape the /
| public isJsonMime(mime: string): boolean { | ||
| const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i'); | ||
| return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json'); | ||
| const jsonMime: RegExp = /^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$/i; |
Member
There was a problem hiding this comment.
Suggested change
| const jsonMime: RegExp = /^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$/i; | |
| const jsonMime: RegExp = /^(application\/json|[^;\/ \t]+\/[^;\/ \t]+[+]json)[ \t]*(;.*)?$/i; |
Contributor
Author
There was a problem hiding this comment.
I just tested escaping them but I get the "unecessary escape character" (typescript:S6535) warning. So I think we are good like this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes a report from SonarQube analysis (typescript:S6325) suggesting to use a literal regex instead of a
RegExpconstructor. There are also 2 report of useless escaping (typescript:S6535) of character/but these will disappear by using a literal.I realize that running a static analysis tool on generated code that I don't control is not the smartest thing. But so far the results were clean with our Sonar config, so I figured I might as well try to keep it that way with an upstream fix.
I only use
typescript-axiosbut found and fixed the same pattern in 3 other typescript clients.Here are a couple questions/points left to clarify:
/are now properly escaped. The 2 unescaped/in brackets should be fine but I'm not an javascript/typescript regexp expert. So if someone can confirm that the new regexp still does the correct thing that would be great.application/json-patch+jsonas I believe the regexp already matches it (including in a case-insensitive way)typescript-fetchclient has a different regexp. It include:?at the beginning of the 2 match groups. I don't see a reason to match an optional:, so my guess is that the original intent was to use?:instead, meaning "don't save the matched group". If this is correct, should I remove:?to match the other clients or fix it and use the correct?:on all 4 clients?PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04) @joscha (2024/10) @dennisameling (2026/02)
Summary by cubic
Switch JSON MIME-type detection to a literal regex and remove the redundant
application/json-patch+jsoncheck across TypeScript generators. This addresses SonarQube findings (S6325, S6535) and simplifies the code.new RegExp(...)with a regex literal for JSON MIME detection intypescript-angular,typescript-axios,typescript-nestjs, andtypescript-fetch./^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$/itest; remove the extraapplication/json-patch+jsonbranch.Written for commit f159d57. Summary will update on new commits.