OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-angular] pattern data gets improperly escaped

Open
#21,283 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

This issue has been raised and fixed years ago for the typescript-axios client:

  • BUG: #5973
  • PR: #5974

I feel like the problem it is sufficiently described, so not adding any more details here.

Example:

  • {{{pattern}}} is /^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$/
  • {{{pattern}}} should be /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
openapi-generator version
  • LATEST
Steps to reproduce
  1. Include this pattern in your OpenAPI spec

    {
        "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$",
    }
    
  2. Run the typescript-angular generator

    • NOTE: That the out of the box templates do NOT use the pattern field. For my needs I have adjusted the mustache templates to include them. So to reproduce you might have to do the same.
  3. Actual vs. Expected output:

  • {{{pattern}}} is /^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$/
  • {{{pattern}}} should be /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
Related issues/PRs
  • #5973
  • #5974
Suggest a fix

Two options here:

  1. A patch for typescript-angular only
    • Apply the same patch that was done for typescript-axios and override the toRegularExpression method in TypeScriptAngularClientCodegen.java
  2. A full fix for all typescript based client (as all typescript generators will eventually face the same problem)
    • Override the toRegularExpression method in the AbstractTypeScriptClientCodegen.java abstract base class.

The implementation will be the same regardless of the option choosen (just the location would change). Here is that code:

    /**
     * Overriding toRegularExpression() to avoid escapeText() being called,
     * as it would return a broken regular expression if any escaped character / metacharacter were present.
     */
    @Override
    public String toRegularExpression(String pattern) {
        return addRegularExpressionDelimiter(pattern);
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the existing typescript-axios fix in PR #5974 with TypeScriptAngularClientCodegen.java and AbstractTypeScriptClientCodegen.java. Reproduce the issue with the OpenAPI pattern shown and the typescript-angular generator; done means generated pattern data retains the intended backslashes without improper escaping.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.