Skip to content

3.0 Upgrade Recipe: Spring MVC and WebFlux URL matching changes [PathMatchConfigurer] #248

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

Closed
10 tasks
Tracked by #206
fabapp2 opened this issue Jul 21, 2022 · 7 comments
Closed
10 tasks
Tracked by #206
Labels
3.0.0 Spring Boot 3.0.0 archived
Milestone

Comments

@fabapp2
Copy link
Contributor

fabapp2 commented Jul 21, 2022

labels: ["type: enhancement","good first issue","upgrade:boot-recipe","3.0.0"]

What needs to be done

An automated migration recipe for remediation possibility 4 of Spring MVC and WebFlux URL matching changes should be created.

Reset behavior to prior 3.0 by configuring PathMatchConfigurer (option 4 in #522)

This automated migration recipe should add the WebMvcConfigurer to reset the behavior to 2.7.

Release Note

As of Spring Framework 6.0, the trailing slash matching configuration option has been deprecated and its default value set to false. This means that previously, the following controller would match both "GET /some/greeting" and "GET /some/greeting/":

@RestController
public class MyController {

  @GetMapping("/some/greeting")
  public String greeting {
    return "Hello";
  } 

}

As of this Spring Framework change, "GET /some/greeting/" doesn?t match anymore by default.

Developers should instead configure explicit redirects/rewrites through a proxy, a Servlet/web filter, or even declare the additional route explicitly on the controller handler (like @GetMapping("/some/greeting", "/some/greeting/") for more targeted cases.

Until your application fully adapts to this change, you can change the default with the following global configuration:

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
      configurer.setUseTrailingSlashMatch(true);
    }
  }
}

Note
We want to contribute as many migrations as possible to OpenRewrite.
Therefore it's desired to implement this recipe as a plain OpenRewrite recipe when possible.
See OpenRewrite docs and Using OpenRewrite recipes

Condition

Any class annotated with @RestController was found.

Description

There's more than one way to deal with the change of Spring MVC and WebFlux URL matching changes.
This issue should provide a recipe to add a WebMvcConfigurer as described in the Release Notes.

Things to keep in mind:

  • The added class must have a unique name
  • The correct package must be chosen so that Boot can pick it up.
  • The recipe should be provided using plain OpenRewrite
  • A dedicated YAML recipe declaration should be provided to reference this recipe from the report section (3.0 Upgrade Report: Spring MVC and WebFlux URL matching changes #522)
  • The recipe file name should be sbu30-248-add-PathMatchConfigurer

Report Section Issue

#522

DoD

  • Action implemented
    • Description provided
    • Tests (single and multi-module)
  • Condition implemented / reused
    • Description provided
    • Tests (single and multi-module)
  • Recipe YAML added
    • Test
    • Integration test (single and multi-module)
  • Report Section YAML references this migration recipe

Additional Resources & Information

TODO for template, link to:

  • Example
  • Testing Actions
  • Testing Conditions
  • Testing Recipes
  • Integration Tests
@fabapp2 fabapp2 changed the title Spring MVC and WebFlux URL matching changes 3.0.0-M4 Spring MVC and WebFlux URL matching changes Jul 21, 2022
@fabapp2 fabapp2 added this to the v0.13.0 milestone Sep 27, 2022
@fabapp2 fabapp2 changed the title 3.0.0-M4 Spring MVC and WebFlux URL matching changes 3.0 Upgrade Recipe: Spring MVC and WebFlux URL matching changes Nov 3, 2022
@fabapp2 fabapp2 changed the title 3.0 Upgrade Recipe: Spring MVC and WebFlux URL matching changes 3.0 Upgrade Recipe: Spring MVC and WebFlux URL matching changes - 1 Nov 8, 2022
@fabapp2 fabapp2 changed the title 3.0 Upgrade Recipe: Spring MVC and WebFlux URL matching changes - 1 3.0 Upgrade Recipe: Spring MVC and WebFlux URL matching changes [PathMatchConfigurer] Nov 8, 2022
@ishu-thakur
Copy link
Contributor

@fabapp2 sorry for the late reply , I was going through the documentation you provided , so we need to enable Trailing Slash Match inside the WebMVCAutoConfiguration ?

@fabapp2
Copy link
Contributor Author

fabapp2 commented Nov 10, 2022

@ishu-thakur correct. it's basically adding the given source code to the ProjectContext (to topmost application modules).
I wouldn't mind if you so it with SBM

projectContext.getApplicationModules().getTopmostApplicationModules().stream()
                .forEach(m -> {
                    String basePackage = m.getMainJavaSourceSet().getJavaSourceLocation().getPackageName();
                    String code = renderJavaCode(basePackage);
                    m.getMainJavaSourceSet().addJavaSource(projectContext, code, basePackage);
                });

Simplified as it should have a check for the unlikely case that a class with the chosen name already exists in the package and choosing a different name in this case.
You can use freemarker to render the code or just plain java string replacement, up to you.

The YAML recipe should be declared in a new file sbu30-248-add-PathMatchConfigurer.yaml and have the name sbu30-248-add-PathMatchConfigurer it's already linked by #536, so you don't need to do anything in boot-new-report.yaml.

@ishu-thakur
Copy link
Contributor

@fabapp2 for clarity I want to ask you that

  1. I need to add the sbu30-248-add-PathMatchConfigurer.yaml under recipes inside the sbm-reciepes-boot-upgrade ?
  2. To add the given above code to ProjectContext inside the apply method of what Topmost applications modules ( Do you mean modules with sbm in their name ?
  3. And I didn't quite understand what to do with freemarker ?
    Sorry, I am just new so have questions to get clarity before I proceed further :)

@fabapp2
Copy link
Contributor Author

fabapp2 commented Nov 16, 2022

Hi @ishu-thakur

Great questions!

@fabapp2 for clarity I want to ask you that

  1. I need to add the sbu30-248-add-PathMatchConfigurer.yaml under recipes inside the sbm-reciepes-boot-upgrade ?

That's correct, actually this changed in the branch webapp-demo where migrations for Spring Boot 3 related migrations were moved to src/main/resources/recipes/27_30/migration and the YAML for the report to src/main/resources/recipes/27_30/report. Not sure when exactly I'll be able to merge this back, so you might want to use the dir you mentioned for now.

  1. To add the given above code to ProjectContext inside the apply method of what Topmost applications modules ( Do you mean modules with sbm in their name ?

The api to access "topmost" modules, actually modules that will create a deployable (e.g. a web application, or a shell application), these are currently the modules that no other module in the scanned project depends on.
They can be accessed like so:

projectContext.getApplicationModules().getTopmostApplicationModules();
  1. And I didn't quite understand what to do with freemarker ?

For "migrations", called "recipe" in the issue (I should change this), there's nothing to do with freemarker.
The idea is to have a report section in the generated report (see #522 ) where freemarker might be useful to render dynmic parts of the report section. The report section allows applying recipes to the application by pressing a button (if applicable, not all changes can be automated). See here:

image

You might want to look into sbu30-report.yaml to see how this migration gets (already) linked in the report.

Sorry, I am just new so have questions to get clarity before I proceed further :)

Totally fine. Sorry for my late reply.
Hope this was helpful.

@fabapp2
Copy link
Contributor Author

fabapp2 commented Dec 1, 2022

Hi @ishu-thakur,

any news on this? Let me know if I can help.

@fabapp2 fabapp2 modified the milestones: v0.13.0, v0.14.0 Dec 1, 2022
@ishu-thakur
Copy link
Contributor

Hi @fabapp2 I'm sorry for late reply , I got busy with my work . But if this issue is still open i can start working on it again ?

@fabapp2
Copy link
Contributor Author

fabapp2 commented Feb 11, 2023

Hi @ishu-thakur glad to have you back.
It's actually linked in the 0.13.0 report but nothing's behind it.
So yes, happy if you want to proceed. 👍
Please ping @sanagaraj-pivotal too as I am not very responsive right now.

@fabapp2 fabapp2 modified the milestones: v0.14.0, v0.15.0 Feb 21, 2023
@fabapp2 fabapp2 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2023
@fabapp2 fabapp2 added archived and removed type: enhancement New feature or request good first issue Good for newcomers upgrade:boot-recipe labels Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.0.0 Spring Boot 3.0.0 archived
Projects
None yet
Development

No branches or pull requests

2 participants