Skip to content

[BUG][rust-axum] Regex patterns in schema result in different regex in code #20686

@sulksamino

Description

@sulksamino

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

We use regex patterns in our API specification. These include usage of character classes like \d and escaping special characters like \.. For example to create a pattern that accepts a string consisting of one or more digits we used pattern: "^\\d+$" (or pattern: '^\d+$').
The generated code would construct a regex like this regex::Regex::new(r"^\\d+$"), which would be different from our intended pattern, as it would correspond to strings starting with a \ followed by at least one d.

We expected either regex::Regex::new(r"^\d+$") or regex::Regex::new("^\\d+$").

example on Rust Playground

openapi-generator version

We tried 7.8.0, 7.11 and the master as of this date. The problem is present in all of them.

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Regex
  description: Regex example api spec
  version: 1.0.0
paths:
  /apps/{id}:
    get:
      parameters:
        - name: id
          in: path
          schema:
            type: string
            pattern: "^\\d+$"
          required: true
      responses:
        "200":
          description: Success
Generation Details

We used:
docker run --rm -v ${PWD}:/local --user $(id -u):$(id -g) openapitools/openapi-generator-cli:v7.11.0 generate -i /local/api/openapi.yaml --additional-properties=packageName=redacted,generateAliasAsModel=true,packageVersion=2.0.0 -g rust-axum -o /local/redacted

Steps to reproduce
  1. Use the above command to generate the code from a given openapi.yaml
  2. The file redacted/src/models.rs contains the mentioned regex patterns
Related issues/PRs

None found.

Suggest a fix

As a workaround we used a custom template to use default literals instead of raw literals to construct regex::Regex (i.e. replace all occurences of regex::Regex::new(r"{{ pattern }}") with regex::Regex::new("{{ pattern }}")). This works for us, but we do not know if this would be a general solution.

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