Skip to content

Fixed NPE Caused By HTTP Methods that would resemble Accessors #83

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 java2typescript-jackson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>jackson-annotations</artifactId>
<version>${jackson.core.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
import java2typescript.jackson.module.grammar.ArrayType;
import java2typescript.jackson.module.grammar.base.AbstractType;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;

public class Configuration {
private Map<String, AbstractType> customTypes = Collections.emptyMap();
private List<String> ignoredMethodNames = new ArrayList<String>();
private TSTypeNamingStrategy namingStrategy = new SimpleJacksonTSTypeNamingStrategy();
private boolean jaxrsRun = false;

public Map<String, AbstractType> getCustomTypes() {
return customTypes;
Expand Down Expand Up @@ -61,4 +67,22 @@ public TSTypeNamingStrategy getNamingStrategy() {
public void setNamingStrategy(TSTypeNamingStrategy namingStrategy) {
this.namingStrategy = namingStrategy;
}

public boolean getJaxrsRun() {
return jaxrsRun;
}

public void setJaxrsRun(boolean bool) {
jaxrsRun = bool;
}

public boolean methodHasHTTPAnnotation(Method method) {
if (method.getAnnotation(GET.class) != null ||
method.getAnnotation(POST.class) != null ||
method.getAnnotation(PUT.class) != null ||
method.getAnnotation(DELETE.class) != null) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import java.beans.PropertyDescriptor;
import java.beans.Transient;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import java2typescript.jackson.module.grammar.AnyType;
import java2typescript.jackson.module.grammar.FunctionType;
import java2typescript.jackson.module.grammar.ClassType;
Expand All @@ -35,7 +39,6 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
Expand All @@ -51,6 +54,7 @@
public class TSJsonObjectFormatVisitor extends ABaseTSJsonFormatVisitor<ClassType> implements JsonObjectFormatVisitor {

private Class clazz;
private List<String> blackListField = new ArrayList<String>();

public TSJsonObjectFormatVisitor(ABaseTSJsonFormatVisitor<?> parentHolder, String className, Class clazz, Configuration conf) {
super(parentHolder, conf);
Expand All @@ -59,7 +63,9 @@ public TSJsonObjectFormatVisitor(ABaseTSJsonFormatVisitor<?> parentHolder, Strin
}

private void addField(String name, AbstractType fieldType) {
type.getFields().put(name, fieldType);
if (!blackListField.contains(name)) {
type.getFields().put(name, fieldType);
}
}

private boolean isAccessorMethod(Method method, BeanInfo beanInfo) {
Expand All @@ -74,6 +80,24 @@ private boolean isAccessorMethod(Method method, BeanInfo beanInfo) {
return false;
}

private void blackListUnnecessaryFieldMethods(Method method) {
Pattern getSearcher = Pattern.compile("^get.*");
Pattern setSearcher = Pattern.compile("^set.*");

String methodName = method.getName();
String ignoredField;

if (getSearcher.matcher(method.getName()).matches()) {
ignoredField = methodName.replaceFirst("^get","");
ignoredField = Introspector.decapitalize(ignoredField);
blackListField.add(ignoredField);
} else if (setSearcher.matcher(method.getName()).matches()) {
ignoredField = methodName.replaceFirst("^set","");
ignoredField = Introspector.decapitalize(ignoredField);
blackListField.add(ignoredField);
}
}

void addPublicMethods() {

for (Method method : this.clazz.getDeclaredMethods()) {
Expand All @@ -83,6 +107,13 @@ void addPublicMethods() {
continue;
}

//Don't exclude accessors with HTTP Annotations
if (conf.getJaxrsRun() && conf.methodHasHTTPAnnotation(method)) {
addMethod(method);
blackListUnnecessaryFieldMethods(method);
continue;
}

// Exclude accessors
try {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import java2typescript.jackson.module.Configuration;
import java2typescript.jackson.module.DefinitionGenerator;
import java2typescript.jackson.module.grammar.AnyType;
import java2typescript.jackson.module.grammar.ClassType;
Expand Down Expand Up @@ -85,14 +87,21 @@ public class ServiceDescriptorGenerator {

private ObjectMapper mapper;

private Configuration conf;

public ServiceDescriptorGenerator(Collection<? extends Class<?>> classes) {
this(classes, new ObjectMapper());
}

public ServiceDescriptorGenerator(Collection<? extends Class<?>> classes, ObjectMapper mapper) {
this(classes,mapper, null);
}

public ServiceDescriptorGenerator(Collection<? extends Class<?>> classes, ObjectMapper mapper, Configuration conf) {
this.classes = classes;
this.mapper = mapper;
addDummyMappingForJAXRSClasses();
this.conf = conf;
}

private class DummySerializer extends JsonSerializer<Object> {
Expand Down Expand Up @@ -153,7 +162,11 @@ public Module generateTypeScript(String moduleName) throws JsonMappingException

// Generates Typescript module out of service classses definition
DefinitionGenerator defGen = new DefinitionGenerator(mapper);
Module module = defGen.generateTypeScript(moduleName, classes, null);
if (conf == null) {
conf = new Configuration();
conf.setJaxrsRun(true);
}
Module module = defGen.generateTypeScript(moduleName, classes, conf);

// For each rest service, update methods with parameter names, got from Rest service descriptor
for (RestService restService : generateRestServices(classes)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,39 @@ static class MyObject {
}

@Path("/")
static private interface ExampleService {
class ExampleService {

@Path("/{id}")
@POST
public String aPostMethod(//
@QueryParam("q1") String queryParam, //
@PathParam("id") String id, //
@FormParam("formParam") Integer formParam, //
String postPayload);
String postPayload){
return "test";
}

@Path("/{id}")
@GET
public void aGetMethod(//
@QueryParam("q1") String queryParam, //
@PathParam("id") String id, //
@FormParam("formParam") Integer formParam, //
MyObject postPayload);
MyObject postPayload){

}

@Path("/random")
@GET
public int getRandom() {
return 4;
}

@Path("/multi")
@GET
public int getMultiWordGetter() {
return 3;
}

}

Expand Down Expand Up @@ -92,4 +108,19 @@ public void testTypescriptGenerate() throws JsonGenerationException, JsonMapping
Module tsModule = descGen.generateTypeScript("modName");
tsModule.write(out);
}

@Test
public void testTypescriptGenerateWithExample() throws JsonGenerationException, JsonMappingException, IOException {

ServiceDescriptorGenerator descGen = new ServiceDescriptorGenerator(
Collections.singletonList(ExampleService.class));

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("custom-mapping");

mapper.registerModule(module);

Module tsModule = descGen.generateTypeScript("modName");
tsModule.write(out);
}
}