Skip to content

Commit 8eea149

Browse files
authored
[doc] Document usage of post-process file feature (#7315)
1 parent 3007483 commit 8eea149

5 files changed

Lines changed: 80 additions & 3 deletions

File tree

docs/file-post-processing.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
id: file-post-processing
3+
title: File post-processing
4+
---
5+
6+
Each tool (CLI and plugins) supports enabling file post-processing at a high-level. Enabling this option allows for generators which support post-processing to call some external process for each generated file, passing the file path to that tool. The external tool must be defined in an environment variable supported by the generator.
7+
8+
Note that:
9+
10+
* this option is `--enable-post-process-file` in the CLI and `enablePostProcessFile` in plugins
11+
* we require _both_ specifying the environment variable _and_ enabling the option at the tooling level; this feature is opt-in for security
12+
* file processing occurs one at a time
13+
* the external tool may be a custom script which invokes multiple tools
14+
15+
Also refer to the relevant documentation for [CLI](./usage.md), [Maven Plugin](https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md), [Gradle Plugin](https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-gradle-plugin/README.adoc), or [SBT Plugin](https://github.com/OpenAPITools/sbt-openapi-generator/blob/master/README.md).
16+
17+
## Supported Environment Variables
18+
19+
The following environment variables are supported by their respective generators:
20+
<!-- query with: grep -Rn '_POST_PROCESS_FILE"' modules | grep -Eo '[^"]+_POST_PROCESS_FILE' | sort -u -->
21+
22+
* `CPP_POST_PROCESS_FILE`
23+
* `CSHARP_POST_PROCESS_FILE`
24+
* `C_POST_PROCESS_FILE`
25+
* `DART_POST_PROCESS_FILE`
26+
* `FSHARP_POST_PROCESS_FILE`
27+
* `GO_POST_PROCESS_FILE`
28+
* `HASKELL_POST_PROCESS_FILE`
29+
* `JAVA_POST_PROCESS_FILE`
30+
* `JS_POST_PROCESS_FILE`
31+
* `KOTLIN_POST_PROCESS_FILE`
32+
* `OCAML_POST_PROCESS_FILE`
33+
* `PERL_POST_PROCESS_FILE`
34+
* `PHP_POST_PROCESS_FILE`
35+
* `POWERSHELL_POST_PROCESS_FILE`
36+
* `PYTHON_POST_PROCESS_FILE`
37+
* `RUBY_POST_PROCESS_FILE`
38+
* `RUST_POST_PROCESS_FILE`
39+
* `SCALA_POST_PROCESS_FILE`
40+
* `SWIFT_POST_PROCESS_FILE`
41+
* `TS_POST_PROCESS_FILE`
42+
43+
## Example
44+
45+
Let's see how to pass Ruby generated files to Rubocop, a static code analysis/linter/formatter tool.
46+
47+
```
48+
# First, export the required environment variable
49+
export RUBY_POST_PROCESS_FILE="/usr/local/bin/rubocop -a"
50+
51+
export OPENAPI_DOC="https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml"
52+
53+
# Invoke the generator with --enable-post-process-file
54+
openapi-generator generate --enable-post-process-file -i $OPENAPI_DOC -g ruby -o .out-ruby/
55+
```
56+
57+
You will now see messages logged about which files have been processed:
58+
59+
```
60+
[main] INFO o.o.codegen.TemplateManager - writing file /Users/jim/projects/openapi-generator/.out-ruby/.rspec
61+
[main] INFO o.o.codegen.TemplateManager - writing file /Users/jim/projects/openapi-generator/.out-ruby/spec/spec_helper.rb
62+
[main] INFO o.o.c.languages.AbstractRubyCodegen - Successfully executed: /usr/local/bin/rubocopy -a /Users/jim/projects/openapi-generator/.out-ruby/spec/spec_helper.rb
63+
[main] INFO o.o.codegen.TemplateManager - writing file /Users/jim/projects/openapi-generator/.out-ruby/spec/configuration_spec.rb
64+
[main] INFO o.o.c.languages.AbstractRubyCodegen - Successfully executed: /usr/local/bin/rubocopy -a /Users/jim/projects/openapi-generator/.out-ruby/spec/configuration_spec.rb
65+
[main] INFO o.o.codegen.TemplateManager - writing file /Users/jim/projects/openapi-generator/.out-ruby/spec/api_client_spec.rb
66+
[main] INFO o.o.c.languages.AbstractRubyCodegen - Successfully executed: /usr/local/bin/rubocopy -a /Users/jim/projects/openapi-generator/.out-ruby/spec/api_client_spec.rb
67+
[main] INFO o.o.codegen.TemplateManager - Skipped /Users/jim/projects/openapi-generator/.out-ruby/.openapi-generator-ignore (Skipped by supportingFiles options supplied by user.)
68+
[main] INFO o.o.codegen.TemplateManager - writing file /Users/jim/projects/openapi-generator/.out-ruby/.openapi-generator/VERSION
69+
[main] INFO o.o.codegen.TemplateManager - writing file /Users/jim/projects/openapi-generator/.out-ruby/.openapi-generator/FILES
70+
```

docs/usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ openapi-generator generate \
522522
523523
> NOTE: mappings are applied to `DateTime`, as this is the representation of the primitive type. See [DefaultCodegen](https://github.com/OpenAPITools/openapi-generator/blob/7cee999543fcc00b7c1eb9f70f0456b707c7f9e2/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L1431).
524524
525+
#### File Post-Processing
526+
527+
The `--enable-post-process-file` option enables specific generators to invoke some external language-specific formatting script. Each filename is passed _individually_ to this external script, allowing for linting, formatting, or other custom clean-up.
528+
529+
For more details, see [File Post-Processing](./file-post-processing.md).
530+
525531
### Target External Models
526532
527533
Sometimes you don't want the codegen to make a model for you--you might want to just include one that already exists in your codebase. Say you already have a `User` object and want to reuse that, which has a different model package from the other generated files:

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public class Generate extends OpenApiGeneratorCommand {
234234
+ " Useful for piping the JSON output of debug options (e.g. `--global-property debugOperations`) to an external parser directly while testing a generator.")
235235
private Boolean logToStderr;
236236

237-
@Option(name = {"--enable-post-process-file"}, title = "enable post-process file", description = CodegenConstants.ENABLE_POST_PROCESS_FILE_DESC)
237+
@Option(name = {"--enable-post-process-file"}, title = "enable post-processing of files (in generators supporting it)", description = CodegenConstants.ENABLE_POST_PROCESS_FILE_DESC)
238238
private Boolean enablePostProcessFile;
239239

240240
@Option(name = {"--generate-alias-as-model"}, title = "generate alias (array, map) as model", description = CodegenConstants.GENERATE_ALIAS_AS_MODEL_DESC)

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/OCamlClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public void processOpts() {
437437

438438
if (StringUtils.isEmpty(System.getenv("OCAML_POST_PROCESS_FILE"))) {
439439
LOGGER.info("Hint: Environment variable 'OCAML_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export OCAML_POST_PROCESS_FILE=\"ocamlformat -i --enable-outside-detected-project\"' (Linux/Mac)");
440-
LOGGER.info("Note: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
440+
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
441441
} else if (!this.isEnablePostProcessFile()) {
442442
LOGGER.info("Warning: Environment variable 'OCAML_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
443443
}

website/sidebars.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module.exports = {
66
'plugins',
77
'online',
88
'usage',
9-
'globals'
9+
'globals',
10+
'file-post-processing'
1011
],
1112
'Extending': [
1213
'templating',

0 commit comments

Comments
 (0)