OpenAPITools / OpenAPITools/openapi-generator-cli

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

Open
#1,094 1 comment 0 reactions 1 assignee View on GitHub

@kay-schecker is already working on this.

Since Jan 9, 2026.

bug
Dominant language
TypeScript
Stars
2k
Forks
208
Avg merge
7h 12m
Merged PRs (30d)
6

Description

🐛 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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.