-
Notifications
You must be signed in to change notification settings - Fork 67
[FEAT] (compile-time error): prevent stub entities from having fields beyond their key fields #2445
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
Open
Waqibsk
wants to merge
4
commits into
ballerina-platform:master
Choose a base branch
from
Waqibsk:feat/compile-time-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
600b904
feat(compile-time error): prevent stub entities from having fields be…
Waqibsk de8ffd7
Merge branch 'master' into feat/compile-time-error
Waqibsk e8a4dc3
[Automated] Update the native jar versions
Waqibsk 48ba731
add: required tests & fix validator function
Waqibsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
.../resources/ballerina_sources/validator_tests/88_valid_entity_with_key_only/Ballerina.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [package] | ||
| org = "graphql_test" | ||
| name = "test_package" | ||
| version = "0.1.0" |
29 changes: 29 additions & 0 deletions
29
...est/resources/ballerina_sources/validator_tests/88_valid_entity_with_key_only/service.bal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com) All Rights Reserved. | ||
| // | ||
| // WSO2 LLC. 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 as gql; | ||
|
|
||
| @gql:ServiceConfig { | ||
| entities: ["Product"] | ||
| } | ||
|
|
||
| service /graphql on new gql:Listener(9090) { | ||
|
|
||
| @gql:Type | ||
| type Product @key(fields: "id") record {| | ||
| readonly string id; | ||
| |}; | ||
| } |
4 changes: 4 additions & 0 deletions
4
...ina_sources/validator_tests/89_valid_entity_with_key_and_resolve_reference/Ballerina.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [package] | ||
| org = "graphql_test" | ||
| name = "test_package" | ||
| version = "0.1.0" |
33 changes: 33 additions & 0 deletions
33
...lerina_sources/validator_tests/89_valid_entity_with_key_and_resolve_reference/service.bal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com) All Rights Reserved. | ||
| // | ||
| // WSO2 LLC. 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; | ||
|
|
||
| @graphql:ServiceConfig { | ||
| entities: ["User"] | ||
| } | ||
| service /graphql on new graphql:Listener(9090) { | ||
|
|
||
| @graphql:Type | ||
| type User @key(fields: "id") record {| | ||
| readonly int id; | ||
| string name?; | ||
| |}; | ||
|
|
||
| isolated function resolveReference(User ref) returns User { | ||
| return {id: ref.id, name: "John Doe"}; | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
...lerina_sources/validator_tests/90_invalid_entity_missing_resolve_reference/Ballerina.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [package] | ||
| org = "graphql_test" | ||
| name = "test_package" | ||
| version = "0.1.0" |
29 changes: 29 additions & 0 deletions
29
...ballerina_sources/validator_tests/90_invalid_entity_missing_resolve_reference/service.bal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com) All Rights Reserved. | ||
| // | ||
| // WSO2 LLC. 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; | ||
|
|
||
| @graphql:ServiceConfig { | ||
| entities: ["User"] | ||
| } | ||
| service /graphql on new graphql:Listener(9090) { | ||
|
|
||
| @graphql:Type | ||
| type User @key(fields: "id") record {| | ||
| readonly int id; | ||
| string name?; | ||
| |}; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,10 +18,12 @@ | |||||||
|
|
||||||||
| package io.ballerina.stdlib.graphql.compiler.service.validator; | ||||||||
|
|
||||||||
| import io.ballerina.compiler.api.SemanticModel; | ||||||||
| import io.ballerina.compiler.api.symbols.AnnotationSymbol; | ||||||||
| import io.ballerina.compiler.api.symbols.ArrayTypeSymbol; | ||||||||
| import io.ballerina.compiler.api.symbols.ClassSymbol; | ||||||||
| import io.ballerina.compiler.api.symbols.EnumSymbol; | ||||||||
| import io.ballerina.compiler.api.symbols.FunctionSymbol; | ||||||||
| import io.ballerina.compiler.api.symbols.FunctionTypeSymbol; | ||||||||
| import io.ballerina.compiler.api.symbols.IntersectionTypeSymbol; | ||||||||
| import io.ballerina.compiler.api.symbols.MapTypeSymbol; | ||||||||
|
|
@@ -76,6 +78,7 @@ | |||||||
| import io.ballerina.tools.diagnostics.Location; | ||||||||
|
|
||||||||
| import java.util.ArrayList; | ||||||||
| import java.util.Collections; | ||||||||
| import java.util.HashSet; | ||||||||
| import java.util.List; | ||||||||
| import java.util.Map; | ||||||||
|
|
@@ -189,6 +192,7 @@ private void validateEntityAnnotation(AnnotationNode entityAnnotation) { | |||||||
| if (entityAnnotation.annotValue().isEmpty()) { | ||||||||
| return; | ||||||||
| } | ||||||||
| List<String> keyFields = new ArrayList<>(); | ||||||||
| for (MappingFieldNode fieldNode : entityAnnotation.annotValue().get().fields()) { | ||||||||
| if (fieldNode.kind() != SPECIFIC_FIELD) { | ||||||||
| addDiagnostic(CompilationDiagnostic.PROVIDE_KEY_VALUE_PAIR_FOR_ENTITY_ANNOTATION, fieldNode.location()); | ||||||||
|
|
@@ -203,8 +207,159 @@ private void validateEntityAnnotation(AnnotationNode entityAnnotation) { | |||||||
| String fieldName = fieldNameToken.text().trim(); | ||||||||
| if (KEY.equals(fieldName)) { | ||||||||
| validateKeyField(specificFieldNode); | ||||||||
| keyFields = extractKeyFields(specificFieldNode); | ||||||||
|
|
||||||||
| } | ||||||||
|
|
||||||||
| validateFieldsAgainstKeys(keyFields, entityAnnotation.location()); | ||||||||
|
|
||||||||
|
Comment on lines
+213
to
+215
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto
Suggested change
|
||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private void validateFieldsAgainstKeys(List<String> keyFields, Location location) { | ||||||||
|
|
||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| if (keyFields == null || keyFields.isEmpty()) { | ||||||||
| return; | ||||||||
|
|
||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| Set<String> keyFieldSet = new HashSet<>(keyFields); | ||||||||
| List<RecordFieldSymbol> recordFields = getRecordFields(); | ||||||||
|
|
||||||||
| boolean hasExtraFields = false; | ||||||||
|
|
||||||||
| for (RecordFieldSymbol recordField : recordFields) { | ||||||||
| String fieldName = recordField.getName().orElse(null); | ||||||||
|
|
||||||||
| if (fieldName == null) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| if (!keyFieldSet.contains(fieldName)) { | ||||||||
| hasExtraFields = true; | ||||||||
| addDiagnostic(CompilationDiagnostic.INVALID_ENTITY_FIELD, | ||||||||
| recordField.location(), fieldName); | ||||||||
| } | ||||||||
| } | ||||||||
| Symbol currentEntity = getCurrentEntitySymbol(); | ||||||||
| if (hasExtraFields && !hasResolveReferenceFunctionForEntity(currentEntity)) { | ||||||||
| addDiagnostic(CompilationDiagnostic.INVALID_ENTITY_FIELD, location); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| private Symbol getCurrentEntitySymbol() { | ||||||||
|
|
||||||||
| for (Map.Entry<String, Symbol> entry : this.interfaceEntityFinder.getEntities().entrySet()) { | ||||||||
| AnnotationSymbol entityAnnotation = getEntityAnnotationSymbol(entry.getValue()); | ||||||||
| if (entityAnnotation != null) { | ||||||||
| return entry.getValue(); | ||||||||
| } | ||||||||
| } | ||||||||
| return null; | ||||||||
| } | ||||||||
|
|
||||||||
| private boolean hasResolveReferenceFunctionForEntity(Symbol entitySymbol) { | ||||||||
| if (entitySymbol == null || context == null) { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| String entityName = entitySymbol.getName().orElse(null); | ||||||||
| if (entityName == null) { | ||||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| // Get semantic model from context | ||||||||
| SemanticModel semanticModel = context.semanticModel(); | ||||||||
|
|
||||||||
| // Get module symbols and search for resolveReference function | ||||||||
| List<Symbol> moduleSymbols = new ArrayList<>(); | ||||||||
| semanticModel.moduleSymbols().forEach(moduleSymbols::add); | ||||||||
|
|
||||||||
| for (Symbol symbol : moduleSymbols) { | ||||||||
| if (symbol.kind() != SymbolKind.FUNCTION) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| FunctionSymbol functionSymbol = (FunctionSymbol) symbol; | ||||||||
| if (!"resolveReference".equals(functionSymbol.getName().orElse(""))) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| Optional<List<ParameterSymbol>> paramsOpt = functionSymbol.typeDescriptor().params(); | ||||||||
| if (paramsOpt.isEmpty() || paramsOpt.get().isEmpty()) { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
|
||||||||
| ParameterSymbol firstParam = paramsOpt.get().get(0); | ||||||||
| TypeSymbol paramType = firstParam.typeDescriptor(); | ||||||||
|
|
||||||||
|
|
||||||||
| // Check if the parameter type matches the entity | ||||||||
| if (paramType != null) { | ||||||||
| // Try matching by name | ||||||||
| if (entityName.equals(paramType.getName().orElse(null))) { | ||||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
| // For type reference types, also check the original type | ||||||||
| if (paramType.typeKind() == TypeDescKind.TYPE_REFERENCE) { | ||||||||
| TypeReferenceTypeSymbol typeRef = (TypeReferenceTypeSymbol) paramType; | ||||||||
| if (entityName.equals(typeRef.getName().orElse(null))) { | ||||||||
| return true; | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return false; | ||||||||
| } | ||||||||
| private List<String> extractKeyFields(SpecificFieldNode specificFieldNode) { | ||||||||
|
|
||||||||
| List<String> keyFields = new ArrayList<>(); | ||||||||
| if (specificFieldNode.valueExpr().isEmpty()) { | ||||||||
| return keyFields; | ||||||||
|
|
||||||||
| } | ||||||||
|
|
||||||||
| ExpressionNode valueNode = specificFieldNode.valueExpr().get(); | ||||||||
|
|
||||||||
| if (valueNode.kind() == SyntaxKind.STRING_LITERAL) { | ||||||||
| keyFields.add(valueNode.toString().replace("\"", "").trim()); | ||||||||
| } else if (valueNode.kind() == SyntaxKind.LIST_CONSTRUCTOR) { | ||||||||
| for (Node keyNode : ((ListConstructorExpressionNode) valueNode).expressions()) { | ||||||||
| if (keyNode.kind() == SyntaxKind.STRING_LITERAL) { | ||||||||
| keyFields.add(keyNode.toString().replace("\"", "").trim()); | ||||||||
| } | ||||||||
| } | ||||||||
| } else { | ||||||||
| addDiagnostic(CompilationDiagnostic.PROVIDE_A_STRING_LITERAL_OR_AN_ARRAY_OF_STRING_LITERALS_FOR_KEY_FIELD, | ||||||||
| valueNode.location()); | ||||||||
|
|
||||||||
| } | ||||||||
|
|
||||||||
| return keyFields; | ||||||||
|
|
||||||||
| } | ||||||||
|
|
||||||||
| private List<RecordFieldSymbol> getRecordFields() { | ||||||||
|
|
||||||||
| for (Map.Entry<String, Symbol> entry : this.interfaceEntityFinder.getEntities().entrySet()) { | ||||||||
| AnnotationSymbol entityAnnotation = getEntityAnnotationSymbol(entry.getValue()); | ||||||||
| if (entityAnnotation != null) { | ||||||||
| Symbol entitySymbol = entry.getValue(); | ||||||||
|
|
||||||||
| if (entitySymbol instanceof TypeDefinitionSymbol) { | ||||||||
| TypeDefinitionSymbol typeDefSymbol = (TypeDefinitionSymbol) entitySymbol; | ||||||||
| TypeSymbol typeDescriptor = typeDefSymbol.typeDescriptor(); | ||||||||
|
|
||||||||
| if (typeDescriptor instanceof RecordTypeSymbol) { | ||||||||
| RecordTypeSymbol recordType = (RecordTypeSymbol) typeDescriptor; | ||||||||
| return new ArrayList<>(recordType.fieldDescriptors().values()); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| return Collections.emptyList(); | ||||||||
| } | ||||||||
|
|
||||||||
| private void validateKeyField(SpecificFieldNode specificFieldNode) { | ||||||||
|
|
||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra empty lines.