Swagger-codegen: [Typescript-Angular2] Array items exported as extending "models.Array"

Created on 19 Aug 2016  路  8Comments  路  Source: swagger-api/swagger-codegen

Description

If I define a model definition as just an array, then the created model ts file says "extends models.Array" instead of just "extends Array"

Swagger-codegen version 2.2.1
Swagger declaration file content or url
swagger: "2.0"

host: localhost
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /test:
    get:
      tags:
        - test
      operationId: test
      description: test
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/TestArray"

definitions:
  TestArray:
    type: array
    items:
      $ref: "#/definitions/TestItem"

  TestItem:
    properties:
      val:
        type: string

The invalid generated model for TestArray is then:

export interface TestArray extends models.Array<models.TestItem> {

}

whereas it should just be as follows (without "models."):

export interface TestArray extends Array<models.TestItem> {

}
Command line used for generation

java -jar swagger-codegen-cli-2.2.1.jar generate -i ./swagger.yaml -l typescript-angular2 --additional-properties modelPropertyNaming=original

Suggest a Fix

For the time-being, I have my codegen run within a script that uses sed to replace the values after generation:
sed -i '' 's/extends models\.Array/extends Array/g' *

TypeScript Workaround available

Most helpful comment

I could, but that seems like a work-around, when I believe the example provided is still valid swagger spec. And while the example is very simple, there are cases where I use an array model multiple times, so I would rather avoid code duplication. As I said though, the sed work-around works for now too.

All 8 comments

I could, but that seems like a work-around, when I believe the example provided is still valid swagger spec. And while the example is very simple, there are cases where I use an array model multiple times, so I would rather avoid code duplication. As I said though, the sed work-around works for now too.

I'm noticing that this may be related to another problem where I have simple models defined without 'properties'. The following two models are not generated (but are referenced in numerous places):

  UnixTime:
    type: integer
    description: UNIX time stamp (num seconds since Jan 1, 1970)

  BoolInt:
    type: integer
    description: DB storage of boolean
    minimum: 0
    maximum: 1

Confirm: model typed as array become interface with model prefix:

SomeArrayLikeDefinition:
  type: array
  items:
    $ref: '#/definitions/SomeDefinition'

produces

export interface SomeArrayLikeDefinition extends models.Array<models.SomeDefinition> {

}

Am I right that models.Array<models.SomeDefinition> should be
Array<models.SomeDefinition>?

4223 changes the imports and probably fixes this problem indirectly.

I had the same issue and worked around it by using a custom generator to replace the first line of modelGeneric.mustache from:

export interface {{classname}} {{#parent}}extends models.{{{parent}}} {{/parent}}{

to:

export interface {{classname}} {{#parent}}extends {{#parentModel}}models.{{/parentModel}}{{{parent}}} {{/parent}}{

I believe it should be explicitly fixed in 2.2.2 also. I can make a PR if needed.

Hi, I'm having trouble with this one too. How is the fix going?

Having the same problem as well, would definitely love a fix

Was this page helpful?
0 / 5 - 0 ratings