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

🐛 Fix empty code snippet in XML when the match is a key of the node, not an inner value #504

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions demo-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,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
11 changes: 11 additions & 0 deletions rule-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,14 @@
deprecatedIn: "v1.9.0"
removedIn: "v1.16.0"
replacementAPI: "apps/v1"
- 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
Loading