Skip to content

[BUG] Environment variable not found (missing .env loading) #1094

@msemionova

Description

@msemionova

🐛 Bug Report:

Describe the bug

The ConfigService in the @openapitools/openapi-generator-cli package does not explicitly load .env files, which causes environment variables defined in .env to be inaccessible during placeholder replacement in the openapitools.json configuration file. This leads to errors when using ${env.VARIABLE_NAME} placeholders if the variables are not explicitly exported in the shell.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create a .env file with the following content:
ART_USER=testuser
ART_PASSWORD=testpassword
  1. Use ${env.ART_USER} and ${env.ART_PASSWORD} placeholders in the openapitools.json file.
    For example:
{
  "$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "7.4.0",
    "repository": {
      "downloadUrl": "https://${env.ART_USER}:${env.ART_PASSWORD}@artifactory.mycorp.com/maven-central/${groupId}/${artifactId}/${versionName}/${artifactId}-${versionName}.jar"
    }
  }
}
  1. Run the openapi-generator-cli command without explicitly exporting the environment variables in the shell.
    Observe that the placeholders are not replaced, and an error is logged:
Download 7.4.0 ...
Environment variable for placeholder 'ART_USER' not found.
Environment variable for placeholder 'ART_PASSWORD' not found.
Download failed, because of: "Request failed with status code 401"

Expected behavior

The ConfigService should automatically load .env files so that environment variables defined in .env are accessible during placeholder replacement.

Screenshots

N/A

Operation System:

  • OS: MacOS
  • Version: Sequoia 15.5

Package System:

  • @openapitools/openapi-generator-cli Version: 2.27.0

Additional context

The issue lies in the replacePlaceholders class. While it attempts to replace placeholders with environment variables, it does not explicitly load .env files. This can be fixed by adding support for .env file loading using the dotenv package.

Suggested fix

Modify the ConfigService to explicitly load .env files in its constructor:

import { config as loadEnv } from 'dotenv'; // Import dotenv

constructor(
  @Inject(LOGGER) private readonly logger: LOGGER,
  @Inject(COMMANDER_PROGRAM) private readonly program: Command,
) {
  // Explicitly load .env file
  loadEnv();
}

This ensures that .env variables are loaded into process.env before placeholder replacement occurs.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions