Skip to content

Commit

Permalink
Negated a potential NPE that was caused be HTTP methods that had a
Browse files Browse the repository at this point in the history
similar structure to accessor methods.
  • Loading branch information
wwhitlow committed Feb 22, 2018
1 parent c533cb1 commit 225024e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
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 @@ -24,13 +24,22 @@
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;
import java2typescript.jackson.module.grammar.TypeDeclarationType;
import java2typescript.jackson.module.grammar.VoidType;
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;

import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand All @@ -51,6 +60,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 +69,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 +86,21 @@ 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 ignoredField = method.getName().toLowerCase();

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

void addPublicMethods() {

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

//Don't exclude accessors
if (method.getAnnotation(GET.class) != null) {
addMethod(method);
blackListUnnecessaryFieldMethods(method);
continue;
}
if (method.getAnnotation(POST.class) != null) {
addMethod(method);
blackListUnnecessaryFieldMethods(method);
continue;
}
if (method.getAnnotation(PUT.class) != null) {
addMethod(method);
blackListUnnecessaryFieldMethods(method);
continue;
}
if (method.getAnnotation(DELETE.class) != null) {
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 @@ -46,23 +46,33 @@ 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;
}

}

Expand Down Expand Up @@ -92,4 +102,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);
}
}

0 comments on commit 225024e

Please sign in to comment.