Skip to content

Commit f2893a7

Browse files
authored
Fix problems enabling request scope in the Webserver (helidon-io#11350)
Fix problems enabling request scope in the Webserver. Update codegen to add new contracts for raw types. Update declarative test to show request scope in action. Signed-off-by: Santiago Pericas-Geertsen <santiago.pericasgeertsen@oracle.com>
1 parent 7d81da7 commit f2893a7

10 files changed

Lines changed: 57 additions & 12 deletions

File tree

declarative/tests/http/src/main/java/io/helidon/declarative/tests/http/GreetServiceEndpoint.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.util.Optional;
2323
import java.util.concurrent.atomic.AtomicInteger;
2424
import java.util.concurrent.atomic.AtomicReference;
25+
import java.util.function.Supplier;
2526

2627
import io.helidon.common.Default;
2728
import io.helidon.common.context.Context;
29+
import io.helidon.common.mapper.OptionalValue;
2830
import io.helidon.common.media.type.MediaTypes;
2931
import io.helidon.config.Configuration;
3032
import io.helidon.faulttolerance.Ft;
@@ -37,6 +39,7 @@
3739
import io.helidon.security.abac.role.RoleValidator;
3840
import io.helidon.service.registry.Service;
3941
import io.helidon.webserver.http.RestServer;
42+
import io.helidon.webserver.http.ServerRequest;
4043

4144
/**
4245
* A simple endpoint to greet you. Examples:
@@ -66,9 +69,16 @@ class GreetServiceEndpoint implements GreetService {
6669
*/
6770
private final AtomicReference<String> greeting = new AtomicReference<>();
6871

72+
/**
73+
* A per-request supplier.
74+
*/
75+
private final Supplier<ServerRequest> serverRequestSupplier;
76+
6977
@Service.Inject
70-
GreetServiceEndpoint(@Configuration.Value("app.greeting") @Default.Value("Ciao") String greeting) {
78+
GreetServiceEndpoint(@Configuration.Value("app.greeting") @Default.Value("Ciao") String greeting,
79+
Supplier<ServerRequest> serverRequestSupplier) {
7180
this.greeting.set(greeting);
81+
this.serverRequestSupplier = serverRequestSupplier;
7282
}
7383

7484
@Http.GET
@@ -227,6 +237,15 @@ JsonObject updateGreetingHandlerWithSecurity(@Http.Entity JsonObject greetingMes
227237
return response;
228238
}
229239

240+
@Http.POST
241+
@Http.Path("/server-request")
242+
@Http.Produces(MediaTypes.TEXT_PLAIN_VALUE)
243+
String serverRequestInjection() {
244+
ServerRequest serverRequest = serverRequestSupplier.get();
245+
OptionalValue<String> greet = serverRequest.query().first("greet");
246+
return greet.get();
247+
}
248+
230249
private JsonObject response(String name) {
231250
return JsonObject.builder()
232251
.set("message", stringResponse(name))

declarative/tests/http/src/main/java/module-info.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,9 @@
3636
requires io.helidon.security.integration.common;
3737
requires io.helidon.security.annotations;
3838

39+
// required for generated binding
40+
requires io.helidon.webserver.context;
41+
requires io.helidon.webserver.observe;
42+
3943
exports io.helidon.declarative.tests.http;
4044
}

declarative/tests/http/src/main/resources/application.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2025 Oracle and/or its affiliates.
2+
# Copyright (c) 2025, 2026 Oracle and/or its affiliates.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -24,6 +24,9 @@ server:
2424
# THIS MUST NOT BE USED IN PRODUCTION!
2525
# exception message will be sent back over the network - this may give potential attackers information about your application
2626
include-entity: true
27+
features:
28+
request-scope:
29+
enabled: true
2730

2831
greet-service.client:
2932
uri: "http://localhost:${test.server.port}"

declarative/tests/http/src/test/java/io/helidon/declarative/tests/http/DeclarativeHttpTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,14 @@ void testTypedClient() {
200200
jsonMessage = typedClient.updateGreetingHandlerReturningCurrent(newGreeting);
201201
assertThat(jsonMessage.stringValue("message", "bad"), is("Ahoj World!"));
202202
}
203+
204+
@Test
205+
void testServerRequestInjection() {
206+
var response = client.post("/greet/server-request")
207+
.queryParam("greet", "hello")
208+
.request(String.class);
209+
210+
assertThat(response.status(), is(Status.OK_200));
211+
assertThat(response.entity(), is("hello"));
212+
}
203213
}

declarative/tests/metrics/src/main/java/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
// needed for generated binding
2828
requires io.helidon.config.yaml;
2929

30+
// required for generated binding
31+
requires io.helidon.webserver.observe;
32+
3033
exports io.helidon.declarative.tests.metrics;
3134
}

declarative/tests/tracing/src/main/java/module-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
requires io.helidon.metrics.api;
2626
requires io.helidon.config.yaml;
2727
requires io.helidon.webserver.observe.tracing;
28-
28+
requires io.helidon.webserver.concurrency.limits;
2929

3030
exports io.helidon.declarative.tests.tracing;
3131
}

service/codegen/src/main/java/io/helidon/service/codegen/ServiceContracts.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, 2025 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2026 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -247,11 +247,15 @@ public void addContracts(Set<ResolvedType> contractSet,
247247
}
248248
}
249249
if (!wildcards.isEmpty()) {
250+
// Circle raw type
251+
contractSet.add(ResolvedType.create(withGenerics.genericTypeName()));
252+
250253
contractSet.add(ResolvedType.create(
251254
TypeName.builder()
252255
.from(withGenerics)
253256
.typeArguments(wildcards)
254257
.build()));
258+
255259
if (extendsValid) {
256260
contractSet.add(ResolvedType.create(
257261
TypeName.builder()

service/registry/src/main/java/io/helidon/service/registry/Service.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ private Service() {
126126
* Injections to different scopes are supported, but must be through a {@link java.util.function.Supplier},
127127
* as we do not provide a proxy mechanism for instances.
128128
* <p>
129-
* Request scope is not started by default. If you want to use request scope, you can add the following
130-
* library to your classpath to add support for it:
131-
* <ul>
132-
* <li>{@code io.helidon.declarative.webserver:helidon-declarative-webserver-request-scope}</li>
133-
* </ul>
129+
* Request scope is not enabled by default in the Webserver. To do so, enable the feature
130+
* as follows:
131+
* <pre>
132+
* server:
133+
* features:
134+
* request-scope:
135+
* enabled: true
136+
* </pre>
134137
*/
135138
@Documented
136139
@Retention(RetentionPolicy.CLASS)

webserver/webserver/src/main/java/io/helidon/webserver/WebServerConfigBlueprint.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2026 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
@Prototype.Blueprint(decorator = WebServerConfigSupport.ServerConfigDecorator.class)
3434
@Prototype.CustomMethods(WebServerConfigSupport.CustomMethods.class)
3535
@Prototype.Configured("server")
36+
@Prototype.RegistrySupport
3637
interface WebServerConfigBlueprint extends ListenerConfigBlueprint, Prototype.Factory<WebServer> {
3738
/**
3839
* When true the webserver registers a shutdown hook with the JVM Runtime.

webserver/webserver/src/main/resources/service.loader

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)