swagger-api / swagger-api/swagger-codegen

angular 7 AOT issue Object is possibly 'undefined'

Open
#9,316 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Please help me with following issue.
AOT compilation fails for generated angular client in this code

        if (this.configuration.apiKeys["Authorization"]) {
            headers = headers.set('Authorization', this.configuration.apiKeys["Authorization"]);
        }

error message: error TS2532: Object is possibly 'undefined'.

I'm using https://generator.swagger.io (currently version 2.4.4)
operation POST /gen/clients/typescript-angular
Body:

{
"spec": {},
"options": {
"ngVersion": "7.1.3"
},
"swaggerUrl": "..trimmed.."
}

Maybe it will be good to change generated code to be

        if (this.configuration.apiKeys && this.configuration.apiKeys["Authorization"]) {
            headers = headers.set('Authorization', this.configuration.apiKeys["Authorization"]);
        }

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

Reproduce the issue with generator.swagger.io operation POST /gen/clients/typescript-angular, generator version 2.4.4, and ngVersion 7.1.3. Inspect the generated TypeScript Angular client around configuration.apiKeys and verify that AOT compilation no longer reports TS2532. Confirm the generated client still sets the Authorization header when the key is present.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.