[openapi-yaml] Config option outputFile was ignored by the generator. (#3199)

This commit is contained in:
Michal Foksa 2019-06-24 16:58:38 +02:00 committed by Jérémie Bresson
parent 06cfdbbdc6
commit 4ac33d80f0
4 changed files with 174 additions and 59 deletions

View File

@ -28,7 +28,7 @@ public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfi
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPIYamlGenerator.class);
protected String outputFile = "openapi.yaml";
protected String outputFile = "openapi/openapi.yaml";
public OpenAPIYamlGenerator() {
super();
@ -36,9 +36,6 @@ public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfi
outputFolder = "generated-code/openapi-yaml";
cliOptions.add(new CliOption(OUTPUT_NAME, "output filename"));
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
supportingFiles.add(new SupportingFile("openapi.mustache",
"openapi",
"openapi.yaml"));
}
@Override
@ -56,13 +53,14 @@ public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfi
return "Creates a static openapi.yaml file (OpenAPI spec v3).";
}
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey(OUTPUT_NAME)) {
this.outputFile = additionalProperties.get(OUTPUT_NAME).toString();
outputFile = additionalProperties.get(OUTPUT_NAME).toString();
}
LOGGER.info("Output file [outputFile={}]", outputFile);
supportingFiles.add(new SupportingFile("openapi.mustache", outputFile));
}
@Override
@ -71,7 +69,6 @@ public class OpenAPIYamlGenerator extends DefaultCodegen implements CodegenConfi
return super.postProcessSupportingFileData(objs);
}
@Override
public String escapeQuotationMark(String input) {
// just return the original string

View File

@ -1,5 +1,7 @@
package org.openapitools.codegen;
import static org.testng.Assert.assertTrue;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
@ -8,12 +10,15 @@ import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
import org.testng.Assert;
import java.io.File;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class TestUtils {
@ -55,4 +60,14 @@ public class TestUtils {
Assert.assertTrue(optional.isPresent());
return optional.get();
}
public static void ensureContainsFile(Map<String, String> generatedFiles, File root, String filename) {
File file = new File(root, filename);
String absoluteFilename = file.getAbsolutePath().replace("\\", "/");
if (!generatedFiles.containsKey(absoluteFilename)) {
Assert.fail("Could not find '" + absoluteFilename + "' file in list:\n" +
generatedFiles.keySet().stream().sorted().collect(Collectors.joining(",\n")));
}
assertTrue(generatedFiles.containsKey(absoluteFilename), "File '" + absoluteFilename + "' was not fould in the list of generated files");
}
}

View File

@ -17,15 +17,37 @@
package org.openapitools.codegen.java;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import com.google.common.collect.ImmutableMap;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.*;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.ClientOpts;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenResponse;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.MockDefaultGenerator;
import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.languages.JavaClientCodegen;
import org.testng.Assert;
@ -33,12 +55,15 @@ import org.testng.annotations.Test;
import java.io.File;
import java.nio.file.Files;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class JavaClientCodegenTest {
@Test
@ -257,42 +282,42 @@ public class JavaClientCodegenTest {
Map<String, String> generatedFiles = generator.getFiles();
Assert.assertEquals(generatedFiles.size(), 34);
ensureContainsFile(generatedFiles, output, ".gitignore");
ensureContainsFile(generatedFiles, output, ".openapi-generator-ignore");
ensureContainsFile(generatedFiles, output, ".openapi-generator/VERSION");
ensureContainsFile(generatedFiles, output, ".travis.yml");
ensureContainsFile(generatedFiles, output, "build.gradle");
ensureContainsFile(generatedFiles, output, "build.sbt");
ensureContainsFile(generatedFiles, output, "docs/DefaultApi.md");
ensureContainsFile(generatedFiles, output, "git_push.sh");
ensureContainsFile(generatedFiles, output, "gradle.properties");
ensureContainsFile(generatedFiles, output, "gradle/wrapper/gradle-wrapper.jar");
ensureContainsFile(generatedFiles, output, "gradle/wrapper/gradle-wrapper.properties");
ensureContainsFile(generatedFiles, output, "gradlew.bat");
ensureContainsFile(generatedFiles, output, "gradlew");
ensureContainsFile(generatedFiles, output, "pom.xml");
ensureContainsFile(generatedFiles, output, "README.md");
ensureContainsFile(generatedFiles, output, "settings.gradle");
ensureContainsFile(generatedFiles, output, "src/main/AndroidManifest.xml");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/api/DefaultApi.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiCallback.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiClient.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiException.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiResponse.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/ApiKeyAuth.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/Authentication.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBasicAuth.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBearerAuth.java");
//ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuth.java");
//ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuthFlow.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/Configuration.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/GzipRequestInterceptor.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/JSON.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/Pair.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ProgressRequestBody.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ProgressResponseBody.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/StringUtil.java");
ensureContainsFile(generatedFiles, output, "src/test/java/xyz/abcdef/api/DefaultApiTest.java");
TestUtils.ensureContainsFile(generatedFiles, output, ".gitignore");
TestUtils.ensureContainsFile(generatedFiles, output, ".openapi-generator-ignore");
TestUtils.ensureContainsFile(generatedFiles, output, ".openapi-generator/VERSION");
TestUtils.ensureContainsFile(generatedFiles, output, ".travis.yml");
TestUtils.ensureContainsFile(generatedFiles, output, "build.gradle");
TestUtils.ensureContainsFile(generatedFiles, output, "build.sbt");
TestUtils.ensureContainsFile(generatedFiles, output, "docs/DefaultApi.md");
TestUtils.ensureContainsFile(generatedFiles, output, "git_push.sh");
TestUtils.ensureContainsFile(generatedFiles, output, "gradle.properties");
TestUtils.ensureContainsFile(generatedFiles, output, "gradle/wrapper/gradle-wrapper.jar");
TestUtils.ensureContainsFile(generatedFiles, output, "gradle/wrapper/gradle-wrapper.properties");
TestUtils.ensureContainsFile(generatedFiles, output, "gradlew.bat");
TestUtils.ensureContainsFile(generatedFiles, output, "gradlew");
TestUtils.ensureContainsFile(generatedFiles, output, "pom.xml");
TestUtils.ensureContainsFile(generatedFiles, output, "README.md");
TestUtils.ensureContainsFile(generatedFiles, output, "settings.gradle");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/AndroidManifest.xml");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/api/DefaultApi.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiCallback.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiClient.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiException.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ApiResponse.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/ApiKeyAuth.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/Authentication.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBasicAuth.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBearerAuth.java");
//TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuth.java");
//TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuthFlow.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/Configuration.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/GzipRequestInterceptor.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/JSON.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/Pair.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ProgressRequestBody.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/ProgressResponseBody.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/StringUtil.java");
TestUtils.ensureContainsFile(generatedFiles, output, "src/test/java/xyz/abcdef/api/DefaultApiTest.java");
String defaultApiFilename = new File(output, "src/main/java/xyz/abcdef/api/DefaultApi.java").getAbsolutePath().replace("\\", "/");
String defaultApiConent = generatedFiles.get(defaultApiFilename);
@ -384,16 +409,6 @@ public class JavaClientCodegenTest {
Assert.assertEquals(cm.getClassname(), "OtherObj");
}
private void ensureContainsFile(Map<String, String> generatedFiles, File root, String filename) {
File file = new File(root, filename);
String absoluteFilename = file.getAbsolutePath().replace("\\", "/");
if (!generatedFiles.containsKey(absoluteFilename)) {
Assert.fail("Could not find '" + absoluteFilename + "' file in list:\n" +
generatedFiles.keySet().stream().sorted().collect(Collectors.joining(",\n")));
}
assertTrue(generatedFiles.containsKey(absoluteFilename), "File '" + absoluteFilename + "' was not fould in the list of generated files");
}
private CodegenProperty codegenPropertyWithArrayOfIntegerValues() {
CodegenProperty array = new CodegenProperty();
final CodegenProperty items = new CodegenProperty();

View File

@ -0,0 +1,88 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen.yaml;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.MockDefaultGenerator;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.languages.OpenAPIYamlGenerator;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class YamlGeneratorTest {
@Test
public void testGeneratePing() throws Exception {
Map<String, Object> properties = new HashMap<>();
File output = Files.createTempDirectory("test").toFile();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("openapi-yaml")
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/ping.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
final ClientOptInput clientOptInput = configurator.toClientOptInput();
MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(clientOptInput).generate();
Map<String, String> generatedFiles = generator.getFiles();
Assert.assertEquals(generatedFiles.size(), 4);
TestUtils.ensureContainsFile(generatedFiles, output, "openapi/openapi.yaml");
TestUtils.ensureContainsFile(generatedFiles, output, "README.md");
TestUtils.ensureContainsFile(generatedFiles, output, ".openapi-generator-ignore");
TestUtils.ensureContainsFile(generatedFiles, output, ".openapi-generator/VERSION");
output.deleteOnExit();
}
@Test
public void testGeneratePingOtherOutputFile() throws Exception {
Map<String, Object> properties = new HashMap<>();
properties.put(OpenAPIYamlGenerator.OUTPUT_NAME, "ping.yaml");
File output = Files.createTempDirectory("test").toFile();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("openapi-yaml")
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/ping.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
final ClientOptInput clientOptInput = configurator.toClientOptInput();
MockDefaultGenerator generator = new MockDefaultGenerator();
generator.opts(clientOptInput).generate();
Map<String, String> generatedFiles = generator.getFiles();
Assert.assertEquals(generatedFiles.size(), 4);
TestUtils.ensureContainsFile(generatedFiles, output, "ping.yaml");
TestUtils.ensureContainsFile(generatedFiles, output, "README.md");
TestUtils.ensureContainsFile(generatedFiles, output, ".openapi-generator-ignore");
TestUtils.ensureContainsFile(generatedFiles, output, ".openapi-generator/VERSION");
output.deleteOnExit();
}
}