-
Notifications
You must be signed in to change notification settings - Fork 77
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
Simplify MediaType.parse
Calls
#404
Conversation
src/main/java/org/openrewrite/java/spring/http/SimplifyMediaTypeParseCalls.java
Outdated
Show resolved
Hide resolved
The reason why it's returning P.D.: Also added spring-core in the classpath of the test, since it's needed as a dependency of spring-web for properly parsing |
if ("application/json".equals(test.getValue())) { | ||
return JavaTemplate.builder("MediaType.APPLICATION_JSON") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perhaps too limiting; we already have a recipe to replace literals with constants:
A comment there was logged as an improvement to pick up cases where we (after the above recipe) pass a constant into a parse method:
If you ask me this implementation should detect String constants passed into parse, and replace those with the pre existing MediaType constants. Open to discuss.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so just to make sure I understand this correctly instead of detecting something like "application/json"
like we are doing now; We should instead detect constants and make the replacement then?
Are there are other MediaType
constants this recipe should try covering also? Instead of just looking for APPLICATION_JSON
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes when the value feeding into the parse call is a constant defined on MediaType
then you want to replace the whole method invocation with the sister constant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so
-MediaType.parseMediaType(MediaType.APPLICATION_JSON_VALUE)
+MediaType.APPLICATION_JSON
notice how we drop the method call, and replace it with a "sister constant" that lacks the _VALUE
suffix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the above is from memory, so be sure to check what's available on the MediaType class in your IDE, but that's the rough outline of what I would expect. And we should not limit ourselves to APPLICATION_JSON
, instead just any constants that follow that pattern (without naming each explicitly).
Ok I updated this recipe to work not just with |
Make a small change not to use a hardcoded list of variables, instead using the convention that if there's a String constant defined on MediaType that there's a matching constant that lacks the |
What's changed?
This PR introduces a recipe to replaces uses of
MediaType.parse("application/json")
withMediaType.APPLICATION_JSON
.What's your motivation?
Fixes #337
Anything in particular you'd like reviewers to focus on?
Currently when the recipe runs the
methodType
forMediaType.parse
is null which is causing the recipe to stop running early. I think it is because I am not including the correct classpath. I have tried usingclasspath("spring-web")
but it kept telling me that this was not a valid resource name. I'm confused why it would tell me that since I've seenspring-web
used in other tests in rewrite-spring.Anyone you would like to review specifically?
@timtebeek
Checklist
./gradlew licenseFormat