openapi-generator-dart
openapi-generator-dart copied to clipboard
Generator adds comment into version controlled source file containing the Openapi annotation
Description of the bug
Every time, when the OpenAPI generator runs, it adds following comment line to the source file containing the Openapi annotation:
// Openapi Generator last run: : 2024-02-01T14:45:00.661335
Since that source file is typically under source control its automatic modification is very annoying, because a developer has either to revert the git change or commit it after each dart run build_runner build. The situation becomes even worse, if the dart run build_runner build is executed each time before the normal flutter build.
Steps to reproduce
execute dart run build_runner build
Expected behavior
The source file containing the openapi annotation is not touched.
Logs
No response
Screenshots
No response
Platform
macOS
Library version
5.0.2
Flutter version
3.16.9
Flutter channel
stable
Additional context
No response
I have the same issue. I've submitted a PR (#139) to allow optional updates to the annotated file.
And also, as a temporary workaround, I've created a script that removes timestamp comment using rps.
pubspec.yaml:
scripts:
gen-apiclient:
$script: dart run build_runner build --delete-conflicting-outputs --build-filter='lib/my_generator_config.dart'
$after: $remove-timestamp
remove-timestamp:
$script:
$windows: powershell -Command "(Get-Content lib\my_generator_config.dart) -notmatch '^\/\/ Openapi Generator last run' | Foreach-Object { $_ + \"`n\" } | Set-Content lib\my_generator_config.dart -NoNewline"
$default: sed -i '' '/^\/\/ Openapi Generator last run/d' lib/my_generator_config.dart
I would like to add that this also causes issues when running dart run build_runner watch. Each time the generator runs, the timestamp is updated causing the watch command to trigger the generation again, resulting in an infinite loop.
If you don't need to rebuild the API everytime the build runner runs, then add this to your build.yaml:
targets:
$default:
builders:
openapi_generator:
enabled: false
if you add a new spec or need to update, just set enabled: true.
Hope this helps!
Thank you for the hint. It seems my case is one of "you are not using it correctly".
I would still prefer that the tool would not touch my manually written files. IMO it would be better to write this comment into one of the generated files instead.
I can speak to this bit! I was torn when writing this functionality. I am not a fan of writing it into a manually written file. There are limitations into how build_runner works. While it would be amazing to be able to always rerun and rebuild when we run dart run build_runner build but the build_runner team is opposed to that. Since this means that we aren't able to rebuild unless we make modifications to the dart files; updating the dart defined file made sense as it allowed for the rebuilding of the spec.
I did consider the possibility of writing it in the generated files, it is difficult as to which file that should live in. There are files that can be omitted via a config file and thus makes it difficult to guarantee which generated files are going to exists. One possibility is to include it in the main exported file of the library.
As users of this library what would be a more preferential handling here.
In the mean time would suggest you follow @grAPPfruit in configuring the build runner to not build everytime until we have a solution that best meets both developer and user simplicity.