Skip to content

Commit d466b0d

Browse files
Fix #239, use simple class name as default name (#241)
1 parent ac82682 commit d466b0d

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

sdk-api-gen/src/main/java/dev/restate/sdk/gen/ElementConverter.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,9 @@ public Component fromTypeElement(TypeElement element) {
9090
? virtualObjectAnnotation.name()
9191
: workflowAnnotation.name();
9292
if (componentName.isEmpty()) {
93-
// Use FQCN
94-
// With this logic we make sure we flatten subclasses names
95-
String simpleComponentName =
96-
targetFqcn.toString().substring(targetPkg.length()).replaceAll(Pattern.quote("."), "");
93+
// Use simple class name, flattening subclasses names
9794
componentName =
98-
targetPkg.length() > 0 ? targetPkg + "." + simpleComponentName : simpleComponentName;
95+
targetFqcn.toString().substring(targetPkg.length()).replaceAll(Pattern.quote("."), "");
9996
}
10097

10198
// Compute handlers
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk;
10+
11+
import dev.restate.sdk.annotation.Handler;
12+
import dev.restate.sdk.annotation.Service;
13+
14+
@Service(name = "MyExplicitName")
15+
public interface GreeterWithExplicitName {
16+
@Handler
17+
String greet(Context context, String request);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk;
10+
11+
import dev.restate.sdk.annotation.Handler;
12+
import dev.restate.sdk.annotation.Service;
13+
14+
@Service
15+
public interface GreeterWithoutExplicitName {
16+
@Handler
17+
String greet(Context context, String request);
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk;
10+
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
public class NameInferenceTest {
16+
17+
@Test
18+
void expectedName() {
19+
assertThat(CodegenTestServiceGreeterClient.COMPONENT_NAME)
20+
.isEqualTo("CodegenTestServiceGreeter");
21+
assertThat(GreeterWithoutExplicitNameClient.COMPONENT_NAME)
22+
.isEqualTo("GreeterWithoutExplicitName");
23+
assertThat(MyExplicitNameClient.COMPONENT_NAME).isEqualTo("MyExplicitName");
24+
}
25+
}

sdk-http-vertx/src/test/kotlin/dev/restate/sdk/http/vertx/RestateHttpEndpointTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal class RestateHttpEndpointTest {
5656
@Timeout(value = 1, timeUnit = TimeUnit.SECONDS)
5757
@Test
5858
fun endpointWithBlockingService(vertx: Vertx): Unit =
59-
greetTest(vertx, BlockingGreeter::class.qualifiedName!!) { it.with(BlockingGreeter()) }
59+
greetTest(vertx, BlockingGreeter::class.simpleName!!) { it.with(BlockingGreeter()) }
6060

6161
private fun greetTest(
6262
vertx: Vertx,
@@ -169,7 +169,7 @@ internal class RestateHttpEndpointTest {
169169
HttpMethod.POST,
170170
endpointPort,
171171
"localhost",
172-
"/invoke/" + BlockingGreeter::class.java.canonicalName + "/unknownMethod")
172+
"/invoke/" + BlockingGreeter::class.java.simpleName + "/unknownMethod")
173173
.coAwait()
174174

175175
// Prepare request header
@@ -218,7 +218,7 @@ internal class RestateHttpEndpointTest {
218218

219219
assertThat(discoveryResponse.components)
220220
.map<String> { it.fullyQualifiedComponentName }
221-
.containsOnly(BlockingGreeter::class.java.canonicalName)
221+
.containsOnly(BlockingGreeter::class.java.simpleName)
222222
}
223223

224224
fun encode(msg: MessageLite): Buffer {

0 commit comments

Comments
 (0)