Skip to content

Feature/c sharp code generation pattern validation issue 3145#5084

Open
Martelleur wants to merge 3 commits intoRicoSuter:masterfrom
Martelleur:feature/CSharp-code-generation-pattern-validation-issue-3145
Open

Feature/c sharp code generation pattern validation issue 3145#5084
Martelleur wants to merge 3 commits intoRicoSuter:masterfrom
Martelleur:feature/CSharp-code-generation-pattern-validation-issue-3145

Conversation

@Martelleur
Copy link
Copy Markdown

This is a fix for #3145: C# Code Generation doesn't validate patterns

Proposed solution:
Add RegEx if statement, in NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid

Before:

{%     for parameter in operation.PathParameters -%}
{%         if parameter.IsNullable == false and parameter.IsRequired -%}
        if ({{ parameter.VariableName }} == null)
            throw new System.ArgumentNullException("{{ parameter.VariableName }}");

{%         endif -%}
{%     endfor -%}

After:

{%     for parameter in operation.PathParameters -%}
{%         if parameter.IsNullable == false and parameter.IsRequired -%}
        if ({{ parameter.VariableName }} == null)
            throw new System.ArgumentNullException("{{ parameter.VariableName }}");

{%         endif -%}
{%         if parameter.Schema.Pattern -%}
        if (!System.Text.RegularExpressions.Regex.IsMatch({{ parameter.VariableName }}, "{{ parameter.Schema.Pattern }}"))
            throw new System.ArgumentException("Parameter '{{ parameter.VariableName }}' does not match the required pattern '{{ parameter.Schema.Pattern }}'.");

{%         endif -%}
{%     endfor -%}

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant