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

TSInterface without a name generates not correct Typescript output #39

Closed mateuszput closed 1 year ago

mateuszput commented 1 year ago

Not sure why, but I end up with with DTO in a typescript file looking like this:

export interface  {}

export interface SomeResponse {
  aaa: number | null;
  bbb: boolean;
}

I have noticed that there are 2 TSInterface objects in a TSModule object and one of them has an empty name (name = "").

Also in the empty object mappedFromJavaTypeSet field has a name equal package.subdir.SomeResponse$1. In the other one it is package.subdir.SomeResponse

SomeResponse$1.class is generated since there is inner class in the file.

I investigated it more and created a patch with a test to reproduce this bug. Please let me know how can I provide it or if you would like me to send you more info.

tomasz-wozniak75 commented 1 year ago

Hey If You are using Intelij, You can create a patch file which You can upload here. If You have a test which replicates a problem it should be enough. My first guess is that it is caused by name mapper, are you using default one NoChangeClassNameMapper or SubstringClassNameMapper with some configuration?

mateuszput commented 1 year ago

I use Lombok which generates code similar to the one from the included Intellij patch. After build the discussed entry is in a file mode-core.ts

In the original code I have also I also filters to exclude additional code from the generated TS like: new NotJavaTypeFilter(new ContainsSubStringJavaTypeFilter("Builder")), but it seems that it is not needed to replicate the problem.

Empty_names.zip

Thank you in advance!

tomasz-wozniak75 commented 1 year ago

Hey I had a quick look, problem is caused by unnamed class which is introduced by builders, we don't need them in TS so you need to filter out them. Selecting classes by package is too broad in such case, if your DTOs have common base class, runtime annotation or name pattern like "response", "Request" you can use that for filtering purposes, if not apply following filter

tsGenerator.setModelClassesCondition(new AndFilterOperator(
                    Arrays.asList(
                            new JavaTypePackageFilter(BaseDTO.class.getPackage()),
                            new NotJavaTypeFilter(
                                new OrFilterOperator(
                                        Arrays.asList(
                                            new ContainsSubStringJavaTypeFilter("Builder"),
                                            new JavaTypeFilter() {

                                                @Override
                                                public boolean accept(Class javaType) {
                                                    return "".equals(javaType.getSimpleName());
                                                }

                                                @Override
                                                public void explain(
                                                        Class packageClass, Logger logger,
                                                        String indentation
                                                ) {
                                                    System.out.println("skipping unnamed class");
                                                }
                                            }
                                )
                            )
                    )
            )
        )
    );

This filter combination skips builders and unnamed classes in desired package

mateuszput commented 1 year ago

The filter you've sent works like a charm, thank you very much for your help!