swagger-api/swagger-codegen

Pattern (regex) always enclosed with '/' (slash)

meerdekens opened this issue · 0 comments

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!