-
-
Notifications
You must be signed in to change notification settings - Fork 63
Harden .refitter settings deserialization and output path handling #1000
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
46e2d5b
Handle settings deserialization and output path adjustments
christianhelle 9122cd1
Regression tests for issue #998 - output path resolution with setting…
christianhelle 20b6014
Streamline output path resolution in single file mode
christianhelle 209509e
Update MSBuild test build script for issue #998 regression checks and…
christianhelle c34d49c
Regression tests for issue #998: MSBuild + .NET 10 ignores outputFold…
christianhelle 7840b90
Ignore generated code files in .gitignore
christianhelle 1e278d3
fix: apply CodeRabbit auto-fixes
coderabbitai[bot] 76736de
Merge pull request #1001 from christianhelle/coderabbitai/autofix/784…
christianhelle a8252d8
Fix failing smoke tests
christianhelle 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
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 |
|---|---|---|
|
|
@@ -319,4 +319,4 @@ private static async Task<string> GenerateCodeFromSpec( | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
186 changes: 186 additions & 0 deletions
186
src/Refitter.Tests/Examples/SettingsFileOutputPathTests.cs
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,186 @@ | ||
| using System.Reflection; | ||
| using FluentAssertions; | ||
| using Refitter.Core; | ||
| using Refitter.Tests.Build; | ||
|
|
||
| namespace Refitter.Tests.Examples; | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for issue #998: MSBuild + .NET 10 ignores outputFolder and filename-from-.refitter behavior | ||
| /// Tests exercise the actual GenerateCommand helper logic via reflection | ||
| /// </summary> | ||
| public class SettingsFileOutputPathTests | ||
| { | ||
| private const string MinimalOpenApiSpec = """ | ||
| { | ||
| "openapi": "3.0.1", | ||
| "info": { "title": "Test API", "version": "1.0.0" }, | ||
| "paths": { | ||
| "/test": { | ||
| "get": { | ||
| "operationId": "getTest", | ||
| "responses": { | ||
| "200": { | ||
| "description": "Success", | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": { "id": { "type": "integer" } } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| [Test] | ||
| public void SettingsFile_WithoutOutputFilename_UsesRefitterFilenameFallback() | ||
| { | ||
| var tempDir = CreateTempDirectory(); | ||
|
|
||
| try | ||
| { | ||
| var settingsFile = Path.Combine(tempDir, "myapi.refitter"); | ||
| File.WriteAllText(settingsFile, "{}"); | ||
|
|
||
| var settingsJson = """ | ||
| { | ||
| "openApiPath": "spec.json", | ||
| "namespace": "TestNamespace" | ||
| } | ||
| """; | ||
|
|
||
| var refitGeneratorSettings = Serializer.Deserialize<RefitGeneratorSettings>(settingsJson); | ||
| var settings = new Settings { SettingsFilePath = settingsFile }; | ||
|
|
||
| ApplySettingsFileDefaults(settingsFile, refitGeneratorSettings); | ||
|
|
||
| refitGeneratorSettings.OutputFilename.Should().Be("myapi.cs", | ||
| "because OutputFilename should default to the .refitter filename"); | ||
|
|
||
| var outputPath = GetOutputPath(settings, refitGeneratorSettings); | ||
| Path.GetFullPath(outputPath).Should().Be(Path.GetFullPath(Path.Combine(tempDir, "Generated", "myapi.cs")), | ||
| "because default output folder is ./Generated rooted at the settings file directory"); | ||
| } | ||
| finally | ||
| { | ||
| CleanupDirectory(tempDir); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void SettingsFile_WithExplicitDefaultOutputFolder_RootsToSettingsFileDirectory() | ||
| { | ||
| var tempDir = CreateTempDirectory(); | ||
|
|
||
| try | ||
| { | ||
| var settingsFile = Path.Combine(tempDir, "api.refitter"); | ||
| File.WriteAllText(settingsFile, "{}"); | ||
|
|
||
| var settingsJson = """ | ||
| { | ||
| "openApiPath": "spec.json", | ||
| "namespace": "TestNamespace", | ||
| "outputFolder": "./Generated", | ||
| "outputFilename": "Api.cs" | ||
| } | ||
| """; | ||
|
|
||
| var refitGeneratorSettings = Serializer.Deserialize<RefitGeneratorSettings>(settingsJson); | ||
| var settings = new Settings { SettingsFilePath = settingsFile }; | ||
|
|
||
| ApplySettingsFileDefaults(settingsFile, refitGeneratorSettings); | ||
|
|
||
| var outputPath = GetOutputPath(settings, refitGeneratorSettings); | ||
| Path.GetFullPath(outputPath).Should().Be(Path.GetFullPath(Path.Combine(tempDir, "Generated", "Api.cs")), | ||
| "because outputFolder is rooted relative to the .refitter file directory"); | ||
| } | ||
| finally | ||
| { | ||
| CleanupDirectory(tempDir); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task SettingsFile_PreservesNamingInterfaceName() | ||
| { | ||
| // Arrange: .refitter with custom naming.interfaceName | ||
| var tempDir = CreateTempDirectory(); | ||
|
|
||
| try | ||
| { | ||
| var openApiFile = Path.Combine(tempDir, "spec.json"); | ||
| await File.WriteAllTextAsync(openApiFile, MinimalOpenApiSpec); | ||
|
|
||
| var settingsJson = """ | ||
| { | ||
| "openApiPath": "spec.json", | ||
| "namespace": "TestNamespace", | ||
| "naming": { | ||
| "useOpenApiTitle": false, | ||
| "interfaceName": "CustomApi" | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| var refitGeneratorSettings = Serializer.Deserialize<RefitGeneratorSettings>(settingsJson); | ||
| refitGeneratorSettings.OpenApiPath = openApiFile; | ||
|
|
||
| var generator = await RefitGenerator.CreateAsync(refitGeneratorSettings); | ||
| var code = generator.Generate(); | ||
|
|
||
| // Assert: Naming settings should be preserved and used in generation | ||
| refitGeneratorSettings.Naming.Should().NotBeNull(); | ||
| refitGeneratorSettings.Naming.InterfaceName.Should().Be("CustomApi"); | ||
| code.Should().Contain("interface ICustomApi", "because custom interface name should be used"); | ||
| BuildHelper.BuildCSharp(code).Should().BeTrue(); | ||
| } | ||
| finally | ||
| { | ||
| CleanupDirectory(tempDir); | ||
| } | ||
| } | ||
|
|
||
| private static string GetOutputPath(Settings settings, RefitGeneratorSettings refitGeneratorSettings) | ||
| { | ||
| var method = typeof(GenerateCommand).GetMethod( | ||
| "GetOutputPath", | ||
| BindingFlags.NonPublic | BindingFlags.Static, | ||
| null, | ||
| [typeof(Settings), typeof(RefitGeneratorSettings)], | ||
| null); | ||
|
|
||
| method.Should().NotBeNull(); | ||
|
|
||
| return (string)method!.Invoke(null, [settings, refitGeneratorSettings])!; | ||
| } | ||
|
|
||
| private static void ApplySettingsFileDefaults(string settingsFilePath, RefitGeneratorSettings refitGeneratorSettings) | ||
| { | ||
| var method = typeof(GenerateCommand).GetMethod( | ||
| "ApplySettingsFileDefaults", | ||
| BindingFlags.NonPublic | BindingFlags.Static); | ||
|
|
||
| method.Should().NotBeNull(); | ||
| method!.Invoke(null, [settingsFilePath, refitGeneratorSettings]); | ||
| } | ||
|
|
||
| private static string CreateTempDirectory() | ||
| { | ||
| var tempDir = Path.Combine(Path.GetTempPath(), $"refitter-test-{Guid.NewGuid()}"); | ||
| Directory.CreateDirectory(tempDir); | ||
| return tempDir; | ||
| } | ||
|
|
||
| private static void CleanupDirectory(string path) | ||
| { | ||
| if (Directory.Exists(path)) | ||
| Directory.Delete(path, recursive: true); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -233,4 +233,4 @@ private static async Task<string> GenerateCodeFromSpec(string openApiSpec) | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.