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

ModelObject in generated typescipt code

Open ghost opened this issue 6 years ago • 14 comments

Using swagger-codegen 3.0.22 all my models containing an object property are now generated as a "ModelObject" referenced by

import { ModelObject } from './modelObject';

that doesn't even exist. I've seen some old issues regrading this, but it should have been fixed already?

IEnumValue:
  type: object
  properties:
    id:
      nullable: true
    name:
      type: string
      nullable: true
    description:
      type: string
      nullable: true
    isDefault:
      type: boolean
  additionalProperties: false
import { ModelObject } from './modelObject';

export interface IEnumValue { 
    id?: ModelObject;
    name?: string;
    description?: string;
    isDefault?: boolean;
}

ghost avatar Oct 07 '20 13:10 ghost

Hi Swagger Team,

I have same issue on my project in .Net Core 3.1 back end and Angular 9 front end. Yesterday, I migrated the all Swagger libraries from 5.5.1 version to 5.6.1 and the issue appears.

project.csproj -> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.1" /> <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.6.1" /> <PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.6.1" />

So, I upgraded to last version (5.6.3) but the issue still persists.

Here is what I found by comparing the generated Swagger.json file:

For 5.5.1 -> Property "parameters", in C# with 'object' type

"parameters": { "type": "object", "additionalProperties": false, "nullable": true },

Same property 'parameters' with Swagger.json but on 5.6.3 version

"parameters": { "nullable": true },

properties 'type' and 'additionalProperties' are not generated.

I also use swagger codegen for typescript 3.0.22.

Hope this comparaison help you. Tks for help!

Alpin-io-inc avatar Oct 08 '20 08:10 Alpin-io-inc

Hi Swagger Team,

Any news about issue ?

Tks for your help!

Alpin-io-inc avatar Oct 19 '20 08:10 Alpin-io-inc

Guess one of us needs to check code and implement it - rock paper scissors?

ghost avatar Oct 29 '20 14:10 ghost

Any news on this?

angelaki avatar Mar 09 '21 19:03 angelaki

For anyone using nestjs-swagger, this might be useful. I noticed that it occurs when the class contains a static method. I previously had the static methods ScheduleDateHelper.fromTimeStamp, ScheduleDateHelper.fromObject and ScheduleDateHelper.fromTimeString inside the ScheduleDate class. As a result the generated typescript was adding the line import { ModelObject } from './modelObject';

With the static methods separated into a helper class, I'm able to avoid seeing the line with ModelObject

// the typescript decorators are from nestjs-swagger
export class ScheduleDate {
    @ApiProperty({ type: "integer" })
    year: number;
    @ApiProperty({ type: "integer", minimum: 0, maximum: 11 })
    month: number;
    @ApiProperty({ type: "integer", minimum: 1, maximum: 31 })
    date: number;
    @ApiProperty({ type: "integer", minimum: 0, maximum: 23 })
    hour: number;
    @ApiProperty({ type: "integer", minimum: 0, maximum: 59 })
    minute: number;
    @ApiProperty({ type: "integer", minimum: 0, maximum: 59 })
    second: number;
}

/** This class has to be separate from ScheduleDate for OpenAPI Generation to work correctly */
export class ScheduleDateHelper {
    static fromTimeStamp(timestamp: number): ScheduleDate {
        const date = moment(timestamp);
        return ScheduleDateHelper.fromObject({
            year: date.year(),
            month: date.month(),
            date: date.date(),
            hour: date.hour(),
            minute: date.minute(),
            second: date.second(),
        });
    }

    static fromObject(value: any): ScheduleDate {
        let date = new ScheduleDate();
        ["year", "month", "date", "hour", "minute", "second"].forEach(key => date[key] = value[key]);
        return date;
    }

    static fromTimeString(timestring: string): ScheduleDate {
        const date = moment(timestring);
        if (!date.isValid()) { throw new Error("Invalid Date") }
        return ScheduleDateHelper.fromObject({
            year: date.year(),
            month: date.month(),
            date: date.date(),
            hour: date.hour(),
            minute: date.minute(),
            second: date.second(),
        });
    }
}

sccheruku avatar Apr 14 '21 14:04 sccheruku

Is anyone aware of this? The solutions seams so pretty easy: assume no type as any. Why isn't this going to be fixed?

angelaki avatar Apr 16 '21 10:04 angelaki

dose swagger team still maintain this project 🥲

den19980107 avatar Jun 11 '21 02:06 den19980107

I'm also experiencing this problem - any news?

It was working with Swashbuckle 5.5.0 but we are seeing the same issue with the swagger definition generated from Swashbuckle 6.1.4.

JonasSL avatar Jun 18 '21 06:06 JonasSL

Same issue on using swagger-codegen -l typescript-fetch. Using version 3.0.32 it generates the type ModelObject for many - I guess currently all - of my models. Running tests/build fails, since the type ModelObject does not exist.

Dirty fix

Manually add:

type ModelObject = any

to your generated api.ts.

That will not help in the long term if your goal is to auto-generate the client in pipelines or in general, in an automated way.

Any progress on this? I mean the last fix comment was at version 3.0.27? Thats already some versions and months that passed ...

WaldemarEnns avatar Jan 25 '22 21:01 WaldemarEnns

Is this even being maintained? Do the maintainers notice activity and requests on this open issue? :) Would be nice if we get some kind of feedback.

WaldemarEnns avatar Feb 22 '22 20:02 WaldemarEnns

@WaldemarEnns going with your workaround. 🚀

We do have auto-generated types in our CI pipeline, so gonna change the script to literally write this to the generated types. Seems there won't be a fix anytime soon.

StanleySathler avatar Jun 24 '22 15:06 StanleySathler

We are experiencing this same issue. Is this being addressed, or should we abandon SwaggerHub on our project?

auzick avatar Jul 06 '22 17:07 auzick

fixed in swagger-api/swagger-codegen-generators/pull/1051

frantuma avatar Jul 21 '22 14:07 frantuma

@frantuma nice! That was surprising - will check it out immediately!

WaldemarEnns avatar Jul 21 '22 14:07 WaldemarEnns