Skip to content

Conversation

@bmuschko
Copy link
Contributor

What's changed?

The org.apache.http.HttpResponse in HttpClient 4.x needs to be migrated to org.apache.hc.core5.http.ClassicHttpResponse in HttpClient 5.x. The ClassicHttpResponse provides some backward-compatibility to the HttpClient 4.x API.

We already wanted to make that switch in our recipe list via

- org.openrewrite.java.ChangeType:
    oldFullyQualifiedTypeName: org.apache.http.HttpRequest
    newFullyQualifiedTypeName: org.apache.hc.core5.http.ClassicHttpRequest
- org.openrewrite.java.ChangeType:
    oldFullyQualifiedTypeName: org.apache.http.HttpResponse
    newFullyQualifiedTypeName: org.apache.hc.core5.http.ClassicHttpResponse
- org.openrewrite.java.ChangeType:
    oldFullyQualifiedTypeName: org.apache.http.client.ResponseHandler
    newFullyQualifiedTypeName: org.apache.hc.core5.http.io.HttpClientResponseHandler

but that was listed after

- org.openrewrite.java.ChangePackage:
    oldPackageName: org.apache.http
    newPackageName: org.apache.hc.core5.http

so it didn't have any more effect as org.apache.http.HttpResponse was already changed to org.apache.hc.core5.http.HttpResponse.

What's your motivation?

The recipe used to change org.apache.http.HttpResponse to org.apache.hc.core5.http.HttpResponse via the following rule:

- org.openrewrite.java.ChangePackage:
    oldPackageName: org.apache.http
    newPackageName: org.apache.hc.core5.http

The new class doesn't provide the method getEntity() anymore. It has been moved to HttpEntityContainer#getEntity(). Accessing the entity requires a ClassicHttpResponse or ClosableHttpResponse.

As a result the end user experiences a compilation issues, as the method cannot be resolved.

Anything in particular you'd like reviewers to focus on?

Given the changed order of execution in the long list of recipes had an impact on other recipes, namely NewStatusLine and NewRequestLine. The description of NewStatusLine says that it works based on the HttpClient 4.x API so the method matcher needs to run before org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping. The NewRequestLine changes a HttpClient 5.x API so it needs to run after org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping.

I noticed that we sometimes conflate recipes for HttpClient 4.x to HttpClient 5.x migration with changes that just apply to the HttpClient 5.x API but aims to fix deprecated APIs. Would it make sense to separate those concerns?

@bmuschko bmuschko requested a review from timtebeek August 26, 2025 15:17
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Aug 26, 2025
@bmuschko bmuschko requested review from Copilot and steve-aom-elliott and removed request for Copilot August 26, 2025 15:17
@bmuschko bmuschko added bug Something isn't working recipe labels Aug 26, 2025
@bmuschko bmuschko marked this pull request as draft August 26, 2025 15:17
@bmuschko bmuschko changed the title Bm/migrate httpresponse Migrate org.apache.http.HttpResponse to org.apache.hc.core5.http.ClassicHttpResponse Aug 26, 2025
…sponseTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Comment on lines +639 to +647
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.http.HttpRequest
newFullyQualifiedTypeName: org.apache.hc.core5.http.ClassicHttpRequest
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.http.HttpResponse
newFullyQualifiedTypeName: org.apache.hc.core5.http.ClassicHttpResponse
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.http.client.ResponseHandler
newFullyQualifiedTypeName: org.apache.hc.core5.http.io.HttpClientResponseHandler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The placement for these unfortunately still feel a bit strange (not sure that a blank line above these would really help, but it would at least separate it from the block of ChangeTypes that are trying to fix things that ended up in org.apache.hc.client5.http that shouldn't have been there).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new line and comment.

Comment on lines -739 to +746
methodPatternChain: ['org.apache.hc.core5.http.HttpResponse getStatusLine()', 'org.apache.hc.core5.http.message.StatusLine getStatusCode()']
methodPatternChain: ['org.apache.http.HttpResponse getStatusLine()', 'org.apache.http.StatusLine getStatusCode()']
newMethodName: getCode
- org.openrewrite.java.SimplifyMethodChain:
methodPatternChain: ['org.apache.hc.core5.http.HttpResponse getStatusLine()', 'org.apache.hc.core5.http.message.StatusLine getReasonPhrase()']
methodPatternChain: ['org.apache.http.HttpResponse getStatusLine()', 'org.apache.http.StatusLine getReasonPhrase()']
newMethodName: getReasonPhrase
- org.openrewrite.java.SimplifyMethodChain:
methodPatternChain: [ 'org.apache.hc.core5.http.HttpResponse getStatusLine()', 'org.apache.hc.core5.http.message.StatusLine getProtocolVersion()' ]
methodPatternChain: [ 'org.apache.http.HttpResponse getStatusLine()', 'org.apache.http.StatusLine getProtocolVersion()' ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate that visually it's taking it from a "working" state with a method it knows to a "broken" state with an unknown method until lines 642-644 run at some point later

- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_DeprecatedMethods
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_TimeUnit
- org.openrewrite.apache.httpclient5.StatusLine
- org.openrewrite.apache.httpclient5.NewRequestLine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it make sense to give NewRequestLine a similar treatment for consistency? You changed NewStatusLine to operate on 4.x class name to bring it to 5.x class: should NewRequestLine operate on 4.x class name and bring it to 5.x class as well? I know the description would have to be altered a bit, but I feel like we should try to be consistent either direction: either migrate the package and then fix the class and methods or fix the methods and then later migrate the class.

Copy link
Contributor Author

@bmuschko bmuschko Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed another change. From your comments, here's what I understood.

  • NewRequestLine and NewStatusLine should both work based on the HttpClient 4.x API.
  • org.openrewrite.apache.httpclient5.StatusLine needs to fully work based on the HttpClient 4.x API.
  • Move the migration logic of org.openrewrite.apache.httpclient5.StatusLine before org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping so that it can work based on the HttpClient 4.x API before packages are migrated to the HttpClient 5.x.

Let me know if I am missing something.

@bmuschko bmuschko marked this pull request as ready for review September 2, 2025 21:47
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Sep 3, 2025
@bmuschko
Copy link
Contributor Author

bmuschko commented Sep 5, 2025

@timtebeek Looks like we are good with merging the change?

@timtebeek timtebeek merged commit 973f2f0 into main Sep 8, 2025
2 checks passed
@timtebeek timtebeek deleted the bm/migrate-httpresponse branch September 8, 2025 09:28
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Sep 8, 2025
@timtebeek
Copy link
Member

Yes all good; and know that you're welcome to merge after approval; sometimes is isn't clear if any further work is planned or expected beyond the approval, so then I hold off on an immediate merge. You'd have been welcome to merge as well. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working recipe

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants