Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bugfix: Fix list reference behavior in RequestPatternBuilder.like()#2897

Open
narcoticfresh wants to merge 2 commits intowiremock:masterfrom
narcoticfresh:fix-list-references
Open

Bugfix: Fix list reference behavior in RequestPatternBuilder.like()#2897
narcoticfresh wants to merge 2 commits intowiremock:masterfrom
narcoticfresh:fix-list-references

Conversation

@narcoticfresh
Copy link

@narcoticfresh narcoticfresh commented Nov 11, 2024

References

I recently was very confused when I did this:

RequestPattern requestPatternA =
    RequestPatternBuilder.newRequestPattern()
        .withRequestBody(WireMock.equalTo("foo"))
        .build();

RequestPattern requestPatternB =
  RequestPatternBuilder.like(requestPatternA)
    .withRequestBody(WireMock.equalTo("bar"))
    .build();

What happens is, is that now both RequestPattern instances contain the same ContentPattern instances.

image

Now, I think this is counter intuitive - I would expect that any changes requestPatternB are not reflecting back to requestPatternA after using like(). Would you share that assumption?

This pull request changes the current behavior by making sure that new lists are created in like(), thus not referencing the lists in requestPatternA anymore.

Submitter checklist

  • Recommended: Join WireMock Slack to get any help in #help-contributing or a project-specific channel like #wiremock-java
  • The PR request is well described and justified, including the body and the references
  • The PR title represents the desired changelog entry
  • The repository's code style is followed (see the contributing guide)
  • Test coverage that demonstrates that the change works as expected
  • For new features, there's necessary documentation in this pull request or in a subsequent PR to wiremock.org

@narcoticfresh narcoticfresh requested a review from a team as a code owner November 11, 2024 13:21
@leeturner
Copy link
Contributor

Many thanks for this PR. At the moment this is a breaking change for all the people who might be relying on the existing functionality. Is there any way to do this without it being a breaking change?

@narcoticfresh
Copy link
Author

@leeturner
i fully understand the point - although it's a "fix", it changes behavior and breaks bc.

i think one may decide (i'm not sure how those decisions are formed within the wiremock project) which one should be the "correct" behavior of RequestPatternBuilder.like().

if it shall be changed (and thus only then it will be behaving like widely used form of the Builder pattern and immutability), then i guess it would be best to document it and just change it with the next major release.

if that is not desired, another approach could be to just introduce for example RequestPatternBuilder.copyOf(RequestPatternBuilder) which then does this "real" property transfer.

let me know if i should add this to the pr..

@leeturner
Copy link
Contributor

At the moment I think the copyOf function would be a good option while we are maintaining the 3.x.x release. We can add this to the backlog for version 4 where we are planning a number of breaking changes (new java baseline etc)

@tomakehurst
Copy link
Member

Could we make the name explicitly indicate that the body patterns would be merged rather than replaced? Or add an overload of like() taking a parameter to indicate this?

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

Successfully merging this pull request may close these issues.

3 participants