Skip to content

Commit

Permalink
Merge pull request #1729 from dilanSachi/remove-interceptor-config
Browse files Browse the repository at this point in the history
Remove interceptor config from listener and service configurations
  • Loading branch information
TharmiganK committed Jul 28, 2023
2 parents 36aee64 + 8a3e4c6 commit 9b4e83f
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 467 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ final http:Client interceptorsBasicTestsClientEP1 = check new("http://localhost:

listener http:Listener interceptorsBasicTestsServerEP1 = new(interceptorBasicTestsPort1, httpVersion = http:HTTP_1_1);

@http:ServiceConfig {
interceptors : [new DefaultRequestInterceptor(), new LastRequestInterceptor(), new DefaultRequestErrorInterceptor()]
}
service /defaultRequestInterceptor on interceptorsBasicTestsServerEP1 {
service http:InterceptableService /defaultRequestInterceptor on interceptorsBasicTestsServerEP1 {

public function createInterceptors() returns [DefaultRequestInterceptor, LastRequestInterceptor, DefaultRequestErrorInterceptor] {
return [new DefaultRequestInterceptor(), new LastRequestInterceptor(), new DefaultRequestErrorInterceptor()];
}

resource function 'default .(http:Caller caller, http:Request req) returns error? {
http:Response res = new();
Expand Down Expand Up @@ -82,10 +83,11 @@ function testinterceptableServiceReqInterceptor() returns error? {
common:assertHeaderValue(check res.getHeader("last-request-interceptor"), "true");
}

@http:ServiceConfig {
interceptors : [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultResponseInterceptor()]
}
service /defaultResponseInterceptor on interceptorsBasicTestsServerEP1 {
service http:InterceptableService /defaultResponseInterceptor on interceptorsBasicTestsServerEP1 {

public function createInterceptors() returns [LastResponseInterceptor, DefaultResponseErrorInterceptor, DefaultResponseInterceptor] {
return [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new DefaultResponseInterceptor()];
}

resource function 'default .(http:Request req) returns string {
string|error payload = req.getTextPayload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,12 @@ function testInvalidPathWithRootService() returns error? {
common:assertHeaderValue(check res.getHeader("error-type"), "DispatchingError-Resource");
}

listener http:Listener singleServiceWithListenerInterceptorsEP = new(singleServiceWithListenerInterceptorsTestPort,
httpVersion = http:HTTP_1_1, interceptors = [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor()]);
listener http:Listener singleServiceWithListenerInterceptorsEP = new(singleServiceWithListenerInterceptorsTestPort, httpVersion = http:HTTP_1_1);

service http:InterceptableService /path1 on singleServiceWithListenerInterceptorsEP {

public function createInterceptors() returns [ResponseInterceptorReturnsResponse, ResponseErrorInterceptorWithReq] {
return [new ResponseInterceptorReturnsResponse(), new ResponseErrorInterceptorWithReq()];
public function createInterceptors() returns [LastResponseInterceptor, DefaultResponseErrorInterceptor, ResponseInterceptorReturnsResponse, ResponseErrorInterceptorWithReq] {
return [new LastResponseInterceptor(), new DefaultResponseErrorInterceptor(), new ResponseInterceptorReturnsResponse(), new ResponseErrorInterceptorWithReq()];
}

resource function get .() returns string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,44 @@ function testHTTPS2DataBinding() returns error? {
test:assertEquals(payload, {'key:"wso2",age:12});
}

@http:ServiceConfig {
interceptors : [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()]
}
service /interceptor1 on generalHTTP2Listener {
service http:InterceptableService /interceptor1 on generalHTTP2Listener {

public function createInterceptors() returns [DefaultRequestInterceptor, DataBindingRequestInterceptor, LastRequestInterceptor] {
return [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()];
}

resource function post databinding(@http:Payload string payload) returns string {
return "Response : " + payload;
}
}

@http:ServiceConfig {
interceptors : [new DefaultRequestInterceptor(), new LastRequestInterceptor()]
}
service /interceptor2 on generalHTTP2Listener {
service http:InterceptableService /interceptor2 on generalHTTP2Listener {

public function createInterceptors() returns [DefaultRequestInterceptor, LastRequestInterceptor] {
return [new DefaultRequestInterceptor(), new LastRequestInterceptor()];
}

resource function post databinding(@http:Payload string payload) returns string {
return "Response : " + payload;
}
}

@http:ServiceConfig {
interceptors : [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()]
}
service /interceptor1 on generalHTTPS2Listener {

service http:InterceptableService /interceptor1 on generalHTTPS2Listener {

public function createInterceptors() returns [DefaultRequestInterceptor, DataBindingRequestInterceptor, LastRequestInterceptor] {
return [new DefaultRequestInterceptor(), new DataBindingRequestInterceptor(), new LastRequestInterceptor()];
}

resource function post databinding(@http:Payload string payload) returns string {
return "Response : " + payload;
}
}

@http:ServiceConfig {
interceptors : [new DefaultRequestInterceptor(), new LastRequestInterceptor()]
}
service /interceptor2 on generalHTTPS2Listener {
service http:InterceptableService /interceptor2 on generalHTTPS2Listener {

public function createInterceptors() returns [DefaultRequestInterceptor, LastRequestInterceptor] {
return [new DefaultRequestInterceptor(), new LastRequestInterceptor()];
}

resource function post databinding(@http:Payload string payload) returns string {
return "Response : " + payload;
Expand Down
5 changes: 0 additions & 5 deletions ballerina/http_annotation.bal
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public type HttpServiceConfig record {|
ListenerAuthConfig[] auth?;
string mediaTypeSubtypePrefix?;
boolean treatNilableAsOptional = true;
# interceptors - An array of interceptor services
# # Deprecated
# Defining interceptor pipeline in `http:ServiceConfig` is deprecated. Define the interceptor pipeline via `http:InterceptableService` service type
@deprecated
Interceptor|Interceptor[] interceptors?;
byte[] openApiDefinition = [];
boolean validation = true;
|};
Expand Down
19 changes: 2 additions & 17 deletions ballerina/http_service_endpoint.bal
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,8 @@ public isolated class Listener {
return externInitEndpoint(self, config);
}

isolated function createInterceptors(Interceptor|Interceptor[]? interceptorConfigValues) returns Interceptor[] {
Interceptor|Interceptor[]? configValues = interceptorConfigValues;
Interceptor[] interceptors = [new DefaultErrorInterceptor()];
if configValues is Interceptor[] {
foreach Interceptor interceptor in configValues {
interceptors.push(interceptor);
}
} else if configValues is Interceptor {
interceptors.push(configValues);
}
return interceptors;
isolated function createInterceptors() returns Interceptor[] {
return [new DefaultErrorInterceptor()];
}

# Starts the registered service programmatically.
Expand Down Expand Up @@ -165,12 +156,6 @@ public type ListenerConfiguration record {|
decimal timeout = DEFAULT_LISTENER_TIMEOUT;
string? server = ();
RequestLimitConfigs requestLimits = {};
# interceptors - An array of interceptor services
# # Deprecated
# Defining interceptor pipeline in `http:ListenerConfiguration` is deprecated.
# Define the interceptor pipeline via `http:InterceptableService` service type
@deprecated
Interceptor|Interceptor[] interceptors?;
decimal gracefulStopTimeout = DEFAULT_GRACEFULSTOP_TIMEOUT;
ServerSocketConfig socketConfig = {};
int http2InitialWindowSize = 65535;
Expand Down
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- [Add open record support for query parameters](https://github.com/ballerina-platform/ballerina-standard-library/issues/4541)

### Changed

- [Remove interceptor configuration from `http:ListenerConfiguration` and `http:ServiceConfig`](https://github.com/ballerina-platform/ballerina-standard-library/issues/4680)

## [2.9.0] - 2023-06-30

### Added
Expand All @@ -28,7 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Changed

- [Removed listener level interceptors](https://github.com/ballerina-platform/ballerina-standard-library/issues/4420)
- [Deprecate listener level interceptors](https://github.com/ballerina-platform/ballerina-standard-library/issues/4420)
- [Improved default error format and message](https://github.com/ballerina-platform/ballerina-standard-library/issues/3961)
- [Move status code errors to `httpscerr` module](https://github.com/ballerina-platform/ballerina-standard-library/issues/4535)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ private void assertError(DiagnosticResult diagnosticResult, int index, String me
}
}

private void assertWarning(DiagnosticResult diagnosticResult, int index, String message, String code) {
Diagnostic diagnostic = (Diagnostic) diagnosticResult.warnings().toArray()[index];
Assert.assertTrue(diagnostic.diagnosticInfo().messageFormat().contains(message));
Assert.assertEquals(diagnostic.diagnosticInfo().code(), code);
}

private void assertTrue(DiagnosticResult diagnosticResult, int index, String message, String code) {
Diagnostic diagnostic = (Diagnostic) diagnosticResult.errors().toArray()[index];
Assert.assertTrue(diagnostic.diagnosticInfo().messageFormat().contains(message));
Expand Down Expand Up @@ -884,66 +878,12 @@ public void testResourceSignatureParamValidations() {
"'(http_test/sample_38:0.1.0:QueryRecord|map<any>)[]?'", HTTP_106);
}

@Test
public void testServiceConfigInterceptorDeprecationWarning() {
Package currentPackage = loadPackage("sample_package_39");
PackageCompilation packageCompilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult();
Assert.assertFalse(diagnosticResult.hasErrors());
Assert.assertEquals(diagnosticResult.warnings().size(), 1);
assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_201.getMessage(),
HttpDiagnosticCodes.HTTP_201.getCode());
}

@Test
public void testServiceConfigInterceptorAndInterceptableServiceError() {
Package currentPackage = loadPackage("sample_package_40");
PackageCompilation packageCompilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.warningCount(), 1);
assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_201.getMessage(),
HttpDiagnosticCodes.HTTP_201.getCode());
Assert.assertEquals(diagnosticResult.errorCount(), 1);
assertTrue(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_153.getMessage(),
HttpDiagnosticCodes.HTTP_153.getCode());
}

@Test
public void testInterceptableServiceInterceptors() {
Package currentPackage = loadPackage("sample_package_41");
Package currentPackage = loadPackage("sample_package_39");
PackageCompilation packageCompilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult();
Assert.assertFalse(diagnosticResult.hasWarnings());
Assert.assertFalse(diagnosticResult.hasErrors());
}

@Test
public void testInterceptorsDefinedInListener() {
Package currentPackage = loadPackage("sample_package_42");
PackageCompilation packageCompilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult();
Assert.assertFalse(diagnosticResult.hasErrors());
Assert.assertEquals(diagnosticResult.warningCount(), 2);
assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_202.getMessage(),
HttpDiagnosticCodes.HTTP_202.getCode());
assertWarning(diagnosticResult, 1, HttpDiagnosticCodes.HTTP_202.getMessage(),
HttpDiagnosticCodes.HTTP_202.getCode());
}

@Test
public void testInterceptorsDefinedInListenerAndServiceConfig() {
Package currentPackage = loadPackage("sample_package_43");
PackageCompilation packageCompilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = packageCompilation.diagnosticResult();
Assert.assertFalse(diagnosticResult.hasErrors());
Assert.assertEquals(diagnosticResult.warningCount(), 4);
assertWarning(diagnosticResult, 0, HttpDiagnosticCodes.HTTP_201.getMessage(),
HttpDiagnosticCodes.HTTP_201.getCode());
assertWarning(diagnosticResult, 1, HttpDiagnosticCodes.HTTP_202.getMessage(),
HttpDiagnosticCodes.HTTP_202.getCode());
assertWarning(diagnosticResult, 2, HttpDiagnosticCodes.HTTP_202.getMessage(),
HttpDiagnosticCodes.HTTP_202.getCode());
assertWarning(diagnosticResult, 3, HttpDiagnosticCodes.HTTP_201.getMessage(),
HttpDiagnosticCodes.HTTP_201.getCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,22 @@ service class MyResponseInterceptor {
}
}

@http:ServiceConfig {
interceptors: [new MyRequestInterceptor(), new MyResponseInterceptor()]
service http:InterceptableService / on new http:Listener(9099) {

public function createInterceptors() returns [MyRequestInterceptor, MyResponseInterceptor] {
return [new MyRequestInterceptor(), new MyResponseInterceptor()];
}

resource function get hello(http:Caller caller) returns error? {
check caller->respond("Hello, World!");
}
}
service / on new http:Listener(9099) {

service http:InterceptableService / on new http:Listener(9100) {

public function createInterceptors() returns MyRequestInterceptor {
return new MyRequestInterceptor();
}

resource function get hello(http:Caller caller) returns error? {
check caller->respond("Hello, World!");
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 9b4e83f

Please sign in to comment.