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

Output filename customization #200

Merged
merged 18 commits into from
Oct 28, 2023
Merged

Output filename customization #200

merged 18 commits into from
Oct 28, 2023

Conversation

christianhelle
Copy link
Owner

The changes here allow further customization of the Refitter output filename using the outputFilename setting in the .refitter settings file

{
  "openApiPath": "https://petstore3.swagger.io/api/v3/openapi.json",
  "namespace": "Petstore",
  "outputFolder": "GeneratedCode",
  "outputFilename": "SwaggerPetstore.cs",
  "naming": {    
    "useOpenApiTitle": false,
    "interfaceName": "SwaggerPetstore"
  }
}

This implements #184

@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Oct 24, 2023
@christianhelle christianhelle self-assigned this Oct 24, 2023
@christianhelle christianhelle linked an issue Oct 24, 2023 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Oct 24, 2023

Codecov Report

Merging #200 (91fceb1) into main (cb3eeb7) will increase coverage by 0.00%.
Report is 21 commits behind head on main.
The diff coverage is 100.00%.

❗ Current head 91fceb1 differs from pull request most recent head a213294. Consider uploading reports for the commit a213294 to get more accurate results

@@           Coverage Diff           @@
##             main     #200   +/-   ##
=======================================
  Coverage   98.64%   98.64%           
=======================================
  Files          52       52           
  Lines        1840     1841    +1     
=======================================
+ Hits         1815     1816    +1     
  Misses          9        9           
  Partials       16       16           
Flag Coverage Δ
unittests 98.64% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/Refitter.Core/Serializer.cs 100.00% <100.00%> (ø)
...itter.Core/Settings/DependencyInjectionSettings.cs 100.00% <ø> (ø)

@github-actions
Copy link

github-actions bot commented Oct 26, 2023

Qodana for .NET

It seems all right 👌

No new problems were found according to the checks applied

💡 Qodana analysis was run in the pull request mode: only the changed files were checked
☁️ View the detailed Qodana report

Contact Qodana team

Contact us at qodana-support@jetbrains.com

@christianhelle christianhelle force-pushed the output-filename branch 2 times, most recently from 67ffe12 to b608c62 Compare October 27, 2023 12:09
In the JsonSerializer class, I've added the setting `PropertyNamingPolicy = JsonNamingPolicy.CamelCase` to the JsonSerializerOptions object. The change is to ensure the serialized JSON adheres to the camel case naming convention. This convention is a common standard in JSON data exchange and makes the serialized data more manageable in client apps, such as JavaScript which typically use camel case naming.
Removed JsonPropertyName attributes from properties in various settings classes. These attributes were unnecessary because the Newtonsoft.Json package automatically infers the JsonPropertyName from the property name and they were adding redundancy.
This commit introduces a new property, OutputFilename, to the RefitGeneratorSettings class. This allows users to optionally set the name of the file generated by the Refit library. This flexibility was added to meet diverse user preferences and requirements for file naming.
The filename in the RefitterSourceGenerator has been moved from the initial file read into the subsequent try block. The change improves the code readability and flow, by keeping the file output manipulation logic in one place. It also allows the filename to leverage the OutputFilename setting if it's provided, providing more flexible file naming.
The SwaggerPetstoreCustomOutputFolder.g.cs file was deleted and recreated as CustomGenerated.cs. This change was made to provide better clarity and organization to the auto-generated files structure in the CustomGenerated directory. The SwaggerPetstoreCustomOutputFolder.refitter configuration file was also updated to reflect this new output file name.
The code was modified to allow users more control over output path configuration in RefitGenerator. Now, users can define the output folder and filename either directly with --output or via 'outputFolder' and 'outputFilename' settings in the file. A validation error will be returned if both routes are attempted simultaneously. This change offers greater flexibility and security to avoid path-related problems. Default values have also been shifted from being hard coded, to being set as constants within Settings and RefitGeneratorSettings classes.
The check for whether the directory for the output file exists and its creation if it doesn't, was moved. Previously it was checking for a valid directory for settings.OutputPath before calculating the actual output path. Now the directory verification is done after calculating the actual outputPath, ensuring that a directory will always be created for the correct outputPath.
These additional fields were added to petstore.refitter to simplify the refitter configuration. It eliminates the redundancy of specifying the output file path every time in the smoke-tests script. As a result, the refitter call within the smoke-tests script has been adjusted to no longer include the output path. The output file name was also updated in the GenerateAndBuild call to match the petstore.refitter setting.
Changed the file name in the test assertion to match updates in the code generation. This was necessary to reflect new file naming convention which is part of an effort to streamline the code generation process.
This commit decomposes the validation logic previously located in the `GenerateCommand` class, and moves it into its own `SettingsValidator` class for better organization and reusability. This provides a cleaner architecture and easier future maintenance, as now settings validation can be altered independent from command generation logic.
…tion"

This commit adds an optional parameter `outputFilename` to the configuration options in the JSON files of `Refitter.SourceGenerator`, `../README` and `Refitter`. The `outputFilename` allows users to specify a name for the output file, with the default being `Output.cs` for the CLI tool. This change was necessary to provide users more flexibility in naming their output files.

Additionally, the commit includes updates to the documentation files - `Refitter.SourceGenerator/README`, `../README`, `Refitter/README` and `../docs/docfx_project/articles/refitter-file-format.md` - to explain the new `outputFilename` option. It also adjusts the explanation on `operationNameTemplate` for clarity.

Extra explanatory comments were added to `dependencyInjectionSettings` and a new block `codeGeneratorSettings` was included in the `Refitter/README.md` file to give users more control over the generated types and contracts.
Simplified the Validate method in SettingsValidator by removing the unnecessary CommandContext parameter. This change optimizes the method by improving readability and reducing complexity. Passed Settings directly as an argument.
The ValidateFileExistence method in SettingsValidator.cs has been made static. This change was made because the method does not access any instance members and does not depend on specific object state. Making methods that satisfy these criteria static can result in minor performance improvements.
The ValidateOperationNameAndUrl method in SettingsValidator.cs was changed to be static. This was done because the method does not use any instance variables and can be called without an instance of the class. This improves performance and reduces memory footprint.
The method ValidateFileAndOutputSettings in SettingsValidator.cs was changed to be static. This change was made because we found out during code review that this method does not use any instance data, therefore it makes sense to make it static to improve the readability and maintainability of the code.
The method ValidateFilePath in SettingsValidator.cs was changed to static since it doesn't use or change any instance-level state. This promotes good practice and increased efficiency by avoiding unnecessary object instantiation.
The Validate method in SettingsValidator class got changed from an instance method to a static method. This was done to avoid creating unnecessary instances of SettingsValidator when validating settings in GenerateCommand.cs. The related call in GenerateCommand has been modified accordingly.
@sonarcloud
Copy link

sonarcloud bot commented Oct 27, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@christianhelle christianhelle merged commit f834c91 into main Oct 28, 2023
426 of 427 checks passed
@christianhelle christianhelle deleted the output-filename branch October 28, 2023 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Single output
1 participant