OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Go] Patterns with escapes or character classes produce 4 consecutive backslashes in struct tags
Nobody has claimed this yet.
- 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?
- 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue by running the OpenAPI Generator CLI with regex.yaml, the go generator, and the shown output directory, then inspect the generated Bar struct. Trace how the pattern becomes the validate struct tag; done means character classes and escape sequences produce the expected single extra backslash rather than four.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100