OpenApiGenerator - How can I skip generating file changes because of a version change
Asked Answered
D

1

3

I am using the typescript-rxjs generator. Whenever I generate a new version of my API clients, all files get changed, 99% of them because of the version change: * The version of the OpenAPI document: 1.47.0-rc.20. Real changes get lost in that amount of changes.

I don't want to overwrite all file templates of the generator. That seems to be inefficient since I would have to watch changes of the templates.

Dragoman answered 7/12, 2020 at 5:35 Comment(2)
In the meantime I'm setting the swagger version to 0.0.0 right before I generate the api, so all real changes are now visible in my git commits.Dragoman
Is this an issue with changes in git? If so, why commit generated files?Buskined
R
2

The different OpenAPI generators use Mustache as the template engine for generating the different artifacts.

In this specific use case, the version information is defined in licenseInfo.mustache.

This template is included by the rest of templates used to generate the different client artifacts.

You can try to modify that template and provide your own version. The OpenAPI generator gives you the ability to override existing templates.

Once modified, you need to configure the tool (CLI, Maven, Gradle) you are using as your generator to indicate where to look for the modified template.

If necessary, you can even provide your own custom user defined templates.

Another approach could be to define some kind of file post-processing logic that will be executed once the generator has finished its process.

In the case of the typescript-rxjs generator, the code to be executed must be defined as a string value in the environment variable TS_POST_PROCESS_FILE.

A typical example:

export TS_POST_PROCESS_FILE="/usr/local/bin/prettier --write"

In your case perhaps you can define some kind of script, for instance, with sed or other text processing tool, if you are using a linux or unix based OS, or a custom node.js script, that removes that information. The solution will depend again on the actual mechanism - CLI, Maven, Gradle,... - you are using for generating your code.

Having said that, please consider if it is necessary to put a generated file under version control. Possibly there are mechanisms that would allow it to be generated and used directly by your code, or you can define CI flows that generate an npm package that could be used in a private registry and use this package in your different projects, if necessary.

Restricted answered 5/1, 2021 at 23:35 Comment(5)
@MaximilianFriedmann Please, consider indicate in the question the tool your are using to generate the code. It could be of help in provide you a more convenient guidance in the answer. I hope some of the solutions listed are helpful.Restricted
I use the OpenApi Generator and the typescript-rxjs template. I explicitly asked for a solution where I don't need to modify the templates. If there's no other solution, I will have to go for that.Dragoman
Thank you very much for the feedback @MaximilianFriedmann, I really appreciate it. Yes, but I mean, are you using Maven, Gradle, the CLI? I understand. Please, in any case, be aware that as far I understand you only need to override the mustache templates you desire, not all of them, and certainly licenseInfo will be a template included in the generator. In addition, what about the post process file approach? I am aware that it will probably provide a less portable solution, but certainly will allow you to process your generated files without the need of changing the templatesRestricted
Didn't know that I can partially override them, many thanks for the hint. I use the NPM CLI, it's working now.Dragoman
That is great @MaximilianFriedmann. I am very happy to know that you were able to make it work. Please, do not hesitate to contact me if I can be of help.Restricted

© 2022 - 2024 — McMap. All rights reserved.