Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #12935
Closed
Open
Issue created Jul 19, 2022 by Administrator@rootContributor5 of 5 checklist items completed5/5 checklist items

[BUG] [Typescript-Angular] client service generator does not honor injected BASE_PATH

Created by: ry99

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

I am trying to generate angular client services from a yaml file. I was previously using the swagger-codegen-cli-v3 docker image, but want to switch to openapi-generator-cli. My application injects the BASE_PATH variable in the app.module.ts file using APP_INITIALIZER callbacks. The BASE_PATH is determined at runtime from a configuration file. It is not available at generation time.

However, when generating client services, the services do not honor the injected BASE_PATH if the configuration is undefined and the basePath argument is not a string...and the basePath argument is always an array of strings

The swagger generator produces code that accepts the injected value in all cases:

    constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (basePath) {
            this.basePath = basePath;
        }
        if (configuration) {
            this.configuration = configuration;
            this.basePath = basePath || configuration.basePath || this.basePath;
        }
    }

The openapi-generator produces code that ignores the injected basePath (in my case, at least):

    constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (configuration) {
            this.configuration = configuration;
        }
        if (typeof this.configuration.basePath !== 'string') {
            if (typeof basePath !== 'string') {
                basePath = this.basePath;
            }
            this.configuration.basePath = basePath;
        }
        this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
    }

The basePath argument is an array, not a string (I return a string from my APP_INITIALIZER), so the code block uses the (incorrect) default basePath.

openapi-generator version

6.0.1

OpenAPI declaration file content or url

Seems to happen with any yaml (probably json, too). I just picked one off the internet

https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml

Generation Details
Steps to reproduce
  1. openapi-generator-cli generate -i XXX.yaml -g typescript-angular -o generated --additional-properties=ngVersion=14.15.4,providedInRoot=true
Related issues/PRs

#12936

Suggest a fix

I assume the basePath argument to the service constructor is supposed to be an array, so the moustache file needs to be updated to reflect the argument types, and handle them appropriately.

https://github.com/OpenAPITools/openapi-generator/blob/92193f7159d62419634c51eb7d2e9a4e0697d283/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache#L69-L80

Assignee
Assign to
Time tracking