Skip to content

Commit

Permalink
Update input param validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aashikam committed May 11, 2021
1 parent c76d35d commit 5f77ad1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public void testValidService6() {
Assert.assertEquals(diagnosticResult.diagnostics().size(), 0);
}

@Test
public void testValidService7() {
Package currentPackage = loadPackage("valid_service_7");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.diagnostics().size(), 0);
}

@Test
public void testInvalidService1() {
Package currentPackage = loadPackage("invalid_service_1");
Expand Down Expand Up @@ -203,6 +211,19 @@ public void testInvalidService10() {
}
}

@Test
public void testInvalidService11() {
Package currentPackage = loadPackage("invalid_service_11");
PackageCompilation compilation = currentPackage.getCompilation();
DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertEquals(diagnosticResult.diagnostics().size(), 4);
Object[] diagnostics = diagnosticResult.diagnostics().toArray();
for (Object obj : diagnostics) {
Diagnostic diagnostic = (Diagnostic) obj;
assertDiagnostic(diagnostic, CompilationErrors.INVALID_RESOURCE_INPUT_PARAM);
}
}

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
@@ -0,0 +1,4 @@
[package]
org = "graphql_test"
name = "invalid_service_11"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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:Service on new graphql:Listener(4000) {
isolated resource function get greeting(int|string? something) returns string {
return "Hello";
}
}

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

service graphql:Service on new graphql:Listener(4000) {
isolated resource function get greet(map<string>? name) returns string {
return "Hello";
}
}

service graphql:Service on new graphql:Listener(4000) {
isolated resource function get greet(byte[]? name) returns string {
return "Hello";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "graphql_test"
name = "valid_service_7"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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:Service on new graphql:Listener(4000) {
isolated resource function get birthdate(string? format) returns string {
return "01-01-1980";
}
}

service graphql:Service on new graphql:Listener(4000) {
isolated resource function get isLegal(int? age) returns boolean {
if (age is int) {
if (age < 21) {
return false;
}
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,27 @@ private void validateInputParamType(FunctionDefinitionNode functionDefinitionNod
}

private boolean hasInvalidInputParamType(TypeSymbol inputTypeSymbol) {
if (inputTypeSymbol.typeKind() == TypeDescKind.UNION) {
List<TypeSymbol> members = ((UnionTypeSymbol) inputTypeSymbol).memberTypeDescriptors();
boolean hasInvalidMember = false;
int hasType = 0;
for (TypeSymbol member: members) {
if (member.typeKind() != TypeDescKind.NIL) {
hasInvalidMember = hasInvalidInputParamType(member);
hasType++;
}
}
if (hasType > 1) {
hasInvalidMember = true;
}
return hasInvalidMember;
}
if (inputTypeSymbol.typeKind() == TypeDescKind.TYPE_REFERENCE) {
if ((((TypeReferenceTypeSymbol) inputTypeSymbol).definition()).kind() == SymbolKind.ENUM) {
return false;
}
} else {
return !hasPrimitiveType(inputTypeSymbol);
}
return true;
return !hasPrimitiveType(inputTypeSymbol);
}

private boolean hasInvalidReturnType(TypeSymbol returnTypeSymbol) {
Expand Down

0 comments on commit 5f77ad1

Please sign in to comment.