OpenAPITools / OpenAPITools/openapi-generator

[REQ] [Typescript] Choose method signature generation

Open
#7,249 5 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test entry point is named. Start by reproducing the generated TypeScript Angular service overloads with the OpenAPI 3 example and the Jasmine spy, then inspect generator options and the TypeScript client generation path. Done means the requested signature selection is supported or its limitation and workaround are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, openapi, typescript
Domain
api, testing, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.