-
Notifications
You must be signed in to change notification settings - Fork 55
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
feat: add endpoint detection #2938
Open
tepi
wants to merge
6
commits into
main
Choose a base branch
from
feat/endpoint-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ce5740
feat: add endpoint detection
tepi f760f89
internal marker and test
mshabarov e032017
fix assert messages
mshabarov 5e2e68c
fix exising tests and add a new test for endpoint usage detector
mshabarov 347cda5
Merge branch 'main' into feat/endpoint-detection
mshabarov 5d7b7a5
Merge branch 'main' into feat/endpoint-detection
mshabarov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/java/endpoint/src/main/java/com/vaadin/hilla/InternalBrowserCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.vaadin.hilla; | ||
|
||
import com.vaadin.flow.server.frontend.EndpointUsageDetector; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marker interface to be used by {@link EndpointUsageDetector} for detecting | ||
* framework's internal {@link BrowserCallable} and {@link Endpoint} endpoints | ||
* that shouldn't start the endpoint generator per se. | ||
* <p> | ||
* For internal use only. May be renamed or removed in a future release. | ||
* | ||
* @author Vaadin Ltd | ||
* @since 24.7 | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface InternalBrowserCallable { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/java/endpoint/src/main/java/com/vaadin/hilla/startup/EndpointUsageDetectorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.vaadin.hilla.startup; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import com.vaadin.flow.server.frontend.EndpointUsageDetector; | ||
import com.vaadin.flow.server.frontend.Options; | ||
import com.vaadin.flow.server.frontend.scanner.ClassFinder; | ||
import com.vaadin.hilla.BrowserCallable; | ||
import com.vaadin.hilla.Endpoint; | ||
import com.vaadin.hilla.InternalBrowserCallable; | ||
|
||
/** | ||
* Implementation of EndpointUsageDetector which determines if the endpoint | ||
* generator task needs to be run. | ||
*/ | ||
public class EndpointUsageDetectorImpl implements EndpointUsageDetector { | ||
|
||
@Override | ||
public boolean areEndpointsUsed(Options options) { | ||
ClassFinder classFinder = options.getClassFinder(); | ||
|
||
return Stream.concat( | ||
classFinder.getAnnotatedClasses(Endpoint.class).stream(), | ||
classFinder.getAnnotatedClasses(BrowserCallable.class).stream()) | ||
.anyMatch(annotatedClass -> !annotatedClass | ||
.isAnnotationPresent(InternalBrowserCallable.class)); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...rc/main/resources/META-INF/services/com.vaadin.flow.server.frontend.EndpointUsageDetector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.vaadin.hilla.startup.EndpointUsageDetectorImpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/java/hilla/src/test/java/com/vaadin/hilla/InternalBrowserCallableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.vaadin.hilla; | ||
|
||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.reflections.Reflections; | ||
import org.reflections.scanners.Scanners; | ||
|
||
/** | ||
* Validates that all endpoint classes in sources are annotated with | ||
* {@link InternalBrowserCallable} to not trigger endpoint generator if | ||
* endpoints are not actually used. | ||
*/ | ||
public class InternalBrowserCallableTest { | ||
|
||
private static final String BASE_PACKAGE = "com.vaadin"; | ||
|
||
@Test | ||
public void testBrowserCallableAndEndpointAnnotationsMarkedAsInternal() { | ||
Reflections reflections = new Reflections(BASE_PACKAGE, | ||
Scanners.TypesAnnotated); | ||
Set<Class<?>> annotatedClasses = reflections | ||
.getTypesAnnotatedWith(BrowserCallable.class); | ||
Set<Class<?>> endpointClasses = reflections | ||
.getTypesAnnotatedWith(Endpoint.class); | ||
annotatedClasses.addAll(endpointClasses); | ||
|
||
for (Class<?> clazz : annotatedClasses) { | ||
if (!clazz.getName().contains(".test.") && !clazz | ||
.isAnnotationPresent(InternalBrowserCallable.class)) { | ||
Assertions.fail("Class " + clazz.getName() | ||
+ " is annotated with @BrowserCallable or @Endpoint, but missing @InternalBrowserCallable."); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testInternalBrowserCallableHasBrowserCallableOrEndpoint() | ||
throws Exception { | ||
Reflections reflections = new Reflections(BASE_PACKAGE, | ||
Scanners.TypesAnnotated); | ||
Set<Class<?>> annotatedClasses = reflections | ||
.getTypesAnnotatedWith(InternalBrowserCallable.class); | ||
|
||
for (Class<?> clazz : annotatedClasses) { | ||
if (!clazz.isAnnotationPresent(BrowserCallable.class) | ||
&& !clazz.isAnnotationPresent(Endpoint.class)) { | ||
Assertions.fail("Class " + clazz.getName() | ||
+ " is annotated with @InternalBrowserCallable and must be annotated with @BrowserCallable or @Endpoint."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.vaadin</groupId> | ||
<artifactId>tests-spring</artifactId> | ||
<version>24.6-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>tests-spring-no-endpoints</artifactId> | ||
<name>ITs for No Endpoints cases</name> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.deploy.skip>true</maven.deploy.skip> | ||
<formatter.basedir>${project.parent.parent.parent.basedir}</formatter.basedir> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.vaadin</groupId> | ||
<artifactId>hilla</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>com.vaadin</groupId> | ||
<artifactId>hilla-maven-plugin</artifactId> | ||
<configuration> | ||
<parser> | ||
<packages> | ||
<package>com.example.application</package> | ||
</packages> | ||
</parser> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<!-- The test here assumes the build creates frontend files --> | ||
<goal>build-frontend</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<includeSystemScope>true</includeSystemScope> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
16 changes: 16 additions & 0 deletions
16
...ges/java/tests/spring/no-endpoints/src/main/java/com/example/application/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.example.application; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* The entry point of the Spring Boot application. | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...ng/no-endpoints/src/main/java/com/example/application/InternalBrowserCallableExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.application; | ||
|
||
import com.vaadin.hilla.Endpoint; | ||
import com.vaadin.hilla.InternalBrowserCallable; | ||
|
||
@Endpoint | ||
@InternalBrowserCallable | ||
public class InternalBrowserCallableExample { | ||
public String exampleMethod() { | ||
return null; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ts/spring/no-endpoints/src/main/java/com/example/application/InternalEndpointExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.application; | ||
|
||
import com.vaadin.hilla.Endpoint; | ||
import com.vaadin.hilla.InternalBrowserCallable; | ||
|
||
@Endpoint | ||
@InternalBrowserCallable | ||
public class InternalEndpointExample { | ||
public String exampleMethod() { | ||
return null; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/java/tests/spring/no-endpoints/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
server.port=8888 | ||
vaadin.devmode.liveReload.enabled=false | ||
logging.level.org.atmosphere = warn |
26 changes: 26 additions & 0 deletions
26
...a/tests/spring/no-endpoints/src/test/java/com/example/application/InternalEndpointIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.application; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class InternalEndpointIT { | ||
Path frontendDir = Paths.get("frontend", "generated"); | ||
|
||
@Test | ||
public void shouldNotRunEndpointGeneratorForOnlyInternalEndpointsPresent() { | ||
assertFalse(Files.exists(frontendDir.resolve("InternalBrowserCallableExample.ts"))); | ||
assertFalse(Files.exists(frontendDir.resolve("InternalEndpointExample.ts"))); | ||
assertFalse(Files.exists(frontendDir | ||
.resolve("com/example/application/InternalBrowserCallableExample.ts"))); | ||
assertFalse(Files.exists(frontendDir | ||
.resolve("com/example/application/InternalEndpointExample.ts"))); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Searching in the classpath for annotated classes does not necessarily match the reality of the endpoints in a Hilla application.
Until 24.6, the list of packages where to search for endpoints is configurable and, if not configured, defaults to search inside
target/classes
for backwards compatibility.Starting from 24.7, endpoints must be Spring beans (which was the case anyway 99% of the times).
So, in both cases the presence of an annotated class does not necessarily mean that an endpoint will be compiled to TypeScript and used in communication with clients.
We have a registry of endpoints, but of course that comes probably too late for your needs.
Also, we should define a common way to skip internal endpoints, to use also in
hilla/packages/java/endpoint/src/main/java/com/vaadin/hilla/EndpointController.java
Lines 134 to 137 in 0638cb8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record, this is where we define the list of endpoints using Spring
ApplicationContext
:hilla/packages/java/endpoint/src/main/java/com/vaadin/hilla/EndpointCodeGenerator.java
Lines 114 to 125 in efc7da0