vojtechhabarta / typescript-generator

Generates TypeScript from Java - JSON declarations, REST service client
MIT License
1.14k stars 237 forks source link

Spring contoller method arguments are not processed with custom TypeProcessor #746

Open porunov opened 2 years ago

porunov commented 2 years ago

I have a controller method like:

@RequestMapping(value = "/hello/{id}/world", method = RequestMethod.GET)
public ResponseEntity<GetResponse> getMethod(@PathVariable String id, @RequestParam(required = false) Set<String> queryParameter) {
    return null;
}

I also have a custom type resolver:

public class CustomTypeProcessor implements TypeProcessor {
    @Override
    public Result processType(Type javaType, Context context) {
        if(context.getTypeContext() != null){
            if(javaType.getTypeName().startsWith("java.util.Set")){
                System.out.println("Hello World");
            }
        }
        return null;
    }
}

I'm using Gradle build which looks like:

apply plugin: 'cz.habarta.typescript-generator'
apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile "cz.habarta.typescript-generator:typescript-generator-core:2.33.956"
    compile "cz.habarta.typescript-generator:typescript-generator-spring:2.33.956"
}

buildscript {

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath ("cz.habarta.typescript-generator:typescript-generator-gradle-plugin:2.33.956")
        classpath ("cz.habarta.typescript-generator:typescript-generator-spring:2.33.956")
    }

}

generateTypeScript {
    jsonLibrary = 'jackson2'
    classPatterns = [
            'path.to.dto.**DTO',
            'path.to.controllers.**'
    ]
    outputFile = 'build/output.ts'
    outputKind = 'global'
    outputFileType = 'implementationFile'
    mapClasses = 'asClasses'
    mapEnum = 'asEnum'
    nonConstEnums = true
    optionalProperties = 'useLibraryDefinition'
    scanSpringApplication = true
    generateSpringApplicationClient = true
    generateSpringApplicationInterface = true
    customTypeProcessor = 'path.to.CustomTypeProcessor'
}

I noticed that CustomTypeProcessor is triggered for all DTO fields but it's not triggered for any method arguments. It would be really great if we could use custom processors to process method arguments as well.

porunov commented 2 years ago

With #788 it's possible to add logic which replaces / changes argument types of necessary arguments before typescript code is generated. So, it could be used as a workaround for this issue.