swagger-api / swagger-api/swagger-codegen

[TypeScript-Fetch] Invalid interface definition when using securityDefinitions

Open
#4,895 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Compiling the following spec into a TypeScript-Fetch client results in invalid code.
Specifically, the generated api.ts file contains

        // authentication (mykey) required
        if (configuration.apiKey && configuration.apiKey.x-api-key) {
            fetchOptions.headers = assign({
                    "x-api-key": configuration.apiKey.x-api-key,
                    }, contentTypeHeader);
        }

and configuration.ts contains

    apiKey: {
        x-api-key: string;
    };
Swagger-codegen version

Tested on 2.2.2 and on 2.3.0 (7aebcfa).

Swagger declaration file content or url
swagger: "2.0"
info:
  version: "1.0.0"
  title: Test
host: localhost:8080
basePath: /
securityDefinitions:
  mykey:
    type: apiKey
    in: header
    name: x-api-key
security:
  - mykey: []
paths:
  /test:
    get:
      responses:
        "200":
          description: Success
          schema:
            type: string
        default:
          description: Error
          schema:
            type: string
Command line used for generation

java -jar ../../swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ../my/test.yaml -l typescript-fetch

Steps to reproduce

Generate the TypeScript-Fetch definition for a Swagger spec that contains securityDefinitions with the - character in the key name.

Related issues

The lack of an implementation for security definitions was reported in #3970.

Suggest a Fix

Wrapping the identifiers in " and performing field accesses through the [] syntax as follows seems to fix the issue.

        // authentication (mykey) required
        if (configuration.apiKey && configuration.apiKey["x-api-key"]) {
            fetchOptions.headers = assign({
                    "x-api-key": configuration.apiKey["x-api-key"],
                    }, contentTypeHeader);
        }

...

    apiKey: {
        "x-api-key": string;
    };

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 the provided Swagger YAML and the typescript-fetch generation command. Inspect the generated api.ts and configuration.ts files for the invalid x-api-key identifiers; done means the generated client is valid TypeScript and preserves the intended API-key authentication behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.