Skip to content

Allow parsing jakarta.rs applications #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions typescript-generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

package cz.habarta.typescript.generator;

import cz.habarta.typescript.generator.parser.SourceType;
import io.github.classgraph.ScanResult;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Application;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;


public class JakartaRsApplicationScanner {

public static List<SourceType<Type>> scanJakartaRsApplication(Class<?> jaxrsApplicationClass, Predicate<String> isClassNameExcluded) {
final ClassLoader originalContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(jaxrsApplicationClass.getClassLoader());
TypeScriptGenerator.getLogger().info("Scanning JAX-RS application: " + jaxrsApplicationClass.getName());
final Constructor<?> constructor = jaxrsApplicationClass.getDeclaredConstructor();
constructor.setAccessible(true);
final Application application = (Application) constructor.newInstance();
final List<Class<?>> resourceClasses = new ArrayList<>();
for (Class<?> cls : application.getClasses()) {
if (cls.isAnnotationPresent(Path.class)) {
resourceClasses.add(cls);
}
}
return new JakartaRsApplicationScanner().scanJakartaRsApplication(jaxrsApplicationClass, resourceClasses, isClassNameExcluded);
} catch (ReflectiveOperationException e) {
throw reportError(e);
} finally {
Thread.currentThread().setContextClassLoader(originalContextClassLoader);
}
}

public static List<SourceType<Type>> scanAutomaticJakartaRsApplication(ScanResult scanResult, Predicate<String> isClassNameExcluded) {
final List<String> namesOfResourceClasses = scanResult.getClassesWithAnnotation(Path.class.getName()).getNames();
final List<Class<?>> resourceClasses = Input.loadClasses(namesOfResourceClasses);
TypeScriptGenerator.getLogger().info(String.format("Found %d root resources.", resourceClasses.size()));
return new JakartaRsApplicationScanner().scanJakartaRsApplication(null, resourceClasses, isClassNameExcluded);
}

private static RuntimeException reportError(ReflectiveOperationException e) {
final String url = "https://github.com/vojtechhabarta/typescript-generator/wiki/JAX-RS-Application";
final String message = "Cannot load JAX-RS application. For more information see " + url + ".";
TypeScriptGenerator.getLogger().error(message);
return new RuntimeException(message, e);
}

List<SourceType<Type>> scanJakartaRsApplication(Class<?> applicationClass, List<Class<?>> resourceClasses, Predicate<String> isClassNameExcluded) {
Collections.sort(resourceClasses, new Comparator<Class<?>>() {
@Override
public int compare(Class<?> o1, Class<?> o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
});
final List<SourceType<Type>> sourceTypes = new ArrayList<>();
if (applicationClass != null) {
sourceTypes.add(new SourceType<Type>(applicationClass));
}
for (Class<?> resourceClass : resourceClasses) {
if (isClassNameExcluded == null || !isClassNameExcluded.test(resourceClass.getName())) {
sourceTypes.add(new SourceType<Type>(resourceClass));
}
}
return sourceTypes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cz.habarta.typescript.generator.compiler.SymbolTable.CustomTypeNamingFunction;
import cz.habarta.typescript.generator.emitter.EmitterExtension;
import cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures;
import cz.habarta.typescript.generator.parser.JakartaRsApplicationParser;
import cz.habarta.typescript.generator.parser.JaxrsApplicationParser;
import cz.habarta.typescript.generator.parser.RestApplicationParser;
import cz.habarta.typescript.generator.parser.TypeParser;
Expand Down Expand Up @@ -94,6 +95,8 @@ public class Settings {
public boolean disableTaggedUnions = false;
public boolean generateReadonlyAndWriteonlyJSDocTags = false;
public boolean ignoreSwaggerAnnotations = false;
public boolean generateJakartaRsApplicationInterface = false;
public boolean generateJakartaRsApplicationClient;
public boolean generateJaxrsApplicationInterface = false;
public boolean generateJaxrsApplicationClient = false;
public boolean generateSpringApplicationInterface = false;
Expand Down Expand Up @@ -408,6 +411,10 @@ public void validate() {
annotation.getName()));
}
}

if (generateJakartaRsApplicationClient && outputFileType != TypeScriptFileType.implementationFile) {
throw new RuntimeException("'generateJaxrsApplicationClient' can only be used when generating implementation file ('outputFileType' parameter is 'implementationFile').");
}
if (generateJaxrsApplicationClient && outputFileType != TypeScriptFileType.implementationFile) {
throw new RuntimeException("'generateJaxrsApplicationClient' can only be used when generating implementation file ('outputFileType' parameter is 'implementationFile').");
}
Expand Down Expand Up @@ -761,9 +768,13 @@ public void setRestOptionsType(String restOptionsType) {
public List<RestApplicationParser.Factory> getRestApplicationParserFactories() {
if (restApplicationParserFactories == null) {
final List<RestApplicationParser.Factory> factories = new ArrayList<>();
if (isGenerateJaxrs() || !isGenerateSpring()) {
if (isGenerateJaxrs() || (!isGenerateSpring() && !isGenerateJakartaRs())) {
factories.add(new JaxrsApplicationParser.Factory());
}
if (isGenerateJakartaRs() || (!isGenerateSpring() && !isGenerateJaxrs())) {
factories.add(new JakartaRsApplicationParser.Factory());
}

if (isGenerateSpring()) {
final String springClassName = "cz.habarta.typescript.generator.spring.SpringApplicationParser$Factory";
final Class<?> springClass;
Expand All @@ -786,6 +797,10 @@ public List<RestApplicationParser.Factory> getRestApplicationParserFactories() {
}
return restApplicationParserFactories;
}

public boolean isGenerateJakartaRs() {
return generateJakartaRsApplicationInterface || generateJakartaRsApplicationClient;
}

public boolean isGenerateJaxrs() {
return generateJaxrsApplicationInterface || generateJaxrsApplicationClient;
Expand All @@ -796,7 +811,7 @@ public boolean isGenerateSpring() {
}

public boolean isGenerateRest() {
return isGenerateJaxrs() || isGenerateSpring();
return isGenerateJakartaRs() || isGenerateJaxrs() || isGenerateSpring();
}

public boolean areDefaultStringEnumsOverriddenByExtension() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ private void createRestClients(TsModel tsModel, SymbolTable symbolTable, List<Re
Collections.<TsStatement>emptyList(),
null
);
final boolean bothInterfacesAndClients = settings.generateJaxrsApplicationInterface || settings.generateSpringApplicationInterface;
final boolean bothInterfacesAndClients = settings.generateJakartaRsApplicationInterface || settings.generateJaxrsApplicationInterface || settings.generateSpringApplicationInterface;
final String groupingSuffix = bothInterfacesAndClients ? null : "Client";
final Map<Symbol, List<TsMethodModel>> groupedMethods = processRestMethods(tsModel, restApplications, symbolTable, groupingSuffix, responseSymbol, optionsType, true);
for (Map.Entry<Symbol, List<TsMethodModel>> entry : groupedMethods.entrySet()) {
Expand Down
Loading