Skip to content

Commit

Permalink
Add rule for SourceHttpMessageConverter issue
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Manuel Leflet Estrada <jleflete@redhat.com>
  • Loading branch information
jmle committed Nov 29, 2024
1 parent 6c4c4fa commit 0c4ddd0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,45 @@
url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#web-applications


- ruleID: spring-framework-5.x-to-6.0-web-applications-00030
category: mandatory
effort: 1
labels:
- konveyor.io/source=spring5
- konveyor.io/target=spring6+
when:
or:
- and:
- or:
- java.referenced:
pattern: org.springframework.stereotype.Controller
location: ANNOTATION
- java.referenced:
pattern: org.springframework.web.bind.annotation.RestController
location: ANNOTATION
as: class
ignore: true
- java.referenced:
pattern: '* javax.xml.transform.Source'
location: METHOD
filepaths: "{{class.Filepaths}}"
from: class
- or:
- java.referenced:
pattern: 'getForObject(URI,Class<Source>)'
location: METHOD_CALL
- java.referenced:
pattern: 'getForObject(String,Class<Source>,*)'
location: METHOD_CALL
description: "SourceHttpMessageConverter is not configured by default anymore in Spring MVC and RestTemplate"
message: |
`SourceHttpMessageConverter` is not configured by default anymore in Spring MVC and `RestTemplate`.
As a consequence, Spring web applications using `javax.xml.transform.Source` now need to configure
`SourceHttpMessageConverter` explicitly. Note that the order of converter registration is important,
and `SourceHttpMessageConverter` should typically be registered before "catch-all" converters like
`MappingJackson2HttpMessageConverter` for example.
links:
- title: 'Spring 6.0 migration guide'
url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#web-applications
- title: 'GitHub issue: Make SourceHttpMessageConverter optional'
url: https://github.com/spring-projects/spring-framework/issues/29535
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.beans.IntrospectionException;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {
import org.springframework.web.client.RestTemplate;

public static void main(String[] args) throws IntrospectionException {
public class Main {

public static void main(String[] args) throws URISyntaxException {
RestTemplate rest = new RestTemplate();
rest.getForObject(new URI("http://www.example.com/"), Source.class);
rest.getForObject("http://www.example.com/", Source.class);
rest.getForObject("http://www.example.com/", Source.class, "Hey");
}

public Source getXmlData() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;

@Controller
public class XmlController {

@GetMapping("/xml")
@PostMapping("/process-xml")
@ResponseBody
public Source processXml(@RequestBody String xmlInput) {
// For example, log the input or perform some processing here
System.out.println("Received XML: " + xmlInput);

// Assuming we want to respond with a modified version of the XML
String responseXml = "<response><status>Success</status><originalMessage>%s</originalMessage></response>";

// Return the new XML wrapped in a StreamSource
return new StreamSource(new StringReader(responseXml));
}

@GetMapping(value = "/xml/", produces = "application/xml")
public Source getXmlData() {
String xmlData =
"<greeting>\n" +
"<message>Hello, world!</message>\n" +
"</greeting>\n";
// Predefined XML string
String xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><greeting><message>Hello, World!</message><timestamp>%s</timestamp></greeting>";

return new StreamSource(new StringReader(xmlData));
// Return as Source
return new StreamSource(new StringReader(xmlContent));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ tests:
mode: "source-only"
hasIncidents:
exactly: 1

- ruleID: spring-framework-5.x-to-6.0-web-applications-00030
testCases:
- name: tc-1
analysisParams:
mode: "source-only"
hasIncidents:
exactly: 6

0 comments on commit 0c4ddd0

Please sign in to comment.