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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
openapi

src/**/bin/**
src/**/obj/**
src/packages/**
Expand Down
180 changes: 180 additions & 0 deletions src/NSwag.CodeGeneration.CSharp.Tests/PathPatternValidationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
using System.IO;


namespace NSwag.CodeGeneration.CSharp.Tests
{
public class PathPatternValidationTests
{
private static string GenerateSpec(bool withPattern)
{
return $@"{{
""openapi"": ""3.0.1"",
""info"": {{
""title"": ""NSwager Test Server API"",
""description"": ""An api used to test NSwager."",
""version"": ""2.0""
}},
""paths"": {{
""/api/v2/test/{{PathVariable}}/ping"": {{
""get"": {{
""tags"": [
""TestControllerVersionTwo""
],
""summary"": ""Used to ping a valid user name."",
""operationId"": ""PingpathVariableV2"",
""parameters"": [
{{
""name"": ""pathVariable"",
""in"": ""path"",
""description"": ""A System.String"",
""required"": true,
""schema"": {{
{(withPattern ? @"""pattern"": ""^[a-zA-Z0-9_]+$"", " : "")}
""type"": ""string""
}}
}}
],
""responses"": {{
""200"": {{
""description"": ""With the user name"",
""content"": {{
""application/json"": {{
""schema"": {{
""$ref"": ""#/components/schemas/PathVariableDTO""
}}
}}
}}
}}
}}
}}
}}
}},
""components"": {{
""schemas"": {{
""PathVariableDTO"": {{
""required"": [
""pathVariable""
],
""type"": ""object"",
""properties"": {{
""pathVariable"": {{
""type"": ""string"",
""description"": ""The value of the path variable"",
""nullable"": true
}}
}},
""additionalProperties"": false,
""description"": ""A DTO containing a path variable""
}}
}}
}}
}}";
}

/// <summary>
/// This string if statement is exactly the same as the if statement in <see cref="ValidatePatternValueMock"/> method.
/// </summary>
private string generatedCode =
@"if (!System.Text.RegularExpressions.Regex.IsMatch(pathVariable, ""^[a-zA-Z0-9_]+$""))
throw new System.ArgumentException(""Parameter 'pathVariable' does not match the required pattern '^[a-zA-Z0-9_]+$'."");";

/// <summary>
/// Used to mock execution of the generated code <see cref="generatedCode"/>
/// </summary>
/// <param name="pathVariable">The value of the path variable.</param>
/// <param name="regexPattern">A regular expression use to validate <see cref="pathVariable"/>.</param>
/// <exception cref="System.ArgumentException">Thrown if <paramref name="pathVariable"/>
/// does not match the <paramref name="regexPattern"/>.</exception>
private static void ValidatePathPatternMock(string pathVariable, string regexPattern)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(pathVariable, regexPattern))
throw new System.ArgumentException(
$"Parameter 'pathVariable' does not match the required pattern '{regexPattern}'."
);
}

[Fact]
public async Task When_path_parameter_have_pattern_field()
{
// Arrange
var seetings = new CSharpClientGeneratorSettings();
var document = await OpenApiDocument.FromJsonAsync(GenerateSpec(withPattern: true));
var generator = new CSharpClientGenerator(document, seetings);

// Act
var code = generator.GenerateFile();

// Assert
Assert.Contains(NormalizeWhitespace(generatedCode), NormalizeWhitespace(code));
}

[Fact]
public async Task When_path_parameter_not_have_pattern_field()
{
// Arrange
var seetings = new CSharpClientGeneratorSettings();
var document = await OpenApiDocument.FromJsonAsync(GenerateSpec(withPattern: false));
var generator = new CSharpClientGenerator(document, seetings);

// Act
var code = generator.GenerateFile();

// Assert
Assert.DoesNotContain(generatedCode, code);
}

[Theory]
[InlineData("MockValue123", "^[a-zA-Z0-9_]+$")] // Alphanumeric and underscores
[InlineData("MockValue-123", "^[a-zA-Z0-9_-]+$")] // Alphanumeric, underscores, and dashes
[InlineData("Mock.Value.123", "^[a-zA-Z0-9._]+$")] // Alphanumeric, dots, and underscores
[InlineData("123456", "^\\d+$")] // Digits only
public void ValidatePathVariable_ValidPathVariables_DoesNotThrow(
string pathVariable,
string regexPattern
)
{
// Arrange & Act & Assert
var exception = Record.Exception(
() => ValidatePathPatternMock(pathVariable, regexPattern)
);
Assert.Null(exception);
}

[Theory]
[InlineData("Mock@123", "^[a-zA-Z0-9_]+$")] // Alphanumeric and underscores
[InlineData("Mock Value", "^[a-zA-Z0-9_-]+$")] // Alphanumeric, underscores, and dashes
[InlineData("Mock!Value", "^[a-zA-Z0-9._]+$")] // Alphanumeric, dots, and underscores
[InlineData("MockValue123", "^\\d+$")] // Digits only
public void ValidatePathVariable_InvalidPathVariables_ThrowsArgumentException(
string pathVariable,
string regexPattern
)
{
// Arrange & Act & Assert
var exception = Assert.Throws<ArgumentException>(
() => ValidatePathPatternMock(pathVariable, regexPattern)
);
Assert.Contains(
$"Parameter 'pathVariable' does not match the required pattern",
exception.Message
);
}

private static string NormalizeWhitespace(string input)
{
char[] separators = ['\r', '\n', '\t', ' '];
return string.Join(
" ",
input.Split(separators, StringSplitOptions.RemoveEmptyEntries)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
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 -%}
{% for parameter in operation.QueryParameters -%}
Expand Down Expand Up @@ -479,4 +484,4 @@

{% template Client.Class.ConvertToString %}
{% template Client.Class.Body %}
}
}
71 changes: 0 additions & 71 deletions src/NSwag.Npm/bin/nswag.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/NSwag.Npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading