Skip to content

Commit f951470

Browse files
authored
[json][codegen] Add option: outputFileName (#7348)
1 parent 7ac7974 commit f951470

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

docs/generators/openapi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ sidebar_label: openapi
99
|disallowAdditionalPropertiesIfNotPresent|Specify the behavior when the 'additionalProperties' keyword is not present in the OAS document. If false: the 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications. If true: when the 'additionalProperties' keyword is not present in a schema, the value of 'additionalProperties' is set to false, i.e. no additional properties are allowed. Note: this mode is not compliant with the JSON schema specification. This is the original openapi-generator behavior.This setting is currently ignored for OAS 2.0 documents: 1) When the 'additionalProperties' keyword is not present in a 2.0 schema, additional properties are NOT allowed. 2) Boolean values of the 'additionalProperties' keyword are ignored. It's as if additional properties are NOT allowed.Note: the root cause are issues #1369 and #1371, which must be resolved in the swagger-parser project.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>when the 'additionalProperties' keyword is not present in a schema, the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. Note: this mode is not compliant with the JSON schema specification. This is the original openapi-generator behavior.</dd></dl>|true|
1010
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
1111
|legacyDiscriminatorBehavior|This flag is used by OpenAPITools codegen to influence the processing of the discriminator attribute in OpenAPI documents. This flag has no impact if the OAS document does not use the discriminator attribute. The default value of this flag is set in each language-specific code generator (e.g. Python, Java, go...)using the method toModelName. Note to developers supporting a language generator in OpenAPITools; to fully support the discriminator attribute as defined in the OAS specification 3.x, language generators should set this flag to true by default; however this requires updating the mustache templates to generate a language-specific discriminator lookup function that iterates over {{#mappedModels}} and does not iterate over {{children}}, {{#anyOf}}, or {{#oneOf}}.|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
12+
|outputFileName|Output file name| |openapi.json|
1213
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1314
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
1415
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919

2020
import io.swagger.v3.oas.models.OpenAPI;
2121
import org.apache.commons.io.FileUtils;
22-
import org.openapitools.codegen.CodegenConfig;
23-
import org.openapitools.codegen.CodegenType;
24-
import org.openapitools.codegen.DefaultCodegen;
25-
import org.openapitools.codegen.SupportingFile;
22+
import org.openapitools.codegen.*;
2623
import org.openapitools.codegen.meta.features.*;
2724
import org.openapitools.codegen.serializer.SerializerUtils;
2825
import org.slf4j.Logger;
@@ -33,9 +30,12 @@
3330
import java.util.EnumSet;
3431

3532
public class OpenAPIGenerator extends DefaultCodegen implements CodegenConfig {
33+
public static final String OUTPUT_NAME = "outputFileName";
3634

3735
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPIGenerator.class);
3836

37+
protected String outputFileName = "openapi.json";
38+
3939
public OpenAPIGenerator() {
4040
super();
4141

@@ -53,6 +53,8 @@ public OpenAPIGenerator() {
5353
outputFolder = "generated-code/openapi";
5454

5555
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
56+
57+
cliOptions.add(CliOption.newString(OUTPUT_NAME, "Output file name").defaultValue(outputFileName));
5658
}
5759

5860
@Override
@@ -70,12 +72,22 @@ public String getHelp() {
7072
return "Creates a static openapi.json file (OpenAPI spec v3.0).";
7173
}
7274

75+
@Override
76+
public void processOpts() {
77+
super.processOpts();
78+
79+
if (additionalProperties.containsKey(OUTPUT_NAME)) {
80+
outputFileName = additionalProperties.get(OUTPUT_NAME).toString();
81+
}
82+
LOGGER.info("Output file name [outputFileName={}]", outputFileName);
83+
}
84+
7385
@Override
7486
public void processOpenAPI(OpenAPI openAPI) {
7587
String jsonOpenAPI = SerializerUtils.toJsonString(openAPI);
7688

7789
try {
78-
String outputFile = outputFolder + File.separator + "openapi.json";
90+
String outputFile = outputFolder + File.separator + outputFileName;
7991
FileUtils.writeStringToFile(new File(outputFile), jsonOpenAPI, StandardCharsets.UTF_8);
8092
LOGGER.info("wrote file to " + outputFile);
8193
} catch (Exception e) {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.openapitools.codegen.json;
2+
3+
import org.openapitools.codegen.ClientOptInput;
4+
import org.openapitools.codegen.DefaultGenerator;
5+
import org.openapitools.codegen.config.CodegenConfigurator;
6+
import org.openapitools.codegen.languages.OpenAPIGenerator;
7+
import org.testng.annotations.Test;
8+
9+
import java.io.File;
10+
import java.nio.file.Files;
11+
import java.nio.file.Paths;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static org.openapitools.codegen.TestUtils.assertFileContains;
16+
17+
public class JsonGeneratorTest {
18+
19+
@Test
20+
public void testGeneratePing() throws Exception {
21+
Map<String, Object> properties = new HashMap<>();
22+
23+
File output = Files.createTempDirectory("test").toFile();
24+
String outputPath = output.getAbsolutePath().replace('\\', '/');
25+
26+
final CodegenConfigurator configurator = new CodegenConfigurator()
27+
.setGeneratorName("openapi")
28+
.setAdditionalProperties(properties)
29+
.setInputSpec("src/test/resources/3_0/ping.yaml")
30+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
31+
32+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
33+
DefaultGenerator generator = new DefaultGenerator();
34+
generator.opts(clientOptInput).generate();
35+
assertFileContains(Paths.get(outputPath + "/openapi.json"));
36+
assertFileContains(Paths.get(outputPath + "/README.md"));
37+
assertFileContains(Paths.get(outputPath + "/.openapi-generator-ignore"));
38+
assertFileContains(Paths.get(outputPath + "/.openapi-generator/FILES"));
39+
assertFileContains(Paths.get(outputPath + "/.openapi-generator/VERSION"));
40+
41+
output.deleteOnExit();
42+
}
43+
44+
45+
@Test
46+
public void testGeneratePingOtherOutputFile() throws Exception {
47+
Map<String, Object> properties = new HashMap<>();
48+
properties.put(OpenAPIGenerator.OUTPUT_NAME, "ping.json");
49+
50+
File output = Files.createTempDirectory("test").toFile();
51+
String outputPath = output.getAbsolutePath().replace('\\', '/');
52+
53+
final CodegenConfigurator configurator = new CodegenConfigurator()
54+
.setGeneratorName("openapi")
55+
.setAdditionalProperties(properties)
56+
.setInputSpec("src/test/resources/3_0/ping.yaml")
57+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
58+
59+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
60+
DefaultGenerator generator = new DefaultGenerator();
61+
generator.opts(clientOptInput).generate();
62+
assertFileContains(Paths.get(outputPath + "/ping.json"));
63+
assertFileContains(Paths.get(outputPath + "/README.md"));
64+
assertFileContains(Paths.get(outputPath + "/.openapi-generator-ignore"));
65+
assertFileContains(Paths.get(outputPath + "/.openapi-generator/FILES"));
66+
assertFileContains(Paths.get(outputPath + "/.openapi-generator/VERSION"));
67+
68+
output.deleteOnExit();
69+
}
70+
}

0 commit comments

Comments
 (0)