diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java index aa386a2995f3..0d33707e32d6 100644 --- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java +++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java @@ -70,7 +70,7 @@ * Goal which generates client/server code from a OpenAPI json/yaml definition. */ @SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"}) -@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES) +@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) public class CodeGenMojo extends AbstractMojo { private static final Logger LOGGER = LoggerFactory.getLogger(CodeGenMojo.class); 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 a60f824278b1..840790ed4c8d 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 @@ -79,67 +79,72 @@ public CodegenConfigurator() { public static CodegenConfigurator fromFile(String configFile, Module... modules) { if (isNotEmpty(configFile)) { - ObjectMapper mapper; + DynamicSettings settings = readDynamicSettings(configFile, modules); - if (FilenameUtils.isExtension(configFile, new String[]{"yml", "yaml"})) { - mapper = Yaml.mapper(); - } else { - mapper = Json.mapper(); - } + CodegenConfigurator configurator = new CodegenConfigurator(); - if (modules != null && modules.length > 0) { - mapper.registerModules(modules); + GeneratorSettings generatorSettings = settings.getGeneratorSettings(); + WorkflowSettings workflowSettings = settings.getWorkflowSettings(); + + // We copy "cached" properties into configurator so it is appropriately configured with all settings in external files. + // FIXME: target is to eventually move away from CodegenConfigurator properties except gen/workflow settings. + configurator.generatorName = generatorSettings.getGeneratorName(); + configurator.inputSpec = workflowSettings.getInputSpec(); + configurator.templatingEngineName = workflowSettings.getTemplatingEngineName(); + if (workflowSettings.getSystemProperties() != null) { + configurator.systemProperties.putAll(workflowSettings.getSystemProperties()); + } + if(generatorSettings.getInstantiationTypes() != null) { + configurator.instantiationTypes.putAll(generatorSettings.getInstantiationTypes()); + } + if(generatorSettings.getTypeMappings() != null) { + configurator.typeMappings.putAll(generatorSettings.getTypeMappings()); + } + if(generatorSettings.getAdditionalProperties() != null) { + configurator.additionalProperties.putAll(generatorSettings.getAdditionalProperties()); + } + if(generatorSettings.getImportMappings() != null) { + configurator.importMappings.putAll(generatorSettings.getImportMappings()); + } + if(generatorSettings.getLanguageSpecificPrimitives() != null) { + configurator.languageSpecificPrimitives.addAll(generatorSettings.getLanguageSpecificPrimitives()); + } + if(generatorSettings.getReservedWordMappings() != null) { + configurator.reservedWordMappings.putAll(generatorSettings.getReservedWordMappings()); + } + if(generatorSettings.getServerVariables() != null) { + configurator.serverVariables.putAll(generatorSettings.getServerVariables()); } - mapper.registerModule(new GuavaModule()); + configurator.generatorSettingsBuilder = GeneratorSettings.newBuilder(generatorSettings); + configurator.workflowSettingsBuilder = WorkflowSettings.newBuilder(workflowSettings); - try { - DynamicSettings settings = mapper.readValue(new File(configFile), DynamicSettings.class); - CodegenConfigurator configurator = new CodegenConfigurator(); + return configurator; + } + return null; + } - GeneratorSettings generatorSettings = settings.getGeneratorSettings(); - WorkflowSettings workflowSettings = settings.getWorkflowSettings(); + private static DynamicSettings readDynamicSettings(String configFile, Module... modules) { + ObjectMapper mapper; - // We copy "cached" properties into configurator so it is appropriately configured with all settings in external files. - // FIXME: target is to eventually move away from CodegenConfigurator properties except gen/workflow settings. - configurator.generatorName = generatorSettings.getGeneratorName(); - configurator.inputSpec = workflowSettings.getInputSpec(); - configurator.templatingEngineName = workflowSettings.getTemplatingEngineName(); - if (workflowSettings.getSystemProperties() != null) { - configurator.systemProperties.putAll(workflowSettings.getSystemProperties()); - } - if(generatorSettings.getInstantiationTypes() != null) { - configurator.instantiationTypes.putAll(generatorSettings.getInstantiationTypes()); - } - if(generatorSettings.getTypeMappings() != null) { - configurator.typeMappings.putAll(generatorSettings.getTypeMappings()); - } - if(generatorSettings.getAdditionalProperties() != null) { - configurator.additionalProperties.putAll(generatorSettings.getAdditionalProperties()); - } - if(generatorSettings.getImportMappings() != null) { - configurator.importMappings.putAll(generatorSettings.getImportMappings()); - } - if(generatorSettings.getLanguageSpecificPrimitives() != null) { - configurator.languageSpecificPrimitives.addAll(generatorSettings.getLanguageSpecificPrimitives()); - } - if(generatorSettings.getReservedWordMappings() != null) { - configurator.reservedWordMappings.putAll(generatorSettings.getReservedWordMappings()); - } - if(generatorSettings.getServerVariables() != null) { - configurator.serverVariables.putAll(generatorSettings.getServerVariables()); - } + if (FilenameUtils.isExtension(configFile.toLowerCase(Locale.ROOT), new String[]{"yml", "yaml"})) { + mapper = Yaml.mapper().copy(); + } else { + mapper = Json.mapper().copy(); + } + + if (modules != null && modules.length > 0) { + mapper.registerModules(modules); + } - configurator.generatorSettingsBuilder = GeneratorSettings.newBuilder(generatorSettings); - configurator.workflowSettingsBuilder = WorkflowSettings.newBuilder(workflowSettings); + mapper.registerModule(new GuavaModule()); - return configurator; - } catch (IOException ex) { - LOGGER.error(ex.getMessage()); - throw new RuntimeException("Unable to deserialize config file: " + configFile); - } + try { + return mapper.readValue(new File(configFile), DynamicSettings.class); + } catch (IOException ex) { + LOGGER.error(ex.getMessage()); + throw new RuntimeException("Unable to deserialize config file: " + configFile); } - return null; } public CodegenConfigurator addServerVariable(String key, String value) {