[REQ] [Typescript] Choose method signature generation
Hello, i'd like to know if there is an option for generator or in the contract in order to choose the signature generated for each service ?
Context In my project we're using OpenApi generator in order to generate typescript client services (angular application). When using generated code in the app, all is fine, but we have some problems with unit testing with jasmine.
We're using OpenApi 3 with generator v4.3.1 (but same problem with 5.0.0-beta).
Issue i'd like to solve When the code is generated, there are 4 signatures for every service (same method name). The difference is the object returned that can be : Observable< MyClass >, Observable<HttpResponse< MyClass >>, Observable<HttpEvent< MyClass >>, and Observable< any >.
For exemple, in my contract i perform a GET with the oprationId myOperation with parameter myParam, the generated service is :
public myOperation(myParam: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json'}): Observable<MyClass>;
public myOperation(myParam: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json'}): Observable<HttpResponse<MyClass>>;
public myOperation(myParam: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json'}): Observable<HttpEvent<MyClass>>;
public myOperation(myParam: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json'}): Observable<any> {
...
In the our test, in order to test our internal services, we need mock the generated services. So, we use a Spy to mock myGeneratedService method. The problem is that the compiler doesn't know what is the right signature. In our code, we use le signature that returns Observable< MyClass > but the compiler is taking the signature with Observable<HttpResponse< MyClass> >.
Here is an exemple of test not working :
const myService = TestBed.inject(MyService);
const mockedValue: MyClass = new MyClass();
const spy = spyOn(myService, 'myOperation').and.returnValue(of(mockedValue));
With this code i get the following error :
Argument of type 'Observable<MyClass>' is not assignable to parameter of type 'Observable<HttpEvent<MyClass>>'.
Question Is there an option to choose what are the signature generated ? In our case, we'd like to generate only the method with Observable< MyClass >
If there is another way to avoid this issue, you're welcome.
Thanks.
can you provide some example of your unit test code?
I edited initial post with test exemple.
I guess in this case you need to disable type checking for the spy:
const spy = spyOn(myService, 'myOperation').and.returnValue(of(mockedValue) as any);
I'm coming across this issue now too. I know this is an old question, but wanted to check to see if there's a better solution at this point rather than just appending as any to the spy method setups.
I'm running in the same issue. Would be amazing if we could choose to only generate the Observable<MyClass> signature