Skip to content

Commit

Permalink
🐛 Fix empty code snippet in XML when the match is a key of the node, …
Browse files Browse the repository at this point in the history
…not an inner value (#504)

Fixes #503

---------

Signed-off-by: Pranav Gaikwad <[email protected]>
Co-authored-by: David Zager <[email protected]>
  • Loading branch information
pranavgaikwad and djzager committed Feb 21, 2024
1 parent 2d0febb commit b3b181c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
13 changes: 13 additions & 0 deletions demo-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,19 @@
data: dependency
innerText: "\n junit\n junit\n 4.11\n test\n "
matchingXML: <groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope>
xml-test-key-match:
description: Test code snippets when match is a key of a XML node
category: potential
incidents:
- uri: file:///analyzer-lsp/examples/java/beans.xml
message: The code snippet should point to <beans> in the beans.xml file
codeSnip: " 8 *\n 9 * http://www.apache.org/licenses/LICENSE-2.0\n10 *\n11 * Unless required by applicable law or agreed to in writing, software\n12 * distributed under the License is distributed on an \"AS IS\" BASIS,\n13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n14 * See the License for the specific language governing permissions and\n15 * limitations under the License.\n16 -->\n17 <beans xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"\n18 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n19 xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee \n20 http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd\"\n21 version=\"1.1\" bean-discovery-mode=\"all\">\n22 </beans>\n"
lineNumber: 17
variables:
data: beans
innerText: |2+
matchingXML: ""
errors:
error-rule-001: |-
unable to get query info: yaml: unmarshal errors:
Expand Down
22 changes: 22 additions & 0 deletions examples/java/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* (C) Copyright IBM Corporation 2015.
*
* Licensed 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.
-->
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
6 changes: 5 additions & 1 deletion provider/internal/builtin/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ func (p *builtinServiceClient) Evaluate(ctx context.Context, cap string, conditi
"data": node.Data,
},
}
location, err := p.getLocation(ctx, ab, node.InnerText())
content := strings.TrimSpace(node.InnerText())
if content == "" {
content = node.Data
}
location, err := p.getLocation(ctx, ab, content)
if err == nil {
incident.CodeLocation = &location
lineNo := int(location.StartPosition.Line)
Expand Down
13 changes: 12 additions & 1 deletion rule-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,15 @@
ruleID: python-sample-rule-003
when:
python.referenced:
pattern: "create_custom_resource_definition"
pattern: "create_custom_resource_definition"
- category: potential
description: "Test code snippets when match is a key of a XML node"
message: "The code snippet should point to <beans> in the beans.xml file"
ruleID: xml-test-key-match
when:
builtin.xml:
filepaths:
- beans.xml
namespaces:
b: http://xmlns.jcp.org/xml/ns/javaee
xpath: /b:beans

0 comments on commit b3b181c

Please sign in to comment.