Skip to content
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

Fix Validating any and anydata Returning from Resources #329

Merged
merged 2 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]

### Changed
- [[1597] Validate Max Query Depth at Runtime](https://github.com/ballerina-platform/ballerina-standard-library/issues/1597)
- [[#1597] Validate Max Query Depth at Runtime](https://github.com/ballerina-platform/ballerina-standard-library/issues/1597)

### Fixed
- [[#1612] Invalidate Returning any or anydata from GraphQL Resource Functions](https://github.com/ballerina-platform/ballerina-standard-library/issues/1622)
ThisaruGuruge marked this conversation as resolved.
Show resolved Hide resolved

## [0.2.0.beta.2] - 2021-07-06

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void testInvalidResourceInputParameterTypes() {
Package currentPackage = loadPackage("invalid_service_5");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.errorCount(), 5);
Assert.assertEquals(diagnosticResult.errorCount(), 7);
Iterator<Diagnostic> diagnosticIterator = diagnosticResult.errors().iterator();

Diagnostic diagnostic = diagnosticIterator.next();
Expand All @@ -210,6 +210,12 @@ public void testInvalidResourceInputParameterTypes() {

diagnostic = diagnosticIterator.next();
assertError(diagnostic, CompilationError.INVALID_RESOURCE_INPUT_PARAM, 49, 49);

diagnostic = diagnosticIterator.next();
assertError(diagnostic, CompilationError.INVALID_RESOURCE_INPUT_PARAM, 55, 37);

diagnostic = diagnosticIterator.next();
assertError(diagnostic, CompilationError.INVALID_RESOURCE_INPUT_PARAM, 61, 41);
}

@Test
Expand Down Expand Up @@ -381,6 +387,21 @@ public void testInvalidResourcePath() {
assertError(diagnostic, CompilationError.INVALID_FIELD_NAME, 36, 23);
}

@Test
public void testInvalidReturnTypeAny() {
Package currentPackage = loadPackage("invalid_service_17");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.errorCount(), 2);
Iterator<Diagnostic> diagnosticIterator = diagnosticResult.errors().iterator();

Diagnostic diagnostic = diagnosticIterator.next();
assertError(diagnostic, CompilationError.INVALID_RETURN_TYPE_ANY, 20, 5);

diagnostic = diagnosticIterator.next();
assertError(diagnostic, CompilationError.INVALID_RETURN_TYPE_ANY, 24, 5);
}

private Package loadPackage(String path) {
Path projectDirPath = RESOURCE_DIRECTORY.resolve(path);
BuildProject project = BuildProject.load(getEnvironmentBuilder(), projectDirPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ distinct service class FirstName {
}
}

service isolated class Age {
isolated resource function get age() returns int {
service class Age {
resource function get age() returns int {
return 15;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ service /graphql on new graphql:Listener(4000) {
};
}

isolated resource function get mountain/name/__first() returns string {
resource function get mountain/name/__first() returns string {
return "MountainFirst";
}

isolated resource function get mountain/__name/last() returns string {
resource function get mountain/__name/last() returns string {
return "MountainLast";
}

isolated resource function get __mountain/id() returns string {
resource function get __mountain/id() returns string {
return "M1";
}

isolated resource function get mountain/getTrail() returns Trail {
resource function get mountain/getTrail() returns Trail {
return new;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "graphql_test"
name = "invalid_service_17"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/graphql;

service /graphql on new graphql:Listener(4000) {
resource function get name() returns any {
return "Sam";
}

resource function get age() returns anydata {
return 3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ service graphql:Service on new graphql:Listener(4000) {
return "Hello";
}
}

service graphql:Service on new graphql:Listener(4000) {
resource function get greet(any name) returns string {
return "Hello";
}
}

service graphql:Service on new graphql:Listener(4000) {
resource function get greet(anydata name) returns string {
return "Hello";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ private void validateResourcePath(MethodSymbol methodSymbol, Location location,
private void validateReturnType(TypeSymbol returnTypeDesc,
Location location, SyntaxNodeAnalysisContext context) {
// if return type is a union
if (returnTypeDesc.typeKind() == TypeDescKind.UNION) {
if (returnTypeDesc.typeKind() == TypeDescKind.ANY || returnTypeDesc.typeKind() == TypeDescKind.ANYDATA) {
Utils.updateContext(context, CompilationError.INVALID_RETURN_TYPE_ANY, location);
} else if (returnTypeDesc.typeKind() == TypeDescKind.UNION) {
validateReturnTypeUnion(context,
((UnionTypeSymbol) returnTypeDesc).memberTypeDescriptors(), location);
} else if (returnTypeDesc.typeKind() == TypeDescKind.ARRAY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
*/
public class Utils {

private Utils() {}
private Utils() {
}

// compiler plugin constants
public static final String PACKAGE_PREFIX = "graphql";
Expand Down Expand Up @@ -79,7 +80,10 @@ enum CompilationError {
DiagnosticSeverity.ERROR),
INVALID_FIELD_NAME("A GraphQL field name must not begin with \"" + DOUBLE_UNDERSCORES +
"\", which is reserved by GraphQL introspection", "GRAPHQL_111",
DiagnosticSeverity.ERROR);
DiagnosticSeverity.ERROR),
INVALID_RETURN_TYPE_ANY(
"A GraphQL resource function cannot return \"any\" or \"anydata\", instead use specific type names",
"GRAPHQL_112", DiagnosticSeverity.ERROR);

private final String error;
private final String errorCode;
Expand Down