Skip to content

[BUG] [Go] Patterns with escapes or character classes produce 4 consecutive backslashes in struct tags #22101

@srgoni

Description

@srgoni

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

When using a regex pattern in an OpenAPI template, openapi-generator will now emit validate:"regexp=..." tags into generated data structures. Unfortunately, the tags are escaped twice somewhere, resulting in 4 consecutive backslashes if the original pattern contained character classes or escape sequences.
Escaping is necessary, but escaping twice is incorrect and leads to problems with regex validation later.

I was previously using the x-go-custom-tag property to insert validate tags into the generated code, and validate those using a 3rd party validation package. This still works fine, but requires removing any extra escaping backslashes, because openapi-generator is now automatically escaping them.

openapi-generator version

This is a regression since 7.7.0, patterns were ignored in 7.6.0 and before.

OpenAPI declaration file content or url
---
openapi: 3.0.0
info:
  title: unset
  version: "1"
paths: {}
components:
  schemas:
    Bar:
      type: object
      properties:
        Email:
          type: string
          pattern: '^\S+@\S+$'
Generation Details

No special options or environment needed.

The CLI was executed using the official Docker container as follows:

openapi-generator-cli generate -i regex.yaml -g go -o openapi
Steps to reproduce

Run the generator on the template to produce Go code, as described above.

The model for Bar renders as:

type Bar struct {
        Email *string `json:"Email,omitempty" validate:"regexp=^\\\\S+@\\\\S+$"`
}

My expectation is that the backslashes are properly escaped, using a single extra backslash:

type Bar struct {
        Email *string `json:"Email,omitempty" validate:"regexp=^\\S+@\\S+$"`
}
Related issues/PRs

N/A

Suggest a fix

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