Skip to content

Commit

Permalink
Merge pull request #521 from kalaiyarasiganeshalingam/2.6.1
Browse files Browse the repository at this point in the history
Fix NPE from the compiler plugin
  • Loading branch information
kalaiyarasiganeshalingam authored Aug 10, 2023
2 parents 77af8c2 + 150232b commit 335415d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ public void testInvalidChildAnnotation() {
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
"invalid annotation attachment: child record does not allow name annotation");
}

@Test
public void testInvalidChildAnnotation1() {
DiagnosticResult diagnosticResult = loadPackage("sample12").getCompilation().diagnosticResult();
List<Diagnostic> errorDiagnosticsList = diagnosticResult.diagnostics().stream()
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(errorDiagnosticsList.size(), 1);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(),
"invalid annotation attachment: child record does not allow name annotation");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "xmldata_test"
name = "sample12"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2023, WSO2 LLC. (https://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/xmldata;
import ballerina/io;

@xmldata:Name {
value: "appointment"
}
type Appointment record {
string firstName;
string lastName;
string email;
string age;
};

@xmldata:Name {
value: "appointments"
}
type Appointments record {
Appointment[] appointment;
};

public function main() returns error? {
xml xmlPayload = xml `<appointments><appointment><firstName>John</firstName><lastName>Doe</lastName><email>[email protected]</email><age>28</age></appointment><appointment><firstName>John</firstName><lastName>Doe</lastName><email>[email protected]</email><age>28</age></appointment></appointments>`;

map<json> result2 = check xmldata:fromXml(xmlPayload);
io:println(result2);

// Sample
Appointments result3 = check xmldata:fromXml(xmlPayload);
io:println(result3);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public void perform(SyntaxNodeAnalysisContext ctx) {
}
}
for (String recordName : this.recordNamesUsedInFunction) {
validateRecord(ctx, this.records.get(recordName));
if (this.records.containsKey(recordName)) {
validateRecord(ctx, this.records.get(recordName));
}
}
}

Expand Down Expand Up @@ -129,7 +131,7 @@ private boolean isValidFunctionName(ExpressionNode expressionNode) {

private void addRecordName(TypeDescriptorNode typeDescriptor) {
if (typeDescriptor.kind() == SyntaxKind.SIMPLE_NAME_REFERENCE) {
String returnTypeName = typeDescriptor.toString().trim();
String returnTypeName = ((SimpleNameReferenceNode) typeDescriptor).name().text().trim();
if (!this.recordNamesUsedInFunction.contains(returnTypeName)) {
this.recordNamesUsedInFunction.add(returnTypeName);
}
Expand All @@ -140,7 +142,7 @@ private void processTypeDefinitionNode(TypeDefinitionNode typeDefinitionNode) {
Node typeDescriptor = typeDefinitionNode.typeDescriptor();
if (typeDescriptor instanceof RecordTypeDescriptorNode) {
RecordTypeDescriptorNode recordTypeDescriptorNode = (RecordTypeDescriptorNode) typeDescriptor;
Record record = new Record(typeDefinitionNode.typeName().text(), typeDefinitionNode.location());
Record record = new Record(typeDefinitionNode.typeName().text().trim(), typeDefinitionNode.location());
typeDefinitionNode.metadata().ifPresent(metadataNode -> {
NodeList<AnnotationNode> annotations = metadataNode.annotations();
for (AnnotationNode annotationNode : annotations) {
Expand All @@ -154,13 +156,14 @@ private void processTypeDefinitionNode(TypeDefinitionNode typeDefinitionNode) {
if (field instanceof RecordFieldNode) {
RecordFieldNode recordFieldNode = (RecordFieldNode) field;
type = recordFieldNode.typeName();
} else {
processFieldType(type, record);
} else if (field instanceof RecordFieldWithDefaultValueNode) {
RecordFieldWithDefaultValueNode recordFieldNode = (RecordFieldWithDefaultValueNode) field;
type = recordFieldNode.typeName();
processFieldType(type, record);
}
processFieldType(type, record);
}
this.records.put(record.getName(), record);
this.records.put(record.getName().trim(), record);
}
}

Expand Down Expand Up @@ -223,7 +226,7 @@ private void validateRecord(SyntaxNodeAnalysisContext ctx, Record record) {
if (!this.validatedRecords.contains(childRecordName)) {
Record childRecord = this.records.get(childRecordName);
validateRecord(ctx, childRecord);
if (childRecord.hasNameAnnotation() && !recordNamesUsedInFunction.contains(childRecordName)) {
if (childRecord.hasNameAnnotation() && !recordNamesUsedInFunction.contains(childRecordName.trim())) {
reportDiagnosticInfo(ctx, childRecord.getLocation(), DiagnosticsCodes.XMLDATA_103);
}
}
Expand Down

0 comments on commit 335415d

Please sign in to comment.