blue-veery-gmbh / spring-rest-2-ts

spring rest 2 ts is typescript generator which produces data model and services in typescript based on Spring MVC annotations. It supports generation for Angular and React
MIT License
64 stars 17 forks source link

No import statements generated for Enums when used as return types #36

Closed mpopp closed 2 years ago

mpopp commented 2 years ago

I have a small controller where I return something like ListResult from one of the endpoints. The enum is not used as a request parameter in any of the endpoints of this controller.

The generated ts api does not contain an import {enum} from "./enum-module" in the imports block. The return types are generated correctly though.

As soon as I add an endpoint that uses the enum as a bodyParameter, the import is done correctly. I have not verified if this happens with Java Classes as well. Right now I only run into this issue with Enums.

This happens independent of whether I use my own ImplementationGenerator, the FetchBased one or the Angular based one.

tomasz-wozniak75 commented 2 years ago

Hey Thanks for raising this I will try to reproduce and fix it soon. Tomasz

mpopp commented 2 years ago

Awesome. I have debugged into the code a little bit in the meantime and couldn't find any call to importMap.put for return types

tomasz-wozniak75 commented 2 years ago

import for return type is added here com/blueveery/springrest2ts/converters/SpringAnnotationsBasedRestClassConverter.java:90 TSEnum extends TSScopedElement ,when You look at this method: com/blueveery/springrest2ts/tsmodel/TSModule.java:171 here TSScopedElement element is added to importMap so I think it should work but maybe there an issue in the code which need to be investigated or based on mentioned method, enum is placed in systemModule

TSModule module = tsScopedElement.getModule(); if (module != this && module != TypeMapper.systemModule) { You can check if this enum module is not a systemModule, systemModule it is global scope from which we don't need to import things like JS Array type

tomasz-wozniak75 commented 2 years ago

hey In branch enums-import-test I created test which generates TS service based on the Java controller which has method where return type is enum, please check this test com.blueveery.springrest2ts.converters.SpringRestToTsConverterTest#endpointReturnTypeShouldBeImported

Import is generated, test prints to std out generated code which looks following:

`import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {Injectable} from '@angular/core'; import {ProductType} from './converters-enums'; import {Observable} from 'rxjs'; import {map} from 'rxjs/operators';

@Injectable() export class ProductController { httpService: HttpClient;

public constructor(httpService: HttpClient) { this.httpService = httpService; }

public getProductType(): Observable { return this.httpService.get('/product/{id}', {responseType: 'text'}).pipe(map(res => JSON.parse(res))); }

}` Please feel free to modify this test to replicate your case. You can also check in setUp method how tsGenerator is configured maybe the issue is in configuration

mpopp commented 2 years ago

Thank you, your test helped me figuring out the issue. It has to do with generics. I use SingleResult in my controller as a return type. Only SingleResult is imported, although the actual api interface is generated correctly.

I modified the test to reproduce the case.

tomasz-wozniak75 commented 2 years ago

ok, thanks I will check that over the weekend

tomasz-wozniak75 commented 2 years ago

this SingleResult is a spring type or it is your own custom type?

mpopp commented 2 years ago

I'm preparing the branch on a fork right now so you can check my setup. It is my own custom type.

tomasz-wozniak75 commented 2 years ago

ok thanks

mpopp commented 2 years ago

Here is the branch in my fork: https://github.com/mpopp/spring-rest-2-ts/tree/failing-enums-import

tomasz-wozniak75 commented 2 years ago

thanks Matthias, it was a good finding.

I've pushed fix to enums-import-test branch You can pull changes, build them locally and use in your setup version 1.5.1-SNAPSHOT if it solve your problem I will make release during the weekend

mpopp commented 2 years ago

Sorry for the late response. Just got around trying out your fix on the SNAPSHOT version you provided.

With the fix, the imports are generated as expected.

tomasz-wozniak75 commented 2 years ago

great, thanks version 1.5.1 is already in maven central repo https://search.maven.org/artifact/com.blue-veery/spring-rest2ts-generator/1.5.1/jar