OpenAPITools / OpenAPITools/openapi-generator
[REQ] add the possibility to check if generated code would change according to spec
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
Rationale: You have a VCS repository containing the OpenAPI specification files as well as the generated code files. You submit generated code to VCS to be available for inspection and review in code reviews. Code generation is run manually. The person changing the OpenAPI specification is responsible for generating the code. To ensure that this task is not forgotten you need a check if the OpenAPI specification and the generated code is still in sync. This check should be able to execute automatically in CI pipelines.
Specification and code being "in sync" is defined as a subsequent generation run to the same output directory over existing files not resulting in any changes to the files.
Describe the solution you'd like
The basic building blocks are already there in terms of the dry-run functionality (specifically DryRunTemplateManager, DryRunStatus for recording of file status) and TemplateManager (for checking if output written to temp file is similar to content of existing output file).
I propose to get rid of DryRunTemplateManager as it is too simplistic and instead add the functionality to the TemplateManager instead. This would then collect the file status on every run and only skip writing files on dry-run. This should be a neglectable performance overhead. On the plus side the TemplateManager already does file content comparisons.
The whole wording around DryRunStatus should then probably change to FileStatus to reflect that change. Additionally, I would change the state enum:
- remove
WriteIfNewer - add
Updatedif an existing file was changed - add
Uptodateif an existing file was not changed because the content was up-to-date
Since the dry-run functionality is currently a bit broken (see #15730) this is not that heavily used and those changes should be possible.
Then I propose to add a new task to the Gradle plugin (probably also Maven plugin and CLI). I have only investigated the Gradle plugin so far. There a common task for generating and checking for changes would be added where both can inherit from as the functionality between generating and checking for changes is really the same with the exception that on checking changes we do not update output files. Then a new ChangesTask would run code generation in dry-run mode and make use of the collected file state info by utilizing a hasChanges method on the Generator.
Describe alternatives you've considered
I think there is not much to be gained from extending DryRunTemplateManager if TemplateManager is already equipped with the basic building blocks. So I think there are no real alternatives.
Additional context
Proof of concept can be found at https://github.com/Eurofunk/openapi-generator/commit/a0f2cfa4dad316690f82470678c75f528d57154b.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with DryRunTemplateManager, DryRunStatus, and TemplateManager to understand the existing dry-run and file-comparison behavior, then review the Gradle plugin's generation task and Generator state handling. Done means generation can report whether existing files would change without writing them, with corresponding status terminology and a check task; Maven and CLI support are also proposed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system, cli, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100