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

--replace-type is not mapping packages correctly #688

Closed
1 of 5 tasks
rkoehl05 opened this issue Aug 8, 2023 · 8 comments · Fixed by #710
Closed
1 of 5 tasks

--replace-type is not mapping packages correctly #688

rkoehl05 opened this issue Aug 8, 2023 · 8 comments · Fixed by #710
Labels

Comments

@rkoehl05
Copy link

rkoehl05 commented Aug 8, 2023

Description

I am trying to mock an interface that requires multiple --replace-type inputs. The target type aliases for these types reside in separate packages but the source internal types are all in the same package. This causes the generate logic to choose the wrong alias when building the mock.

The issue seems to be caused by addPackageImportWithName only looking at the the package portion of the from replaceType here. It should probably scan all the replace type inputs first to see if there is an explicit type-level mapping and then a follow up scan for package-level mappings.

Mockery Version

2.30.1, 2.32.3

Golang Version

1.20

Installation Method

  • Binary Distribution
  • Docker
  • brew
  • go install
  • Other: [specify]

Steps to Reproduce

I set up a reproducer repo here. You can run go generate. in it to produce the broken mock.

Expected Behavior

The DeleteResponse type should use the blob alias, not blockblob

Actual Behavior

DeleteResponse is incorrectly referenced using the blockblob alias. See here for an example.

@rkoehl05 rkoehl05 changed the title --replace-type does not mapping packages correctly --replace-type is not mapping packages correctly Aug 8, 2023
@LandonTClipp
Copy link
Collaborator

Hi thanks for the report, I'll be looking through this to get a better understanding.

@vitaly-eureka-security
Copy link

Hi. I face a similar issue:

packages:
    interfaces:
      IBlockBlobClient:
        config:
          replace-type:
            - 'github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/internal/generated.BlockBlobClientCommitBlockListResponse=blockblob:github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob.UploadStreamResponse'
            - 'github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/internal/generated.BlobClientDeleteResponse=blob:github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob.DeleteResponse'

Instead of blob.DeleteResponse the generated mock contains blockblob.DeleteResponse

@LandonTClipp
Copy link
Collaborator

It seems the issue here is that it's not looking at the entire path of the replacement, just the path itself. It looks only at github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/internal/generated and not the name of the struct, so it's selecting the first one it comes across when it should be selecting the second? So instead we might need something like

if path == from.pkg && name == from.name {

in the linked piece of code. Let me know if you think that's the solution, but that's what I'm gathering

@vitaly-eureka-security
Copy link

Yes. It seems that addPackageImportWithName should consider the original type name and not just the original package when deciding what replacement rule to apply.

@rkoehl05
Copy link
Author

That is what I was considering as well but it wouldn't handle the case where your replace-type is just a package to package mapping.

I was thinking we could do two passes where the first attempt will try to map with pkg & name. The second pass would just use the pkg as long as name is not set.

@LandonTClipp
Copy link
Collaborator

LandonTClipp commented Aug 16, 2023

I think you're right we'll have to do two passes to get the most specific type replacement possible. I think this could have been resolved if the parameter itself was a mapping instead of a list of strings. If you want to implement the two pass suggestion I'll accept it, but I do think we need a better way to model this.

Edit: on second thought, you don't need two passes. You just have to keep track of specificity and simply select the most specific entry you found through the loop.

@LandonTClipp
Copy link
Collaborator

@RangelReale since you were the one who made this feature, I'd give you first dibs if you have the time/energy to do this? No worries at all if you can't, just thought I'd ask 😄

@RangelReale
Copy link
Contributor

Hmm I tried some initial fixes but it was more complex than I thought, probably would need to refactor the addPackageImportWithName to know the object name, but in reality it doesn't need it.

I'm a little busy this week, won't be able to focus too much on it, if you have a solution in mind and want to implement it, go ahead!

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

Successfully merging a pull request may close this issue.

4 participants