OpenAPITools / OpenAPITools/openapi-generator

[BUG][C#] Regex Strings both Escaped and Literal

Open
#9,262 0 comments 1 reaction 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 (example)?
  • 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

When using a patterned string with escaped characters, the generated regex in the C# model both escapes the backslashes and makes the string literal. It should be one or the other. For example, pattern: '/^[a-z0-9]([a-z0-9\-\_ ]*[a-z0-9])?$/i' results in new Regex(@"^[a-z0-9]([a-z0-9\\-\\_ ]*[a-z0-9])?$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase).

openapi-generator version

5.1.0

OpenAPI declaration file content or url
swagger: "2.0"
info:
  title: Example
  version: 1.0.0
paths:
  /fake:
    get:
      responses:
        "400":
          description: fake
definitions:
  SomeType:
    type: object
    required:
      - name
    properties:
      name:
        type: string
        pattern: '/^[a-z0-9]([a-z0-9\-\_ ]*[a-z0-9])?$/i'

Generation Details
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate `
    --global-property modelTests=false,modelDocs=false,apiTests=false,apiDocs=false `
    -i /local/$swaggerSpecFile `
    -g csharp-netcore `
    -o /local `
    -c /local/openapi-generator-config.json
{
  "packageName": "SomeProject",
  "targetFramework": "netstandard2.0",
  "optionalAssemblyInfo": false,
  "optionalProjectFile": false
}
Steps to reproduce
  1. Create swagger.yml with the above contents in new folder.
  2. Create powershell file with the above contents in same folder.
  3. Create openapi-generator-config.json with above contents in same folder.
  4. Install docker if it isn't, making sure to add it to path.
  5. Run docker pull openapitools/openapi-generator-cli in a terminal.
  6. Run the created powershell file in the same folder.
  7. Examine generated src/SomeProject/Model/SomeType and note regex associated with Name.
Related issues/PRs

None that I was able to find.

Suggest a fix

Either make the string not literal (remove the @ from @"{REGEX}") or remove the escaping backslashes.

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

Reproduce the issue using the swagger.yml declaration, the PowerShell generation script, and openapi-generator-config.json. Inspect the generated src/SomeProject/Model/SomeType model and trace the C# generator template or entry point responsible for the Name regex. Done means the generated pattern uses compatible escaping and literal-string handling without changing the intended regex.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, docker, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.