Skip to content

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

@spunzmann

Description

@spunzmann

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:

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
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);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions