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

Add rule for SourceHttpMessageConverter #176

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,46 @@
url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#web-applications
- title: 'Specific Spring Framwork change'
url: https://github.com/spring-projects/spring-framework/issues/28552

- 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:
jmle marked this conversation as resolved.
Show resolved Hide resolved
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
@@ -1,11 +1,20 @@
package org.konveyor;

import java.beans.IntrospectionException;
import org.springframework.web.client.RestTemplate;

import javax.xml.transform.Source;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {

public static void main(String[] args) throws IntrospectionException {
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");

java.lang.System.getenv();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.konveyor.controller;

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 {

@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() {
// Predefined XML string
String xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><greeting><message>Hello, World!</message><timestamp>%s</timestamp></greeting>";

// Return as Source
return new StreamSource(new StringReader(xmlContent));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.konveyor.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class XmlRestController {

public Source method(@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));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ 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