Skip to content

Commit 767c815

Browse files
committed
APT NPE with empty controllers
- check if super-types is present, it won't be present on empty-controllers - fix #3656 Stacktrace: ``` Caused by: java.lang.NullPointerException: Cannot read field "context" because "parent" is null at io.jooby.internal.apt.MvcRouter.<init> (MvcRouter.java:39) at io.jooby.apt.JoobyProcessor.lambda$buildRouteRegistry$11 (JoobyProcessor.java:296) at java.util.HashMap.computeIfAbsent (HashMap.java:1220) at io.jooby.apt.JoobyProcessor.buildRouteRegistry (JoobyProcessor.java:296) at io.jooby.apt.JoobyProcessor.buildRouteRegistry (JoobyProcessor.java:219) at io.jooby.apt.JoobyProcessor.process (JoobyProcessor.java:109) ```
1 parent 9c7517b commit 767c815

File tree

6 files changed

+53
-8
lines changed

6 files changed

+53
-8
lines changed

modules/jooby-apt/src/main/java/io/jooby/apt/JoobyProcessor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,13 @@ private void buildRouteRegistry(Map<TypeElement, MvcRouter> registry, TypeElemen
292292
});
293293
} else {
294294
if (!currentType.equals(superType)) {
295-
// edge case when controller has no method and extends another class which has.
296-
registry.computeIfAbsent(currentType, key -> new MvcRouter(key, registry.get(superType)));
295+
// edge-case #1: when controller has no method and extends another class which has.
296+
// edge-case #2: some odd usage a controller could be empty.
297+
// See https://github.com/jooby-project/jooby/issues/3656
298+
if (registry.containsKey(superType)) {
299+
registry.computeIfAbsent(
300+
currentType, key -> new MvcRouter(key, registry.get(superType)));
301+
}
297302
}
298303
}
299304
}

modules/jooby-apt/src/test/java/io/jooby/apt/ProcessorRunner.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import java.nio.file.Files;
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
15-
import java.util.ArrayList;
16-
import java.util.List;
17-
import java.util.Map;
18-
import java.util.Objects;
15+
import java.util.*;
1916
import java.util.function.Consumer;
2017
import java.util.stream.Stream;
2118

@@ -147,7 +144,10 @@ public ProcessorRunner withSourceCode(SneakyThrows.Consumer<String> consumer) {
147144
}
148145

149146
public ProcessorRunner withSourceCode(boolean kt, SneakyThrows.Consumer<String> consumer) {
150-
consumer.accept(kt ? processor.kotlinSource : processor.getSource().toString());
147+
consumer.accept(
148+
kt
149+
? processor.kotlinSource
150+
: Optional.ofNullable(processor.getSource()).map(Objects::toString).orElse(null));
151151
return this;
152152
}
153153

modules/jooby-apt/src/test/java/tests/i3539/Issue3539.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void shouldGenerateAnnotationWithDefaultValue() throws Exception {
2929
assertNotNull(attributes);
3030
assertFalse(attributes.isEmpty());
3131
var secured = attributes.get(Secured3525.class.getSimpleName());
32-
assertSame(secured, Boolean.TRUE);
32+
assertSame(Boolean.TRUE, secured);
3333
});
3434
}
3535
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3656;
7+
8+
import io.jooby.annotation.Path;
9+
10+
@Path("/b3656")
11+
public class B3656 {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3656;
7+
8+
import io.jooby.annotation.Path;
9+
10+
@Path("/c3656")
11+
public class C3656 extends B3656 {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package tests.i3656;
7+
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import io.jooby.apt.ProcessorRunner;
12+
13+
public class Issue3656 {
14+
@Test
15+
public void shouldNotThrowErrorOnEmptyController() throws Exception {
16+
new ProcessorRunner(new C3656()).withSourceCode(Assertions::assertNull);
17+
}
18+
}

0 commit comments

Comments
 (0)