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

Update ServiceValidator.java #2105

Closed

Conversation

Codewithsumeet
Copy link

@Codewithsumeet Codewithsumeet commented Nov 22, 2024

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

  • Linked to an issue
  • Updated the changelog
  • Added tests
  • Updated the spec
  • Checked native-image compatibility
  • No commons package changes (if there are any, please update the GraphQL version in GraphQL tools and Ballerina dev tools)
  • No compiler package changes (if there are any, please update the GraphQL version in Ballerina dev tools)

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.
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

Comment on lines +189 to +199
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;
Copy link
Contributor

@DimuthuMadushan DimuthuMadushan Nov 22, 2024

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();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

IdentifierToken fieldNameToken = (IdentifierToken) fieldNameNode;
String fieldName = fieldNameToken.text().trim();
if (KEY.equals(fieldName)) {
validateKeyField(specificFieldNode);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines +214 to +215


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

private void validateFieldsAgainstKeys(List<String> keyFields, Location entityLocation) {
for (RecordFieldSymbol recordField : getRecordFields()) {
String fieldName = recordField.getName().orElse("");

Copy link
Contributor

@DimuthuMadushan DimuthuMadushan Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

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);
Copy link
Contributor

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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined function getRecordFields() ?

Comment on lines +230 to +246
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
}

@DimuthuMadushan
Copy link
Contributor

hi @Codewithsumeet ! are you still working on this?

@Codewithsumeet
Copy link
Author

Sorry, im not working on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce a compile-time error when a stub entity contributes additional fields to a subgraph
3 participants