-
Notifications
You must be signed in to change notification settings - Fork 62
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
Update ServiceValidator.java #2105
Conversation
Added compile-time validation in the GraphQL compiler plugin to ensure stub entities annotated with @subgraph:Entity`only contain key fields. Non-key fields are now identified and trigger a diagnostic error during validation. The implementation includes extracting key fields from the annotation and validating record fields against these keys.
|
Quality Gate passedIssues Measures |
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()); | ||
continue; |
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.
Incorrect indentation. Check other places as well.
} | ||
SpecificFieldNode specificFieldNode = (SpecificFieldNode) fieldNode; | ||
Node fieldNameNode = specificFieldNode.fieldName(); | ||
|
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.
IdentifierToken fieldNameToken = (IdentifierToken) fieldNameNode; | ||
String fieldName = fieldNameToken.text().trim(); | ||
if (KEY.equals(fieldName)) { | ||
validateKeyField(specificFieldNode); | ||
|
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.
|
||
|
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.
private void validateFieldsAgainstKeys(List<String> keyFields, Location entityLocation) { | ||
for (RecordFieldSymbol recordField : getRecordFields()) { | ||
String fieldName = recordField.getName().orElse(""); | ||
|
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 new lines. Check other places as well.
String fieldName = recordField.getName().orElse(""); | ||
|
||
if (!keyFields.contains(fieldName)) { | ||
addDiagnostic(CompilationDiagnostic.INVALID_ENTITY_FIELD, entityLocation, fieldName); |
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.
Diagnostic CompilationDiagnostic.INVALID_ENTITY_FIELD
should be defined in CompilationDiagnostic
.
} | ||
|
||
private void validateFieldsAgainstKeys(List<String> keyFields, Location entityLocation) { | ||
for (RecordFieldSymbol recordField : getRecordFields()) { |
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.
Undefined function getRecordFields()
?
List<String> keyFields = new ArrayList<>(); | ||
if (specificFieldNode.valueExpr().isPresent()) { | ||
ExpressionNode valueNode = specificFieldNode.valueExpr().get(); | ||
|
||
if (valueNode.kind() == SyntaxKind.LIST_CONSTRUCTOR) { | ||
for (Node keyField : ((ListConstructorExpressionNode) valueNode).expressions()) { | ||
if (keyField.kind() == SyntaxKind.STRING_LITERAL) { | ||
keyFields.add(keyField.toString().replace("\"", "").trim()); | ||
} | ||
} | ||
} else if (valueNode.kind() == SyntaxKind.STRING_LITERAL) { | ||
|
||
keyFields.add(valueNode.toString().replace("\"", "").trim()); | ||
} | ||
} | ||
return keyFields; | ||
} |
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.
List<String> keyFields = new ArrayList<>(); | |
if (specificFieldNode.valueExpr().isPresent()) { | |
ExpressionNode valueNode = specificFieldNode.valueExpr().get(); | |
if (valueNode.kind() == SyntaxKind.LIST_CONSTRUCTOR) { | |
for (Node keyField : ((ListConstructorExpressionNode) valueNode).expressions()) { | |
if (keyField.kind() == SyntaxKind.STRING_LITERAL) { | |
keyFields.add(keyField.toString().replace("\"", "").trim()); | |
} | |
} | |
} else if (valueNode.kind() == SyntaxKind.STRING_LITERAL) { | |
keyFields.add(valueNode.toString().replace("\"", "").trim()); | |
} | |
} | |
return keyFields; | |
} | |
List<String> keyFields = new ArrayList<>(); | |
if (specificFieldNode.valueExpr().isPresent()) { | |
ExpressionNode valueNode = specificFieldNode.valueExpr().get(); | |
if (valueNode.kind() == SyntaxKind.LIST_CONSTRUCTOR) { | |
for (Node keyField : ((ListConstructorExpressionNode) valueNode).expressions()) { | |
if (keyField.kind() == SyntaxKind.STRING_LITERAL) { | |
keyFields.add(keyField.toString().replace("\"", "").trim()); | |
} | |
} | |
} else if (valueNode.kind() == SyntaxKind.STRING_LITERAL) { | |
keyFields.add(valueNode.toString().replace("\"", "").trim()); | |
} | |
} | |
return keyFields; | |
} |
hi @Codewithsumeet ! are you still working on this? |
Sorry, im not working on this |
Added compile-time validation in the GraphQL compiler plugin to ensure stub entities annotated with @subgraph:Entity`only contain key fields. Non-key fields are now identified and trigger a diagnostic error during validation. The implementation includes extracting key fields from the annotation and validating record fields against these keys.
Purpose
Fixes: #4816
Examples
Checklist
commons
package changes (if there are any, please update the GraphQL version in GraphQL tools and Ballerina dev tools)compiler
package changes (if there are any, please update the GraphQL version in Ballerina dev tools)