Closed dwcahyo23 closed 2 months ago
Can you please test version 1.20.0? I might have accidentially introduced an old behavior with the latest release (because I solved this issue few months ago #31). But it would be good if you can test it as well.
I could confirm that I also have the same problem with v1.21.0 And after downgrading to v1.20.0 everything works fine again
Should now have been fixed with the latest release.
This unfortunately causes Cannot acccess 'YourType' before initialization
errors if building with SWC.
Here for typeorm they solve it using a wrapper: https://github.com/swc-project/swc/issues/5047#issuecomment-1188874962
Which is also what nestjs's recipe recommends under common pitfalls: https://docs.nestjs.com/recipes/swc
I am aware that this is maybe a pretty niche situation.
So, this is basically an issue with SWC, but even NestJs recommends a workaround?
So all relations and composite types would need to be wrapped with export type WrapperType<T> = T;
as it says at the NestJs docs (https://docs.nestjs.com/recipes/swc#common-pitfalls)? I'm not sure when I have time for this the next days.
I'm messing with this myself currently see if I can do another PR. However the suggested workaround breaks swagger schemas for the wrapped types, the errors are gone but for the referred type its just empty in the schema, I myself find this undesirable, so I'm continuing my search for a solution. SWC is so much faster I don't really want to leave it behind.
Actually excluding the type property in the @ApiProperty
while having the swagger plugin setup like this: https://docs.nestjs.com/openapi/cli-plugin fixes both the error and still displays it regularly in swagger.
Would you accept a PR with a setting for the generator nestjsDto
function in the .prisma file that is called outputTypeToApiProperty
that defaults to true, so I can set it to false?
I'm not sure if I got it right. I thought it's basically the type definition of the property?
export class User {
@ApiProperty({
type: () => Profile, // <--- you say it's okay to omit this line
})
profile: WrapperType<Profile>; // <--- docs say it needs the wrapper type here
}
Maybe you can share some example of what works for you and what not.
Yes, the docs say it needs the wrapper type there indeed, however I've not had any issues with circular dependencies + SWC while using normal types there. I assume that's because this is a newly defined property, where as @ApiProperty
's type requires a (property) type?: string | Function | Record<string, any> | Type<unknown> | [Function] | undefined
. That said, this depth is kind of out of my league, I just know what works for me and what doesn't.
When I tried adding the WrapperType to the type
property in @ApiProperty
because I assumed the same documented fix would work here too, it did fix the circular dependency issue with SWC, however it broke swagger's schema. So like I said previously this is undesirable to me so I continued looking for different solutions.
What I found out is, that it's okay to omit the type
property in @ApiProperty
IF you have the swagger cli-plugin setup (https://docs.nestjs.com/openapi/cli-plugin). This plugin will "set the type
or enum
property depending on the type (supports arrays as well)" apparently in a manner where SWC has no issue with it and swagger can still derive the actual schema from it.
Meaning that if we add an option that omits the type
property in @ApiProperty
and lean on the cli-plugin it works with swagger and with SWC without having to change much.
Please let me know if I can clarify anything else and thank you for your patience.
Sorry for the late response. If you are willing to do so, you can do a PR as you proposed with that flag. Maybe called outputApiPropertyType
?
I too am running into this issue, where I am using swc and [this commit] is causing the Cannot acccess 'YourType' before initialization
. Being able to omit the type
property would fix this issue for me as well.
I made a PR which you could use until it's merged.
I just released a new version with the changes of @KoenLemmen
Should now have been fixed with the latest release.
When omitting the type
property in @ApiProperty
, the type is not always inferred correctly by the swagger plugin (#49). Hence, I added a new setting that kind of wraps the imports as types, which should solve the circular dependency issue with SWC (see discussion above and https://docs.nestjs.com/recipes/swc#common-pitfalls).
Can you please test out 1.24.0-beta0 and check if it solves the issue?
Using following configs:
outputApiPropertyType = "true"
wrapRelationsAsType = "true"
If it works, outputApiPropertyType
should be obsolete.
Works for me.
@Brakebein i have issue, i dont know how to solve it.
v :
"@brakebein/prisma-generator-nestjs-dto": "^1.21.0",
nest:"@nestjs/common": "^10.3.4",
this my schema