OpenAPITools / OpenAPITools/openapi-generator

[BUG] OpenApi Generated ApiModule doesn’t register when declared in node_modules but works outside that folder

Open
#11,302 5 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Asked on stackoverflow too
https://stackoverflow.com/questions/70700113/in-angualr-why-openapi-generated-apimodule-doesn-t-register-when-declared-in-nod

Description

I’m having an issue. I need to consume OpenApi Generated files (services, interfaces) via NPM package. The problem it doesn’t work when it’s in node_modules, but if I move it outside this folder it works. ApiModule comes as undefined when in node_modules for some reason. Github link in the bottom is documentation how to use it.

I have tried this on 2 different machines running Angular 12 (Node.js v12 and v16) with:

If I put in a different folder than node_modules it works, inside it doesn’t. Any help or ideas? Thanks.

Generation Details
app.module.ts:30 Uncaught TypeError: Cannot read properties of undefined (reading 'forRoot')
    at Module.6747 (app.module.ts:30)
    at __webpack_require__ (bootstrap:19)
    at Module.4431 (environment.ts:16)
    at __webpack_require__ (bootstrap:19)
    at __webpack_exec__ (log$:23)
    at log$:23
    at Function.__webpack_require__.O (chunk loaded:23)
    at log$:23
    at webpackJsonpCallback (jsonp chunk loading:33)
    at main.js:1
Steps to reproduce

Download either

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';

////// added
import { HttpClientModule } from '@angular/common/http';
import {
  ApiModule,
  Configuration,
  ConfigurationParameters,
} from 'client-petstore';


export function apiConfigFactory(): Configuration {
  const params: ConfigurationParameters = {
    basePath: 'https://localhost:4200',
  };
  return new Configuration(params);
}
////// end

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ////// added
    ApiModule.forRoot(apiConfigFactory),
    HttpClientModule
    ////// end
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Contributor guide

Open the contributing guide

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.

Research direction

Start with the Angular 12 reproduction in app.module.ts, installing the mentioned client-petstore package under node_modules and checking the ApiModule import used by ApiModule.forRoot(apiConfigFactory). Done means the generated ApiModule is defined and registers successfully from the npm package, matching the behavior when the generated files are outside node_modules.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, node.js, openapi, typescript
Domain
api, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.