swagger-api / swagger-api/swagger-codegen
Pattern (regex) always enclosed with '/' (slash)
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Hi,
I wanted to implement the pattern (RegularExpression), minLength and maxLength validation attributes on the model generated for aspnetcore, but encountered an issue:
While in most other languages a regex is defined as delimited by a '/', this is not the case in c#.
Instead it is only defined as a string, enclosed by quotes.
Which leaves me with a problem because the slashes are always put around the expression by the addRegularExpressionDelimiter method on DefaultCodegen.java:3915 and there is no easy way to remove them while mustache, I can't generate a proper RegularExpression attribute.
Imho, delimiting the expression with / is something language dependent and should happen nowhere except for in the mustache templates. But I suppose removing them would mean a breaking change for all existing java-like mustache templates using the pattern property. Right?
Do you see an easy way to remove the delimiting characters in case of generating c# code?
Swagger-codegen version
v3.0.23
Swagger declaration file content or url
` public String addRegularExpressionDelimiter(String pattern) {
if (StringUtils.isEmpty(pattern)) {
return pattern;
}
if (!pattern.matches("^/.*")) {
return "/" + pattern.replaceAll("/", "\\\\/") + "/";
}
return pattern;
}`
Command line used for generation
Steps to reproduce
Generate a project using a custom mustache template for rendering a RegularExpression attribute on a model property:
{{#pattern}}
[RegularExpression(@"{{pattern}}")]
{{/pattern}}
Related issues/PRs
Suggest a fix/enhancement
Either remove the delimiters or at least remove them in case of c# codegen
Thanks!
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
Start in DefaultCodegen.java around addRegularExpressionDelimiter at line 3915, then inspect how the custom Mustache template consumes pattern for C# model properties. Reproduce generation with the shown RegularExpression template and define done as producing a valid C# attribute without slash delimiters, while accounting for existing Java-like templates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100