swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[TypeScript-Fetch] Invalid interface definition when using additionalProperties

Open ranma42 opened this issue 8 years ago • 17 comments

Description

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

export interface GenericMap extends null<String, number> {
}

which the TypeScript compiler rejects.

Swagger-codegen version

I tested it on 2.2.1 (latest stable) and 2.3.0 (7aebcfa).

Swagger declaration file content or url
...
definitions:
  GenericMap:
    additionalProperties:
      type: integer
  Container:
    type: object
    properties:
      works:
        type: object
        additionalProperties:
          type: string
      breaks:
        $ref: "#/definitions/GenericMap"
Command line used for generation

swagger-codegen generate -i test.yaml -l typescript-fetch

Steps to reproduce

Compile a swagger API containing the definitions mentioned above into a typescript-fetch client.

Suggest a Fix

The Container type shows that additionalProperties are correctly handled in some cases (when used for the anonymous type of the works member), but not when they are used for a type which is basically a map. I believe it would be sufficient to emit the following type, without inheriting from any other type:

export interface GenericMap {
  [key: string]: number;
}

ranma42 avatar Feb 22 '17 17:02 ranma42

@ranma42 thanks for reporting the issue. Can you try the latest master, which uses swagger parser 1.0.26-snapshot with better support for reference?

wing328 avatar Feb 23 '17 07:02 wing328

The latest master (d11d0f88bb032d4a3302f90ab3addeb1a6b0559b) seems to have the same problem.

ranma42 avatar Feb 23 '17 09:02 ranma42

@ranma42 thanks for testing the latest master, may I suggest defining the map inline instead of a reference as a workaround?

wing328 avatar Feb 23 '17 09:02 wing328

I am already doing that for my project ;) I reported the issue because I was suggested to do so in the Swagger IRC channel (at first I was unsure if the spec was bad or it was just handled incorrectly).

ranma42 avatar Feb 23 '17 09:02 ranma42

I am already doing that for my project ;)

👍

Looks like an issue that needs to be addressed in the Swagger Parser

wing328 avatar Feb 23 '17 09:02 wing328

@wing328 should I close this and report against https://github.com/swagger-api/swagger-parser ?

ranma42 avatar Feb 23 '17 09:02 ranma42

@ranma42 yes please open an issue there as well.

wing328 avatar Feb 23 '17 09:02 wing328

This still seems to be an issue in the 2.2.3 release.

thegecko avatar Sep 04 '17 18:09 thegecko

Same thing happens on the typescript-node templates on the latest release.

baronfel avatar Sep 12 '17 21:09 baronfel

cc @TiFu @taxpon @sebastianhaas @kenisteward

wing328 avatar Sep 19 '17 08:09 wing328

@ranma42 @wing328 Can I ask to clarify a bit how to make the workaround you mentioned.

I suggest defining the map inline instead of a reference as a workaround

But it's already inlined, not referenced. 👇

As the issue says, I have this input yaml:

  GenericMap:
    additionalProperties:
      type: integer

And I want to have a generated result like this:

export interface GenericMap {
  [key: string]: number;
}

But getting this instead:

export interface GenericMap extends null<String, number> { }

gordey4doronin avatar Jan 29 '18 12:01 gordey4doronin

Any updates here? :)

stefanmeschke avatar Sep 10 '18 20:09 stefanmeschke

@ranma42 did you report this issue in swagger-api/swagger-parser?

naXa777 avatar Sep 12 '19 13:09 naXa777

I found it myself https://github.com/swagger-api/swagger-parser/issues/401

naXa777 avatar Sep 12 '19 13:09 naXa777

Hello 👋 any updates on this?

sammy-da avatar Mar 21 '22 16:03 sammy-da

Same issue

alois-git avatar Jun 02 '23 13:06 alois-git

Same issue

threydor avatar Jul 12 '24 09:07 threydor