-
-
Notifications
You must be signed in to change notification settings - Fork 63
Improve code coverage #1132
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
Merged
Merged
Improve code coverage #1132
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c1d220
test: add RichGenerationReporter and FormParameterExtractor unit tests
christianhelle 4b33011
test: cover remaining ParameterExtractor private methods
christianhelle 3d31313
test: cover OutputPlanner edge cases and branch paths
christianhelle 6a58923
test: cover GenerationOrchestrator warning paths and FormatFileSize
christianhelle 14c7f46
fix: apply CodeRabbit auto-fixes
coderabbitai[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using FluentAssertions; | ||
| using NJsonSchema; | ||
| using NSwag; | ||
| using NSwag.CodeGeneration.CSharp.Models; | ||
| using Refitter.Core; | ||
| using TUnit.Core; | ||
|
|
||
| namespace Refitter.Tests; | ||
|
|
||
| public class FormParameterExtractorTests | ||
| { | ||
| [Test] | ||
|
christianhelle marked this conversation as resolved.
|
||
| public void CanExtract_Returns_True_For_FormData() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| extractor.CanExtract(OpenApiParameterKind.FormData).Should().BeTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void CanExtract_Returns_False_For_Non_FormData() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| extractor.CanExtract(OpenApiParameterKind.Query).Should().BeFalse(); | ||
| extractor.CanExtract(OpenApiParameterKind.Path).Should().BeFalse(); | ||
| extractor.CanExtract(OpenApiParameterKind.Header).Should().BeFalse(); | ||
| extractor.CanExtract(OpenApiParameterKind.Body).Should().BeFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Returns_Empty_When_No_FormData_Parameters() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var operationModel = CreateEmptyOperationModel(); | ||
| var operation = new OpenApiOperation(); | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()); | ||
| result.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Returns_FormData_Parameters_With_Correct_Format() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var parameter = CreateFormDataParameterModel("field1", "field1", "string"); | ||
| var operationModel = CreateOperationModel(parameter); | ||
| var operation = new OpenApiOperation(); | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
| result.Should().ContainSingle().Which.Should().Be("string field1"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Handles_Alias_For_Different_Variable_Name() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var parameter = CreateFormDataParameterModel("field-name", "field_name", "string"); | ||
| var operationModel = CreateOperationModel(parameter); | ||
| var operation = new OpenApiOperation(); | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
| result.Should().ContainSingle().Which.Should().Be("[AliasAs(\"field-name\")] string field_name"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Deduplicates_FormData_Parameters_By_Variable_Name() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var parameter1 = CreateFormDataParameterModel("field1", "field1", "string"); | ||
| var parameter2 = CreateFormDataParameterModel("field1", "field1", "int"); | ||
| var operationModel = CreateOperationModel(parameter1, parameter2); | ||
| var operation = new OpenApiOperation(); | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
| result.Should().ContainSingle(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Extracts_Multipart_Text_Fields_From_RequestBody() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var operationModel = CreateEmptyOperationModel(); | ||
| var operation = new OpenApiOperation | ||
| { | ||
| RequestBody = new OpenApiRequestBody() | ||
| }; | ||
| var schema = new JsonSchema(); | ||
| schema.Properties["title"] = new JsonSchemaProperty | ||
| { | ||
| Type = JsonObjectType.String | ||
| }; | ||
| schema.Properties["description"] = new JsonSchemaProperty | ||
| { | ||
| Type = JsonObjectType.String | ||
| }; | ||
| operation.RequestBody.Content["multipart/form-data"] = new OpenApiMediaType | ||
| { | ||
| Schema = schema | ||
| }; | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
|
|
||
| result.Should().HaveCount(2); | ||
| result.Should().Contain("string title"); | ||
| result.Should().Contain("string description"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Skips_Binary_Fields_From_Multipart_RequestBody() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var operationModel = CreateEmptyOperationModel(); | ||
| var operation = new OpenApiOperation | ||
| { | ||
| RequestBody = new OpenApiRequestBody() | ||
| }; | ||
| var schema = new JsonSchema(); | ||
| schema.Properties["avatar"] = new JsonSchemaProperty | ||
| { | ||
| Type = JsonObjectType.String, | ||
| Format = "binary" | ||
| }; | ||
| schema.Properties["title"] = new JsonSchemaProperty | ||
| { | ||
| Type = JsonObjectType.String | ||
| }; | ||
| operation.RequestBody.Content["multipart/form-data"] = new OpenApiMediaType | ||
| { | ||
| Schema = schema | ||
| }; | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
|
|
||
| result.Should().ContainSingle().Which.Should().Be("string title"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Skips_Array_Of_Binary_From_Multipart_RequestBody() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var operationModel = CreateEmptyOperationModel(); | ||
| var operation = new OpenApiOperation | ||
| { | ||
| RequestBody = new OpenApiRequestBody() | ||
| }; | ||
| var schema = new JsonSchema(); | ||
| schema.Properties["files"] = new JsonSchemaProperty | ||
| { | ||
| Type = JsonObjectType.Array, | ||
| Item = new JsonSchema | ||
| { | ||
| Type = JsonObjectType.String, | ||
| Format = "binary" | ||
| } | ||
| }; | ||
| operation.RequestBody.Content["multipart/form-data"] = new OpenApiMediaType | ||
| { | ||
| Schema = schema | ||
| }; | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
| result.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Handles_Missing_RequestBody_Schema() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var operationModel = CreateEmptyOperationModel(); | ||
| var operation = new OpenApiOperation | ||
| { | ||
| RequestBody = new OpenApiRequestBody() | ||
| }; | ||
| operation.RequestBody.Content["multipart/form-data"] = new OpenApiMediaType(); | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
| result.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Extract_Handles_Null_RequestBody_Properties() | ||
| { | ||
| var extractor = new FormParameterExtractor(); | ||
| var operationModel = CreateEmptyOperationModel(); | ||
| var operation = new OpenApiOperation | ||
| { | ||
| RequestBody = new OpenApiRequestBody() | ||
| }; | ||
| var schema = new JsonSchema(); | ||
| operation.RequestBody.Content["multipart/form-data"] = new OpenApiMediaType | ||
| { | ||
| Schema = schema | ||
| }; | ||
|
|
||
| var result = extractor.Extract(operationModel, operation, new RefitGeneratorSettings()).ToList(); | ||
| result.Should().BeEmpty(); | ||
| } | ||
|
|
||
| private static CSharpOperationModel CreateEmptyOperationModel() | ||
| { | ||
| var operationModel = (CSharpOperationModel)RuntimeHelpers.GetUninitializedObject(typeof(CSharpOperationModel)); | ||
| var baseType = typeof(CSharpOperationModel).BaseType!; | ||
|
|
||
| baseType | ||
| .GetField("<Parameters>k__BackingField", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)! | ||
| .SetValue(operationModel, new List<CSharpParameterModel>()); | ||
|
|
||
| return operationModel; | ||
| } | ||
|
|
||
| private static CSharpOperationModel CreateOperationModel(params CSharpParameterModel[] parameters) | ||
| { | ||
| var operationModel = (CSharpOperationModel)RuntimeHelpers.GetUninitializedObject(typeof(CSharpOperationModel)); | ||
| var baseType = typeof(CSharpOperationModel).BaseType!; | ||
|
|
||
| baseType | ||
| .GetField("<Parameters>k__BackingField", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)! | ||
| .SetValue(operationModel, parameters.ToList()); | ||
|
|
||
| return operationModel; | ||
| } | ||
|
|
||
| private static CSharpParameterModel CreateFormDataParameterModel( | ||
| string name, | ||
| string variableName, | ||
| string type = "string") | ||
| { | ||
| var parameterModel = (CSharpParameterModel)RuntimeHelpers.GetUninitializedObject(typeof(CSharpParameterModel)); | ||
| var baseType = typeof(CSharpParameterModel).BaseType!; | ||
|
|
||
| baseType | ||
| .GetField("<Type>k__BackingField", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)! | ||
| .SetValue(parameterModel, type); | ||
| baseType | ||
| .GetField("<Name>k__BackingField", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)! | ||
| .SetValue(parameterModel, name); | ||
| baseType | ||
| .GetField("<VariableName>k__BackingField", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)! | ||
| .SetValue(parameterModel, variableName); | ||
| baseType | ||
| .GetField("_parameter", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)! | ||
| .SetValue( | ||
| parameterModel, | ||
| new OpenApiParameter | ||
| { | ||
| Name = name, | ||
| Kind = OpenApiParameterKind.FormData, | ||
| Schema = new JsonSchema() | ||
| }); | ||
|
|
||
| return parameterModel; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.