Skip to content

[typescript-*]: Improve literal regexp for JSON MIME-type#23319

Open
sjoubert wants to merge 1 commit intoOpenAPITools:masterfrom
sjoubert:ts_literal_regexp
Open

[typescript-*]: Improve literal regexp for JSON MIME-type#23319
sjoubert wants to merge 1 commit intoOpenAPITools:masterfrom
sjoubert:ts_literal_regexp

Conversation

@sjoubert
Copy link
Contributor

@sjoubert sjoubert commented Mar 23, 2026

  • 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

This fixes a report from SonarQube analysis (typescript:S6325) suggesting to use a literal regex instead of a RegExp constructor. 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-axios but found and fixed the same pattern in 3 other typescript clients.

Here are a couple questions/points left to clarify:

  1. With a literal, the 2 escaped / 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.
  2. On 3 of the clients, I've removed the explicit and separate check for application/json-patch+json as I believe the regexp already matches it (including in a case-insensitive way)
  3. The typescript-fetch client 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

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@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+json check across TypeScript generators. This addresses SonarQube findings (S6325, S6535) and simplifies the code.

  • Refactors
    • Replace new RegExp(...) with a regex literal for JSON MIME detection in typescript-angular, typescript-axios, typescript-nestjs, and typescript-fetch.
    • Rely on a single /^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$/i test; remove the extra application/json-patch+json branch.
    • Regenerate affected samples to reflect the template changes.

Written for commit f159d57. Summary will update on new commits.

- 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
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

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-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

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

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;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const jsonMime: RegExp = /^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$/i;
const jsonMime: RegExp = /^(application\/json|[^;\/ \t]+\/[^;\/ \t]+[+]json)[ \t]*(;.*)?$/i;

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 just tested escaping them but I get the "unecessary escape character" (typescript:S6535) warning. So I think we are good like this.

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.

2 participants