ModelObject in generated typescipt code
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;
}
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!
Hi Swagger Team,
Any news about issue ?
Tks for your help!
Guess one of us needs to check code and implement it - rock paper scissors?
Any news on this?
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(),
});
}
}
Is anyone aware of this? The solutions seams so pretty easy: assume no type as any. Why isn't this going to be fixed?
dose swagger team still maintain this project 🥲
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.
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 ...
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 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.
We are experiencing this same issue. Is this being addressed, or should we abandon SwaggerHub on our project?
fixed in swagger-api/swagger-codegen-generators/pull/1051
@frantuma nice! That was surprising - will check it out immediately!