forked from loafle/openapi-generator-original
[json][codegen] Add option: outputFileName (#7348)
This commit is contained in:
@@ -9,6 +9,7 @@ sidebar_label: openapi
|
||||
|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|
|
||||
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
|
||||
|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|
|
||||
|outputFileName|Output file name| |openapi.json|
|
||||
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|
||||
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|
||||
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|
||||
|
||||
@@ -19,10 +19,7 @@ package org.openapitools.codegen.languages;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.openapitools.codegen.CodegenConfig;
|
||||
import org.openapitools.codegen.CodegenType;
|
||||
import org.openapitools.codegen.DefaultCodegen;
|
||||
import org.openapitools.codegen.SupportingFile;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.meta.features.*;
|
||||
import org.openapitools.codegen.serializer.SerializerUtils;
|
||||
import org.slf4j.Logger;
|
||||
@@ -33,9 +30,12 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.EnumSet;
|
||||
|
||||
public class OpenAPIGenerator extends DefaultCodegen implements CodegenConfig {
|
||||
public static final String OUTPUT_NAME = "outputFileName";
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPIGenerator.class);
|
||||
|
||||
protected String outputFileName = "openapi.json";
|
||||
|
||||
public OpenAPIGenerator() {
|
||||
super();
|
||||
|
||||
@@ -53,6 +53,8 @@ public class OpenAPIGenerator extends DefaultCodegen implements CodegenConfig {
|
||||
outputFolder = "generated-code/openapi";
|
||||
|
||||
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
|
||||
|
||||
cliOptions.add(CliOption.newString(OUTPUT_NAME, "Output file name").defaultValue(outputFileName));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,12 +72,22 @@ public class OpenAPIGenerator extends DefaultCodegen implements CodegenConfig {
|
||||
return "Creates a static openapi.json file (OpenAPI spec v3.0).";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey(OUTPUT_NAME)) {
|
||||
outputFileName = additionalProperties.get(OUTPUT_NAME).toString();
|
||||
}
|
||||
LOGGER.info("Output file name [outputFileName={}]", outputFileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpenAPI(OpenAPI openAPI) {
|
||||
String jsonOpenAPI = SerializerUtils.toJsonString(openAPI);
|
||||
|
||||
try {
|
||||
String outputFile = outputFolder + File.separator + "openapi.json";
|
||||
String outputFile = outputFolder + File.separator + outputFileName;
|
||||
FileUtils.writeStringToFile(new File(outputFile), jsonOpenAPI, StandardCharsets.UTF_8);
|
||||
LOGGER.info("wrote file to " + outputFile);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.openapitools.codegen.json;
|
||||
|
||||
import org.openapitools.codegen.ClientOptInput;
|
||||
import org.openapitools.codegen.DefaultGenerator;
|
||||
import org.openapitools.codegen.config.CodegenConfigurator;
|
||||
import org.openapitools.codegen.languages.OpenAPIGenerator;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.openapitools.codegen.TestUtils.assertFileContains;
|
||||
|
||||
public class JsonGeneratorTest {
|
||||
|
||||
@Test
|
||||
public void testGeneratePing() throws Exception {
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
|
||||
File output = Files.createTempDirectory("test").toFile();
|
||||
String outputPath = output.getAbsolutePath().replace('\\', '/');
|
||||
|
||||
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||
.setGeneratorName("openapi")
|
||||
.setAdditionalProperties(properties)
|
||||
.setInputSpec("src/test/resources/3_0/ping.yaml")
|
||||
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
|
||||
|
||||
final ClientOptInput clientOptInput = configurator.toClientOptInput();
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
generator.opts(clientOptInput).generate();
|
||||
assertFileContains(Paths.get(outputPath + "/openapi.json"));
|
||||
assertFileContains(Paths.get(outputPath + "/README.md"));
|
||||
assertFileContains(Paths.get(outputPath + "/.openapi-generator-ignore"));
|
||||
assertFileContains(Paths.get(outputPath + "/.openapi-generator/FILES"));
|
||||
assertFileContains(Paths.get(outputPath + "/.openapi-generator/VERSION"));
|
||||
|
||||
output.deleteOnExit();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGeneratePingOtherOutputFile() throws Exception {
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put(OpenAPIGenerator.OUTPUT_NAME, "ping.json");
|
||||
|
||||
File output = Files.createTempDirectory("test").toFile();
|
||||
String outputPath = output.getAbsolutePath().replace('\\', '/');
|
||||
|
||||
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||
.setGeneratorName("openapi")
|
||||
.setAdditionalProperties(properties)
|
||||
.setInputSpec("src/test/resources/3_0/ping.yaml")
|
||||
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
|
||||
|
||||
final ClientOptInput clientOptInput = configurator.toClientOptInput();
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
generator.opts(clientOptInput).generate();
|
||||
assertFileContains(Paths.get(outputPath + "/ping.json"));
|
||||
assertFileContains(Paths.get(outputPath + "/README.md"));
|
||||
assertFileContains(Paths.get(outputPath + "/.openapi-generator-ignore"));
|
||||
assertFileContains(Paths.get(outputPath + "/.openapi-generator/FILES"));
|
||||
assertFileContains(Paths.get(outputPath + "/.openapi-generator/VERSION"));
|
||||
|
||||
output.deleteOnExit();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user