Skip to content

Commit

Permalink
Validate the Default Field Complexity and Field Complexity Values (#2004
Browse files Browse the repository at this point in the history
)

* [Automated] Update the native jar versions

* [Automated] Update the native jar versions

* Validate default field complexity and field complexity values
  • Loading branch information
ThisaruGuruge authored Aug 16, 2024
1 parent 8990fab commit d9b9ec0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ballerina-tests/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0-20240806-083400-aabac46a"
distribution-version = "2201.10.0-20240814-095500-9d14866a"

[[package]]
org = "ballerina"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.10.0-20240806-083400-aabac46a"
distribution-version = "2201.10.0-20240814-095500-9d14866a"

[[package]]
org = "ballerina"
Expand Down
9 changes: 7 additions & 2 deletions ballerina/engine.bal
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ isolated class Engine {
if maxQueryDepth is int && maxQueryDepth < 1 {
return error Error("Max query depth value must be a positive integer");
}
if queryComplexityConfig is QueryComplexityConfig && queryComplexityConfig.maxComplexity < 0 {
return error Error("Max complexity value must be greater than zero");
if queryComplexityConfig is QueryComplexityConfig {
if queryComplexityConfig.maxComplexity < 0 {
return error Error("Max complexity value must be greater than zero");
}
if queryComplexityConfig.defaultFieldComplexity < 0 {
return error Error("Default field complexity value must be greater than zero");
}
}
self.maxQueryDepth = maxQueryDepth;
self.schema = check createSchema(schemaString);
Expand Down
14 changes: 14 additions & 0 deletions ballerina/tests/06_configurations_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,17 @@ isolated function testInvalidMaxQueryComplexity() returns error? {
test:assertEquals(err.message(), "Max complexity value must be greater than zero");
}
}

@test:Config {
groups: ["configs", "validation", "query_complexity"]
}
isolated function testInvalidDefaultFieldComplexity() returns error? {
lock {
Engine|Error engine = new ("", 10, testService, [], true, true, queryComplexityConfig = {
defaultFieldComplexity: -1
});
test:assertTrue(engine is Error);
Error err = <Error>engine;
test:assertEquals(err.message(), "Default field complexity value must be greater than zero");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package io.ballerina.stdlib.graphql.runtime.engine.meta;

/**
* This class stores the meta information of a Ballerina GraphQL resource.
* This class stores the meta-information of a Ballerina GraphQL resource.
*
* @param complexity - The complexity of the resource.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.stdlib.graphql.runtime.utils.Utils;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -86,15 +87,15 @@ private void analyzeResourceMethod(ResourceMethodType resourceMethod, String typ
return;
}
BMap<BString, Object> resourceConfig = getResourceConfig(resourceMethod);
Long complexity = getComplexity(resourceConfig);
Long complexity = getComplexity(coordinate, resourceConfig);
Resource resource = new Resource(complexity);
this.resourceMap.put(coordinate, resource);
analyzeType(resourceMethod.getType().getReturnType());
}

private void analyzeRemoteMethod(RemoteMethodType remoteMethod, String coordinate) {
BMap<BString, Object> resourceConfig = getResourceConfig(remoteMethod);
Long complexity = getComplexity(resourceConfig);
Long complexity = getComplexity(coordinate, resourceConfig);
Resource resource = new Resource(complexity);
this.resourceMap.put(coordinate, resource);
analyzeType(remoteMethod.getType().getReturnType());
Expand Down Expand Up @@ -166,11 +167,16 @@ public Map<String, Resource> getResourceMap() {
return this.resourceMap;
}

private Long getComplexity(BMap<BString, Object> resourceConfig) {
private Long getComplexity(String coordinate, BMap<BString, Object> resourceConfig) {
if (resourceConfig == null) {
return this.defaultQueryComplexity;
}
if (resourceConfig.containsKey(complexityKey)) {
Long complexity = (Long) resourceConfig.get(complexityKey);
if (complexity < 0) {
String message = "Complexity of the field \"" + coordinate + "\" cannot be negative";
throw Utils.createError(message, Utils.ERROR_TYPE);
}
return (Long) resourceConfig.get(complexityKey);
}
return this.defaultQueryComplexity;
Expand Down

0 comments on commit d9b9ec0

Please sign in to comment.