Skip to content
Merged
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
63 changes: 62 additions & 1 deletion src/NSwag.CodeGeneration.CSharp.Tests/OptionalParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public enum MyEnum
Two,
Three,
Four

}

public class MyClass
{
#pragma warning disable IDE0051
Expand Down Expand Up @@ -145,5 +145,66 @@ public async Task When_setting_is_enabled_then_parameters_are_reordered()
// Assert
Assert.Contains("TestAsync(string a, string b, string c = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))", code);
}

[Fact]
public async Task When_optional_parameter_comes_before_required()
{
// Arrange
const string specification = """
{
"openapi": "3.0.0",
"paths": {
"/": {
"get": {
"operationId": "Get",
"parameters": [
{
"name": "firstname",
"in": "query",
"schema": {
"type": "string",
"nullable": true
},
"x-position": 1
},
{
"name": "lastname",
"in": "query",
"required": true,
"schema": {
"type": "string"
},
"x-position": 2
}
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
}
}
""";

// Act
var document = await OpenApiDocument.FromJsonAsync(specification, "", SchemaType.OpenApi3);
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
{
GenerateOptionalParameters = true
});
var code = generator.GenerateFile();

// Assert
Assert.Contains("GetAsync(string lastname, string firstname = null,", code);
}
}
}
20 changes: 13 additions & 7 deletions src/NSwag.CodeGeneration.CSharp/Models/CSharpOperationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,22 @@ public CSharpOperationModel(
// TODO: Move to CSharpControllerOperationModel
if (generator is CSharpControllerGenerator)
{
parameters = [.. parameters
.OrderBy(p => p.Position ?? 0)
.ThenBy(p => !p.IsRequired)
.ThenBy(p => p.Default == null)];
parameters =
[
.. parameters
.OrderBy(p => !p.IsRequired)
.ThenBy(p => p.Default == null)
.ThenBy(p => p.Position ?? 0)
];
}
else
{
parameters = [.. parameters
.OrderBy(p => p.Position ?? 0)
.ThenBy(p => !p.IsRequired)];
parameters =
[
.. parameters
.OrderBy(p => !p.IsRequired)
.ThenBy(p => p.Position ?? 0)
];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using NJsonSchema;
using Xunit;

namespace NSwag.CodeGeneration.TypeScript.Tests
{
public class OptionalParameterTests
{
[Fact]
public async Task When_optional_parameter_comes_before_required()
{
// Arrange
const string specification = """
{
"openapi": "3.0.0",
"paths": {
"/": {
"get": {
"operationId": "Get",
"parameters": [
{
"name": "firstname",
"in": "query",
"schema": {
"type": "string",
"nullable": true
},
"x-position": 1
},
{
"name": "lastname",
"in": "query",
"required": true,
"schema": {
"type": "string"
},
"x-position": 2
}
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
}
}
""";

// Act
var document = await OpenApiDocument.FromJsonAsync(specification, "", SchemaType.OpenApi3);
var generator = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
{
GenerateOptionalParameters = true
});
var code = generator.GenerateFile();

// Assert
Assert.Contains("get(lastname: string, firstname?: string | null | undefined)", code);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public TypeScriptOperationModel(

if (settings.GenerateOptionalParameters)
{
parameters = [.. parameters.OrderBy(p => p.Position ?? 0).ThenBy(p => !p.IsRequired)];
parameters =
[
.. parameters
.OrderBy(p => !p.IsRequired)
.ThenBy(p => p.Position ?? 0)
];
}

Parameters = parameters
Expand Down
Loading