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

BaseBuilder not cloned properly #2782

Open
hosuaby opened this issue Feb 23, 2025 · 0 comments
Open

BaseBuilder not cloned properly #2782

hosuaby opened this issue Feb 23, 2025 · 0 comments

Comments

@hosuaby
Copy link
Contributor

hosuaby commented Feb 23, 2025

@edudar @vitalijr2

Guys, I have a serious question for ya. I am working on a new feign-oauth2 module now and having an issue with current implementation of BaseBuilder. If I clone the builder using method Object#clone and then add an request or response interceptors, those interceptors are simply forgotten. Here an example:

B enrichedBuilder = (B) builder.clone();
return enrichedBuilder
        .requestInterceptor(new AuthenticationInterceptor()); // <-- this interceptor is lost

Why does it happens:

  1. BaseBuilder has the private field B thisB pointing to itself.
  2. When builder is cloned, the value of that field is copied as is, and continue to point on the object it was cloned from.
  3. The references requestInterceptors and responseInterceptors point to the new ArrayList instances, because their values are initialized outside constructor.
  4. Method BaseBuilder#requestInterceptor adds an interceptor into the list from the new builder object, but returns an old builder then:
  /** Adds a single request interceptor to the builder. */
  public B requestInterceptor(RequestInterceptor requestInterceptor) {
    this.requestInterceptors.add(requestInterceptor);  // <-- interceptor added to new list
    return thisB;    // <-- returns an old builder object without added interceptor
  }

So here my questions:

  1. @edudar why do we even need to catch value of this into instance field thisB? I removed it, by returning this from every builder method and it seems to works. At least, all tests pass.

  2. @vitalijr2 why to use Object#clone that is going against all best practices of programming with modern Java? Using a copy constructor would be a more clean and relyable solution.

I can fix both of those problems, but I need a confirmation from you that I didn't miss something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant