0
# Configuration Management
1
2
The configuration system provides fluent APIs for setting up code generation parameters, including input specifications, output directories, package names, and custom properties.
3
4
## Configuration Classes
5
6
### CodegenConfigurator
7
8
Main configuration builder for setting up code generation workflows.
9
10
```java { .api }
11
class CodegenConfigurator {
12
// Static factory methods
13
public static CodegenConfigurator fromFile(String configFile, Module... modules);
14
15
// Core configuration
16
public CodegenConfigurator setGeneratorName(String generatorName);
17
public CodegenConfigurator setInputSpec(String inputSpec);
18
public CodegenConfigurator setOutputDir(String outputDir);
19
public CodegenConfigurator setTemplateDir(String templateDir);
20
21
// Package configuration
22
public CodegenConfigurator setApiPackage(String apiPackage);
23
public CodegenConfigurator setModelPackage(String modelPackage);
24
public CodegenConfigurator setInvokerPackage(String invokerPackage);
25
26
// Artifact configuration
27
public CodegenConfigurator setGroupId(String groupId);
28
public CodegenConfigurator setArtifactId(String artifactId);
29
public CodegenConfigurator setArtifactVersion(String artifactVersion);
30
public CodegenConfigurator setLibrary(String library);
31
32
// Authentication
33
public CodegenConfigurator setAuth(String auth);
34
35
// Custom properties
36
public CodegenConfigurator addAdditionalProperty(String key, Object value);
37
public CodegenConfigurator addTypeMapping(String key, String value);
38
public CodegenConfigurator addImportMapping(String key, String value);
39
public CodegenConfigurator addLanguageSpecificPrimitive(String value);
40
41
// Generation control
42
public CodegenConfigurator setSkipOverwrite(Boolean skipOverwrite);
43
public CodegenConfigurator setRemoveOperationIdPrefix(Boolean removeOperationIdPrefix);
44
45
// Build methods
46
public ClientOptInput toClientOptInput();
47
public Context<?> toContext();
48
}
49
```
50
51
### ClientOptInput
52
53
Container for generator input parameters and configuration.
54
55
```java { .api }
56
class ClientOptInput {
57
// Fluent configuration
58
public ClientOptInput openAPI(OpenAPI openAPI);
59
public ClientOptInput config(CodegenConfig codegenConfig);
60
public ClientOptInput userDefinedTemplates(List<TemplateDefinition> userDefinedTemplates);
61
62
// Getters (deprecated but functional)
63
@Deprecated public OpenAPI getOpenAPI();
64
@Deprecated public CodegenConfig getConfig();
65
@Deprecated public List<TemplateDefinition> getUserDefinedTemplates();
66
}
67
```
68
69
### CliOption
70
71
Represents a command-line option for code generation.
72
73
```java { .api }
74
class CliOption {
75
// Constructors
76
public CliOption(String opt, String description);
77
public CliOption(String opt, String description, String type);
78
79
// Static factory methods
80
public static CliOption newBoolean(String opt, String description);
81
public static CliOption newBoolean(String opt, String description, boolean defaultValue);
82
public static CliOption newString(String opt, String description);
83
84
// Configuration
85
public CliOption defaultValue(String defaultValue);
86
public CliOption addEnum(String value, String description);
87
88
// Getters
89
public String getOpt();
90
public String getDescription();
91
public String getType();
92
public String getDefault();
93
public String getOptionHelp();
94
public Map<String, String> getEnum();
95
}
96
```
97
98
## Usage Examples
99
100
### Basic Configuration
101
102
```java
103
// Create configurator and set basic options
104
CodegenConfigurator configurator = new CodegenConfigurator()
105
.setGeneratorName("java")
106
.setInputSpec("https://petstore.swagger.io/v2/swagger.json")
107
.setOutputDir("./generated-client")
108
.setApiPackage("com.example.petstore.api")
109
.setModelPackage("com.example.petstore.model")
110
.setInvokerPackage("com.example.petstore.client");
111
112
// Generate code
113
DefaultGenerator generator = new DefaultGenerator();
114
generator.opts(configurator.toClientOptInput()).generate();
115
```
116
117
### Configuration from File
118
119
```java
120
// Load configuration from JSON file
121
CodegenConfigurator configurator = CodegenConfigurator.fromFile("config.json");
122
123
// Additional configuration can be added
124
configurator
125
.setOutputDir("./custom-output")
126
.addAdditionalProperty("dateLibrary", "java8");
127
128
// Generate with loaded configuration
129
ClientOptInput input = configurator.toClientOptInput();
130
new DefaultGenerator().opts(input).generate();
131
```
132
133
Example configuration file (`config.json`):
134
```json
135
{
136
"generatorName": "java",
137
"inputSpec": "./openapi.yaml",
138
"outputDir": "./generated",
139
"apiPackage": "com.example.api",
140
"modelPackage": "com.example.model",
141
"additionalProperties": {
142
"dateLibrary": "java8",
143
"java8": "true",
144
"hideGenerationTimestamp": "true"
145
},
146
"typeMappings": {
147
"DateTime": "java.time.OffsetDateTime"
148
}
149
}
150
```
151
152
### Advanced Configuration
153
154
```java
155
CodegenConfigurator configurator = new CodegenConfigurator()
156
.setGeneratorName("spring")
157
.setInputSpec("api-spec.yaml")
158
.setOutputDir("./spring-server")
159
.setApiPackage("com.example.controller")
160
.setModelPackage("com.example.dto")
161
.setInvokerPackage("com.example")
162
163
// Library and framework options
164
.setLibrary("spring-boot")
165
.addAdditionalProperty("dateLibrary", "java8")
166
.addAdditionalProperty("java8", "true")
167
.addAdditionalProperty("interfaceOnly", "true")
168
.addAdditionalProperty("skipDefaultInterface", "true")
169
170
// Maven coordinates
171
.setGroupId("com.example")
172
.setArtifactId("petstore-api")
173
.setArtifactVersion("1.0.0")
174
175
// Type and import mappings
176
.addTypeMapping("DateTime", "java.time.OffsetDateTime")
177
.addImportMapping("OffsetDateTime", "java.time.OffsetDateTime")
178
.addLanguageSpecificPrimitive("java.time.OffsetDateTime")
179
180
// Generation control
181
.setSkipOverwrite(false)
182
.setRemoveOperationIdPrefix(true);
183
184
// Generate with advanced configuration
185
new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
186
```
187
188
### Custom CLI Options
189
190
```java
191
public class MyCustomGenerator extends DefaultCodegen {
192
193
public static final String USE_OPTIONAL = "useOptional";
194
public static final String ASYNC_NATIVE = "asyncNative";
195
196
public MyCustomGenerator() {
197
super();
198
199
// Add custom CLI options
200
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,
201
"Use Optional container for optional parameters", false));
202
203
cliOptions.add(CliOption.newString(ASYNC_NATIVE,
204
"Native async library to use")
205
.addEnum("none", "No async support")
206
.addEnum("completablefuture", "Use CompletableFuture")
207
.addEnum("reactor", "Use Reactor")
208
.defaultValue("none"));
209
}
210
211
@Override
212
public void processOpts() {
213
super.processOpts();
214
215
// Process custom options
216
if (additionalProperties.containsKey(USE_OPTIONAL)) {
217
boolean useOptional = convertPropertyToBooleanAndWriteBack(USE_OPTIONAL);
218
additionalProperties.put("useOptional", useOptional);
219
}
220
221
if (additionalProperties.containsKey(ASYNC_NATIVE)) {
222
String asyncNative = (String) additionalProperties.get(ASYNC_NATIVE);
223
additionalProperties.put("asyncNative", asyncNative);
224
additionalProperties.put("isReactor", "reactor".equals(asyncNative));
225
}
226
}
227
}
228
```
229
230
### Template Configuration
231
232
```java
233
CodegenConfigurator configurator = new CodegenConfigurator()
234
.setGeneratorName("java")
235
.setInputSpec("openapi.yaml")
236
.setOutputDir("./generated")
237
.setTemplateDir("./custom-templates") // Use custom templates
238
239
// Additional properties for template customization
240
.addAdditionalProperty("apiDocumentationUrl", "https://docs.example.com")
241
.addAdditionalProperty("modelDocumentationUrl", "https://docs.example.com/models")
242
.addAdditionalProperty("hideGenerationTimestamp", true)
243
.addAdditionalProperty("serializationLibrary", "jackson");
244
245
// Custom user-defined templates can also be added
246
List<TemplateDefinition> userTemplates = Arrays.asList(
247
new SupportingFile("custom-readme.mustache", "", "README.md"),
248
new SupportingFile("dockerfile.mustache", "docker", "Dockerfile")
249
);
250
251
ClientOptInput input = configurator.toClientOptInput()
252
.userDefinedTemplates(userTemplates);
253
254
new DefaultGenerator().opts(input).generate();
255
```
256
257
## Constants
258
259
### CodegenConstants
260
261
Static constants for common configuration keys.
262
263
```java { .api }
264
class CodegenConstants {
265
// System properties
266
public static final String APIS = "apis";
267
public static final String MODELS = "models";
268
public static final String SUPPORTING_FILES = "supportingFiles";
269
public static final String MODEL_TESTS = "modelTests";
270
public static final String MODEL_DOCS = "modelDocs";
271
public static final String API_TESTS = "apiTests";
272
public static final String API_DOCS = "apiDocs";
273
274
// Package configuration
275
public static final String API_PACKAGE = "apiPackage";
276
public static final String MODEL_PACKAGE = "modelPackage";
277
public static final String INVOKER_PACKAGE = "invokerPackage";
278
279
// Template configuration
280
public static final String TEMPLATE_DIR = "templateDir";
281
282
// Common options
283
public static final String GROUP_ID = "groupId";
284
public static final String ARTIFACT_ID = "artifactId";
285
public static final String ARTIFACT_VERSION = "artifactVersion";
286
public static final String LIBRARY = "library";
287
public static final String GIT_USER_ID = "gitUserId";
288
public static final String GIT_REPO_ID = "gitRepoId";
289
290
// Behavior flags
291
public static final String ALLOW_UNICODE_IDENTIFIERS = "allowUnicodeIdentifiers";
292
public static final String REMOVE_OPERATION_ID_PREFIX = "removeOperationIdPrefix";
293
public static final String SKIP_OVERWRITE = "skipOverwrite";
294
public static final String HIDE_GENERATION_TIMESTAMP = "hideGenerationTimestamp";
295
}
296
```
297
298
### Usage with Constants
299
300
```java
301
CodegenConfigurator configurator = new CodegenConfigurator()
302
.setGeneratorName("java")
303
.setInputSpec("openapi.yaml")
304
.setOutputDir("./generated")
305
.addAdditionalProperty(CodegenConstants.HIDE_GENERATION_TIMESTAMP, true)
306
.addAdditionalProperty(CodegenConstants.SKIP_OVERWRITE, false)
307
.addAdditionalProperty(CodegenConstants.LIBRARY, "okhttp-gson");
308
```