Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Martelleur
Copy link

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