Swagger auto generated file issue with _observableOf(null) in angular 12?
Asked Answered
C

2

5

So i'm using swagger and Nswag for a very long time, I can see the auto generated methods used to generate a line like this to exit as default value:

return _observableOf<MyClassData>(<any>null);

...And now with the same nswag json file, it generates this way:

return _observableOf(null);

Which seems to be valid for Angular 9 because it did not support strict=true, but now with my Angular 11/12 projects, this throws an error that says:

error TS2322: Type 'Observable' is not assignable to type 'Observable'. Type 'null' is not assignable to type 'MyClassData'.

Solution 1) Manually replace the auto generated file by adding: return _observableOf < MyClassData >(< any > null); which is crazy because everytime I generate the file, I will lose the changes.

Solution 2) Open/edit the angular.json, in the schematics/angular:application, strict: change it from true to false, of course it stops failing, but I would very prefeer to keep the code as strict.

What could be the best solution here? Is there a way to specify to NSWAG that should keep returning as it was doing it in the past?

Thanks.

Carmencita answered 1/8, 2021 at 19:9 Comment(1)
Nswag is a library right? If so, you should open an issue on their GitHub to ask for proper compatibility with strict mode of Typescript.Etruria
C
5

I had a similar issue with my project. I upgraded most of my packages, including Angular to v12 and rxjs to v7.

The nswag-generated file also gave me a compile error saying the _observableOf was depricated and that:

Type 'Observable<object>' is not assignable to type 'Observable<object[]>'

for all endpoints that returned arrays.

I think this is because nswag is not compatible with rxjs 7+ (yet). It worked for me to downgrade the rxjs to 6.6.7.

Calcutta answered 14/9, 2021 at 8:40 Comment(0)
M
1

It seems it is solved for RXJS Verison 7. After updating ngswag version my problem like yours was solved. Also, I changed my rxjs and typescript versions inside .nswag file. "rxJsVersion": 7.5, and "typeScriptVersion": 4.7

Issue Solved Here https://github.com/RicoSuter/NSwag/issues/3463

Mccready answered 25/10, 2022 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.