Skip to content

Commit

Permalink
Add braces to if statements and ensure correct use of variables (#5049)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 22, 2024
1 parent 5d30b04 commit 5a0073a
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 19 deletions.
4 changes: 1 addition & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<!--
[IDE0005] Using directive is unnecessary
[IDE0011] Add braces to 'if' statement
[IDE0008] Use explicit type instead of 'var'
[IDE0017] Object initialization can be simplified
[IDE0021] Use block body for constructor
Expand All @@ -63,7 +62,6 @@
[IDE0055] Fix formatting
[IDE0056] Indexing can be simplified
[IDE0057] Substring can be simplified
[IDE0059] Unnecessary assignment of a value
[IDE0060] Remove unused parameter
[IDE0061] Use block body for local function
[IDE0090] 'new' expression can be simplified
Expand Down Expand Up @@ -98,7 +96,7 @@
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
-->
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0021;IDE0022;IDE0025;IDE0027;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0090;IDE0130;IDE0160;IDE0200;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0017;IDE0021;IDE0022;IDE0025;IDE0027;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0060;IDE0061;IDE0090;IDE0130;IDE0160;IDE0200;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1834;CA1845;CA1847;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012</NoWarn>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void When_parameter_has_default_then_set_in_partial_controller()
Assert.Contains("BarAsync(int abc, bool booldef, int intdef, double doubledef, decimal decdef, string strdef, int? bar = null);", code);

var trimmedCode = RemoveExternalReferences(code);
Assert.NotNull(trimmedCode);

//CompilerParameters parameters = new CompilerParameters { GenerateInMemory = true };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public async Task When_setting_is_enabled_then_parameters_are_reordered()
operation.Parameters.Remove(lastParameter);
operation.Parameters.Insert(0, lastParameter);
var json = document.ToJson();
Assert.NotNull(json);

var codeGenerator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
{
Expand Down
3 changes: 2 additions & 1 deletion src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ public async Task When_using_json_schema_with_references_in_service_then_referen
document.Definitions["Foo"] = schema;

// Assert
var jsonService = document.ToJson(); // no exception expected
var json = document.ToJson(); // no exception expected
Assert.NotNull(json);
}

[Fact]
Expand Down
19 changes: 11 additions & 8 deletions src/NSwag.CodeGeneration.TypeScript.Tests/AngularJSTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public class Foo
public class DiscussionController : Controller
{
[HttpPost]
public void AddMessage([FromBody]Foo message)
public void AddMessage([FromBody] Foo message)
{
}
}
public class UrlEncodedRequestConsumingController: Controller

public class UrlEncodedRequestConsumingController : Controller
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public void AddMessage([FromForm]Foo message, [FromForm]string messageId)
public void AddMessage([FromForm] Foo message, [FromForm] string messageId)
{
}
}
Expand All @@ -40,9 +40,10 @@ public async Task When_export_types_is_true_then_add_export_before_classes()
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }
});

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -70,9 +71,10 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }
});

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand All @@ -91,7 +93,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes
Assert.DoesNotContain("export class DiscussionClient", code);
Assert.DoesNotContain("export interface IDiscussionClient", code);
}

[Fact]
public async Task When_consumes_is_url_encoded_then_construct_url_encoded_request()
{
Expand All @@ -102,6 +104,7 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques
});
var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand All @@ -120,4 +123,4 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques
Assert.Contains("\"Content-Type\": \"application/x-www-form-urlencoded\"", code);
}
}
}
}
5 changes: 5 additions & 0 deletions src/NSwag.CodeGeneration.TypeScript.Tests/AngularTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public async Task When_return_value_is_void_then_client_returns_observable_of_vo
});
var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -110,6 +111,7 @@ public async Task When_export_types_is_true_then_add_export_before_classes()
});
var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -139,6 +141,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes
});
var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -168,6 +171,7 @@ public async Task When_generic_request()
});
var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -197,6 +201,7 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques
});
var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down
5 changes: 5 additions & 0 deletions src/NSwag.CodeGeneration.TypeScript.Tests/AxiosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public async Task When_export_types_is_true_then_add_export_before_classes()

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -73,6 +74,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -103,6 +105,7 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -132,6 +135,7 @@ public async Task Add_cancel_token_to_every_call()

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -163,6 +167,7 @@ public async Task When_abort_signal()

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down
17 changes: 12 additions & 5 deletions src/NSwag.CodeGeneration.TypeScript.Tests/FetchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ public class Foo
public class DiscussionController : Controller
{
[HttpPost]
public void AddMessage([FromBody]Foo message)
public void AddMessage([FromBody] Foo message)
{
}
}

public class UrlEncodedRequestConsumingController: Controller
public class UrlEncodedRequestConsumingController : Controller
{
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public void AddMessage([FromForm]Foo message, [FromForm]string messageId, [FromForm]System.DateTime date, [FromForm]System.Collections.Generic.List<string> list)
public void AddMessage([FromForm] Foo message, [FromForm] string messageId, [FromForm] System.DateTime date,
[FromForm] System.Collections.Generic.List<string> list)
{
}
}
Expand All @@ -43,6 +44,7 @@ public async Task When_export_types_is_true_then_add_export_before_classes()

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -73,6 +75,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -103,6 +106,7 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand All @@ -129,9 +133,10 @@ public async Task When_abort_signal()
{
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.Swagger2 }
});

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -160,6 +165,7 @@ public async Task When_no_abort_signal()

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -216,6 +222,7 @@ public async Task When_no_includeHttpContext()

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand All @@ -233,4 +240,4 @@ public async Task When_no_includeHttpContext()
Assert.DoesNotContain("context: httpContext", code);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public async Task When_export_types_is_true_then_add_export_before_classes()

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -73,6 +74,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -103,6 +105,7 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public async Task When_export_types_is_true_then_add_export_before_classes()

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -73,6 +74,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes

var document = await generator.GenerateForControllerAsync<DiscussionController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down Expand Up @@ -103,6 +105,7 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques

var document = await generator.GenerateForControllerAsync<UrlEncodedRequestConsumingController>();
var json = document.ToJson();
Assert.NotNull(json);

// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public async Task When_query_parameter_is_enum_array_then_the_enum_is_referenced
// Act
var document = await generator.GenerateForControllerAsync<FooController>();
var json = document.ToJson();
Assert.NotNull(json);

var clientSettings = new TypeScriptClientGeneratorSettings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public async Task When_parameter_is_abstract_then_generate_union()
});

var json = document.ToJson();
Assert.NotNull(json);

// Act
var code = clientGenerator.GenerateFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public async Task When_parameter_is_nullable_and_ts20_then_it_is_a_union_type_wi
});

var json = document.ToJson();
Assert.NotNull(json);

// Act
var code = clientGenerator.GenerateFile();
Expand Down Expand Up @@ -77,6 +78,7 @@ public async Task When_parameter_is_nullable_and_ts20_then_it_is_not_included_in
});

var json = document.ToJson();
Assert.NotNull(json);

// Act
var code = clientGenerator.GenerateFile();
Expand Down Expand Up @@ -105,6 +107,7 @@ public async Task When_parameter_is_nullable_optional_and_ts20_then_it_is_a_unio
});

var json = document.ToJson();
Assert.NotNull(json);

// Act
var code = clientGenerator.GenerateFile();
Expand Down Expand Up @@ -133,6 +136,7 @@ public async Task When_parameter_is_nullable_optional_and_ts20_then_it_is_not_in
});

var json = document.ToJson();
Assert.NotNull(json);

// Act
var code = clientGenerator.GenerateFile();
Expand Down
1 change: 1 addition & 0 deletions src/NSwag.Core.Tests/DocumentLoadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public async Task When_locale_is_not_english_then_types_are_correctly_serialized
// Act
var document = await OpenApiDocument.FromJsonAsync(json);
var j = document.ToJson();
Assert.NotNull(j);

// Assert
Assert.Equal(JsonObjectType.Integer, document.Definitions["Pet"].Properties["id"].Type);
Expand Down
1 change: 1 addition & 0 deletions src/NSwag.Core.Tests/DocumentReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public async Task When_parameter_references_schema_then_it_is_resolved()
// Act
var document = await OpenApiDocument.FromJsonAsync(json);
json = document.ToJson();
Assert.NotNull(json);

// Assert
Assert.Equal("secret", document.Operations.First().Operation.ActualParameters[0].ActualSchema.Format);
Expand Down
Loading

0 comments on commit 5a0073a

Please sign in to comment.