Skip to content

Commit

Permalink
Merge pull request #1459 from TharmiganK/fix-req-ctx-apis-3.x
Browse files Browse the repository at this point in the history
[2201.3.x] Fix the `ReqCtxMember` type definition
  • Loading branch information
chamil321 committed Feb 27, 2023
2 parents d59f5c4 + 084ca6a commit 18be22d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 50 deletions.
6 changes: 3 additions & 3 deletions ballerina-tests/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
org = "ballerina"
name = "http_tests"
version = "2.5.3"
version = "2.5.4"

[[platform.java11.dependency]]
scope = "testOnly"
path = "../native/build/libs/http-native-2.5.3.jar"
path = "../native/build/libs/http-native-2.5.4-SNAPSHOT.jar"

[[platform.java11.dependency]]
scope = "testOnly"
path = "../test-utils/build/libs/http-test-utils-2.5.3.jar"
path = "../test-utils/build/libs/http-test-utils-2.5.4-SNAPSHOT.jar"
4 changes: 2 additions & 2 deletions ballerina-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.5.3"
version = "2.5.4"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -112,7 +112,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http_tests"
version = "2.5.3"
version = "2.5.4"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "constraint"},
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/tests/interceptors_test_common.bal
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ service class ByteArrayPayloadBindingRequestInterceptor {
*http:RequestInterceptor;

resource function 'default [string... path](http:RequestContext ctx, @http:Payload byte[] person) returns http:NextService|error? {
ctx.set("request-payload", strings:fromBytes(person));
ctx.set("request-payload", check strings:fromBytes(person));
return ctx.next();
}
}
Expand Down
4 changes: 2 additions & 2 deletions ballerina-tests/tests/req_ctx_methods_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ function testReqCtxGetWithTypeSuccess() returns error? {
function testReqCtxGetWithTypeFailure() {
http:RequestContext reqCtx = new;
string|error value1 = reqCtx.getWithType("key");
test:assertTrue(value1 is error, msg = "Unexpected value returned");
test:assertTrue(value1 is http:ListenerError, msg = "Unexpected value returned");

reqCtx.set("key", "value");
User|error value2 = reqCtx.getWithType("key");
test:assertTrue(value2 is error, msg = "Unexpected value returned");
test:assertTrue(value2 is http:ListenerError, msg = "Unexpected value returned");
}

@test:Config {}
Expand Down
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "http"
version = "2.5.3"
version = "2.5.4"
authors = ["Ballerina"]
keywords = ["http", "network", "service", "listener", "client"]
repository = "https://github.com/ballerina-platform/module-ballerina-http"
Expand All @@ -12,8 +12,8 @@ distribution = "2201.3.0"
[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "http-native"
version = "2.5.3"
path = "../native/build/libs/http-native-2.5.3.jar"
version = "2.5.4"
path = "../native/build/libs/http-native-2.5.4-SNAPSHOT.jar"

[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "http-compiler-plugin"
class = "io.ballerina.stdlib.http.compiler.HttpCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/http-compiler-plugin-2.5.3.jar"
path = "../compiler-plugin/build/libs/http-compiler-plugin-2.5.4-SNAPSHOT.jar"
10 changes: 5 additions & 5 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ modules = [
[[package]]
org = "ballerina"
name = "constraint"
version = "1.0.1"
version = "1.0.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -78,7 +78,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.5.3"
version = "2.5.4"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -237,7 +237,7 @@ modules = [
[[package]]
org = "ballerina"
name = "log"
version = "2.5.0"
version = "2.5.1"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down Expand Up @@ -279,7 +279,7 @@ modules = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.0.5"
version = "1.0.6"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -300,7 +300,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "regex"
version = "1.3.1"
version = "1.3.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"}
Expand Down
17 changes: 10 additions & 7 deletions ballerina/http_request_context.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
// under the License.

import ballerina/jballerina.java;
import ballerina/lang.value;

// This is same as the `value:Cloneable`, except that it does not include `error` type.
# Represents a non-error type that can be cloned.
public type Cloneable (any & readonly)|xml|Cloneable[]|map<Cloneable>|table<map<Cloneable>>;

# Request context member type.
public type ReqCtxMember value:Cloneable|isolated object {};
public type ReqCtxMember Cloneable|isolated object {};

# Request context member type descriptor.
public type ReqCtxMemberType typedesc<ReqCtxMember>;
Expand All @@ -32,15 +35,15 @@ public isolated class RequestContext {
# + key - Represents the member key
# + value - Represents the member value
public isolated function set(string key, ReqCtxMember value) {
if value is value:Cloneable {
if value is Cloneable {
lock {
self.members[key] = value.clone();
}
}
else {
lock {
self.members[key] = value;
}
}
}
}

Expand All @@ -50,9 +53,9 @@ public isolated class RequestContext {
# + return - Member value
public isolated function get(string key) returns ReqCtxMember {
lock {
value:Cloneable|isolated object {} value = self.members.get(key);
Cloneable|isolated object {} value = self.members.get(key);

if value is value:Cloneable {
if value is Cloneable {
return value.clone();
} else {
return value;
Expand Down Expand Up @@ -95,7 +98,7 @@ public isolated class RequestContext {
# + key - Represents the member key
public isolated function remove(string key) {
lock {
ReqCtxMember err = trap self.members.remove(key);
ReqCtxMember|error err = trap self.members.remove(key);
if err is error {
panic err;
}
Expand Down
55 changes: 30 additions & 25 deletions docs/spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2057,51 +2057,56 @@ service class RequestInterceptor {
```

##### 8.1.1.1 Request context
Following is the rough definition of the interceptor context.
Following is the rough definition of the interceptor context. Request context can store non-error values, and these values
can be retrieved at the next services in the pipeline.
```ballerina
# Request context attribute type.
public type ReqCtxAttribute value:Cloneable|isolated object {};
// This is same as the `value:Cloneable`, except that it does not include `error` type.
# Represents a non-error type that can be cloned.
public type Cloneable (any & readonly)|xml|Cloneable[]|map<Cloneable>|table<map<Cloneable>>;
# Request context member type.
public type ReqCtxMember Cloneable|isolated object {};
# Request context attribute type descriptor.
public type ReqCtxAttributeType typedesc<ReqCtxAttribute>;
# Request context member type descriptor.
public type ReqCtxMemberType typedesc<ReqCtxMember>;
# Represents an HTTP Context that allows user to pass data between interceptors.
public isolated class RequestContext {
private final map<ReqCtxAttribute> attributes = {};
private final map<ReqCtxMember> members = {};
# Sets an attribute to the request context object.
# Sets an member to the request context object.
#
# + key - Represents the attribute key
# + value - Represents the attribute value
public isolated function set(string key, ReqCtxAttribute value) {}
# + key - Represents the member key
# + value - Represents the member value
public isolated function set(string key, ReqCtxMember value) {}
# Gets an attribute value from the request context object.
# Gets an member value from the request context object.
#
# + key - Represents the attribute key
# + return - Attribute value
public isolated function get(string key) returns ReqCtxAttribute {}
# + key - Represents the member key
# + return - Member value
public isolated function get(string key) returns ReqCtxMember {}
# Checks whether the request context object has an attribute corresponds to the key.
# Checks whether the request context object has an member corresponds to the key.
#
# + key - Represents the attribute key
# + return - true if the attribute exists, else false
# + key - Represents the member key
# + return - true if the member exists, else false
public isolated function hasKey(string key) returns boolean {}
# Returns the attribute keys of the request context object.
# Returns the member keys of the request context object.
#
# + return - Array of attribute keys
# + return - Array of member keys
public isolated function keys() returns string[] {}
# Gets an attribute value with type from the request context object.
#
# + key - Represents the attribute key
# + targetType - Represents the expected type of the attribute value
# + return - Attribute value or an error if the attribute value is not of the expected type
public isolated function getWithType(string key, ReqCtxAttributeType targetType = <>) returns targetType|ListenerError = external;
# + key - Represents the member key
# + targetType - Represents the expected type of the member value
# + return - Attribute value or an error if the member value is not of the expected type
public isolated function getWithType(string key, ReqCtxMemberType targetType = <>) returns targetType|ListenerError = external;
# Removes an attribute from the request context object. It panics if there is no such member.
# Removes an member from the request context object. It panics if there is no such member.
#
# + key - Represents the attribute key
# + key - Represents the member key
public isolated function remove(string key) {}
# Calls the next service in the interceptor pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ public static Object getWithType(BObject requestCtx, BString key, BTypedesc targ
BMap members = requestCtx.getMapValue(HttpConstants.REQUEST_CTX_MEMBERS);
try {
Object value = members.getOrThrow(key);
return EnsureType.ensureType(value, targetType);
Object convertedType = EnsureType.ensureType(value, targetType);
if (convertedType instanceof BError) {
return HttpUtil.createHttpError("type conversion failed for value of key: " + key.getValue(),
HttpErrorType.GENERIC_LISTENER_ERROR,
(BError) convertedType);
}
return convertedType;
} catch (Exception exp) {
return HttpUtil.createHttpError("no member found for key: " + key.getValue(),
HttpErrorType.GENERIC_LISTENER_ERROR,
Expand Down

0 comments on commit 18be22d

Please sign in to comment.