From 944e1c34688c29b0003a4dd0d371afb28ae3285c Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Thu, 18 Jul 2019 09:50:54 -0400 Subject: [PATCH] [fix] Assign template directory to additional properties (#3385) * [fix] Assign template directory to additional properties --- .../codegen/config/CodegenConfigurator.java | 11 ++ .../config/CodegenConfiguratorTest.java | 122 ++++++++++++++++++ .../builds/multiple-parameters/runtime.ts | 7 +- .../php-symfony/SymfonyBundle-php/.gitignore | 54 ++++++++ 4 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/config/CodegenConfiguratorTest.java create mode 100644 samples/server/petstore/php-symfony/SymfonyBundle-php/.gitignore diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java index 4c251ffc4a0..88f2a548989 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java @@ -223,6 +223,7 @@ public class CodegenConfigurator { } public CodegenConfigurator setImportMappings(Map importMappings) { + this.importMappings = importMappings; generatorSettingsBuilder.withImportMappings(importMappings); return this; } @@ -234,6 +235,7 @@ public class CodegenConfigurator { } public CodegenConfigurator setInstantiationTypes(Map instantiationTypes) { + this.instantiationTypes = instantiationTypes; generatorSettingsBuilder.withInstantiationTypes(instantiationTypes); return this; } @@ -295,6 +297,7 @@ public class CodegenConfigurator { } public CodegenConfigurator setReservedWordsMappings(Map reservedWordMappings) { + this.reservedWordMappings = reservedWordMappings; generatorSettingsBuilder.withReservedWordMappings(reservedWordMappings); return this; } @@ -310,6 +313,7 @@ public class CodegenConfigurator { } public CodegenConfigurator setSystemProperties(Map systemProperties) { + this.systemProperties = systemProperties; workflowSettingsBuilder.withSystemProperties(systemProperties); return this; } @@ -326,6 +330,7 @@ public class CodegenConfigurator { } public CodegenConfigurator setTypeMappings(Map typeMappings) { + this.typeMappings = typeMappings; generatorSettingsBuilder.withTypeMappings(typeMappings); return this; } @@ -454,6 +459,12 @@ public class CodegenConfigurator { config.reservedWordsMappings().putAll(generatorSettings.getReservedWordMappings()); config.additionalProperties().putAll(generatorSettings.getAdditionalProperties()); + // any other additional properties? + String templateDir = workflowSettings.getTemplateDir(); + if (templateDir != null) { + config.additionalProperties().put(CodegenConstants.TEMPLATE_DIR, workflowSettings.getTemplateDir()); + } + ClientOptInput input = new ClientOptInput() .config(config); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/config/CodegenConfiguratorTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/config/CodegenConfiguratorTest.java new file mode 100644 index 00000000000..68271020ca8 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/config/CodegenConfiguratorTest.java @@ -0,0 +1,122 @@ +/* + * Copyright 2019 OpenAPI-Generator Contributors (https://openapi-generator.tech) + * + * 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.config; + +import org.openapitools.codegen.ClientOptInput; +import org.openapitools.codegen.CodegenConfig; +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.DefaultGenerator; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import static org.testng.Assert.*; + +public class CodegenConfiguratorTest { + private void want(Map additionalProperties, String key, Object expected) { + assertEquals(additionalProperties.getOrDefault(key, null), expected); + } + + @Test + public void shouldSetConfiglProperties() throws IOException { + // This tests that properties we set on CodegenConfigurator make it down into generator properties, + // limiting to those managed in DefaultCodegen. + Map properties = new HashMap() {{ + put("foo", "bar"); + put("baz", "quux"); + put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, true); + put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, true); + put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, false); + put(CodegenConstants.ENSURE_UNIQUE_PARAMS, true); + put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, true); + put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, false); + put(CodegenConstants.DOCEXTENSION, "D"); + put(CodegenConstants.ENABLE_POST_PROCESS_FILE, false); + put(CodegenConstants.GENERATE_ALIAS_AS_MODEL, true); + }}; + + File output = Files.createTempDirectory("test").toFile(); + File template = Files.createTempDirectory("test").toFile(); + String outDir = Paths.get(output.toURI()).toAbsolutePath().toString(); + String templateDir = Paths.get(template.toURI()).toAbsolutePath().toString(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("java") + .setAdditionalProperties(properties) + .setInputSpec("src/test/resources/3_0/ping.yaml") + .addImportMapping("one", "two") + .addInstantiationType("three", "four") + .addLanguageSpecificPrimitive("five") + .addSystemProperty("six", "seven") + .addTypeMapping("eight", "nine") + .setApiPackage("test-api") + .setArtifactId("test-artifactId") + .setArtifactVersion("test-artifactVersion") + .setAuth("test-auth") + .setGitRepoId("git") + .setGitUserId("user") + .setGroupId("group") + .setHttpUserAgent("agent") + .setModelNamePrefix("model-prefix") + .setModelNameSuffix("model-suffix") + .setModelPackage("model-package") + .setPackageName("package-name") + .setReleaseNote("release-note") + .setTemplateDir(templateDir) + .setOutputDir(outDir); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + + CodegenConfig config = clientOptInput.getConfig(); + config.processOpts(); + + Map props = config.additionalProperties(); + + // This verifies that things we expect to make it into the template will, as a result of this CodegenConfigurator. + want(props, CodegenConstants.MODEL_PACKAGE, "model_package"); // * mutated by codegen + want(props, CodegenConstants.API_PACKAGE, "test_api"); // * mutated by codegen + want(props, CodegenConstants.HIDE_GENERATION_TIMESTAMP, true); + want(props, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, true); + want(props, CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, false); + want(props, CodegenConstants.ENSURE_UNIQUE_PARAMS, true); + want(props, CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, true); + want(props, CodegenConstants.MODEL_NAME_PREFIX, "model-prefix"); + want(props, CodegenConstants.MODEL_NAME_SUFFIX, "model-suffix"); + want(props, CodegenConstants.REMOVE_OPERATION_ID_PREFIX, false); + want(props, CodegenConstants.DOCEXTENSION, "D"); + want(props, CodegenConstants.ENABLE_POST_PROCESS_FILE, false); + want(props, CodegenConstants.GENERATE_ALIAS_AS_MODEL, true); + want(props, CodegenConstants.TEMPLATE_DIR, templateDir); + want(props, CodegenConstants.GIT_REPO_ID, "git"); + want(props, CodegenConstants.GIT_USER_ID, "user"); + want(props, CodegenConstants.GROUP_ID, "group"); + want(props, CodegenConstants.ARTIFACT_ID, "test-artifactId"); + want(props, CodegenConstants.ARTIFACT_VERSION, "test-artifactVersion"); + want(props, CodegenConstants.HTTP_USER_AGENT, "agent"); + want(props, CodegenConstants.RELEASE_NOTE, "release-note"); + want(props, CodegenConstants.PACKAGE_NAME, "package-name"); + + // test custom properties + want(props, "foo", "bar"); + want(props, "baz", "quux"); + } +} \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts b/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts index 5b07f811186..c95a101fbac 100644 --- a/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts +++ b/samples/client/petstore/typescript-fetch/builds/multiple-parameters/runtime.ts @@ -58,7 +58,7 @@ export class BaseAPI { // only add the querystring to the URL if there are query parameters. // this is done to avoid urls ending with a "?" character which buggy webservers // do not handle correctly sometimes. - url += '?' + querystring(context.query); + url += '?' + this.configuration.queryParamsStringify(context.query); } const body = (context.body instanceof FormData || isBlob(context.body)) ? context.body @@ -127,6 +127,7 @@ export interface ConfigurationParameters { basePath?: string; // override base path fetchApi?: FetchAPI; // override for fetch implementation middleware?: Middleware[]; // middleware to apply before/after fetch requests + queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings username?: string; // parameter for basic security password?: string; // parameter for basic security apiKey?: string | ((name: string) => string); // parameter for apiKey security @@ -148,6 +149,10 @@ export class Configuration { return this.configuration.middleware || []; } + get queryParamsStringify(): (params: HTTPQuery) => string { + return this.configuration.queryParamsStringify || querystring; + } + get username(): string | undefined { return this.configuration.username; } diff --git a/samples/server/petstore/php-symfony/SymfonyBundle-php/.gitignore b/samples/server/petstore/php-symfony/SymfonyBundle-php/.gitignore new file mode 100644 index 00000000000..20b7b989760 --- /dev/null +++ b/samples/server/petstore/php-symfony/SymfonyBundle-php/.gitignore @@ -0,0 +1,54 @@ +# ref: https://github.com/github/gitignore/blob/master/Symfony.gitignore + +# Cache and logs (Symfony2) +/app/cache/* +/app/logs/* +!app/cache/.gitkeep +!app/logs/.gitkeep + +# Email spool folder +/app/spool/* + +# Cache, session files and logs (Symfony3) +/var/cache/* +/var/logs/* +/var/sessions/* +!var/cache/.gitkeep +!var/logs/.gitkeep +!var/sessions/.gitkeep + +# Parameters +/app/config/parameters.yml +/app/config/parameters.ini + +# Managed by Composer +/app/bootstrap.php.cache +/var/bootstrap.php.cache +/bin/* +!bin/console +!bin/symfony_requirements +/vendor/ + +# Assets and user uploads +/web/bundles/ +/web/uploads/ + +# PHPUnit +/app/phpunit.xml +/phpunit.xml + +# Build data +/build/ + +# Composer PHAR +/composer.phar + +# Backup entities generated with doctrine:generate:entities command +**/Entity/*~ + +# Embedded web-server pid file +/.web-server-pid + +# From root gitignore +/Tests/cache/ +/Tests/logs/ \ No newline at end of file