Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
sulksamino opened this issue Feb 19, 2025 · 0 comments
Open
5 of 6 tasks

Comments

@sulksamino
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant