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

Fix: Ensure correct handling of _included entities in Entity class #225

Conversation

lightedcode
Copy link
Contributor

Background

I encountered this issue while calling the roles.list method with the include parameter, specifically:

roles = self.client.v3.roles.list(space_guids=[xxx], types=["space_developer", "space_manager", "space_auditor", "space_supporter"], include=["user", "space"])

for role in roles:
    user = role.user()
    space = role.space()

When iterating over the returned roles object, I attempted to access the included user and space data using dynamically generated methods, such as role.user() and role.space(). However, I noticed that these methods did not return the correct associated data. Instead, both methods returned the same data, corresponding to the last entity processed in the _included dictionary.

Root Cause:

The problematic line of code is:

setattr(self, entity_name, lambda: entity)

Here, the lambda function captures the variable entity by reference. Since entity is updated during each iteration of the loop, all dynamically created methods (e.g., role.user() and role.space()) share the same reference to entity. As a result, after the loop completes, all methods return the data of the last processed entity.

Solution:

To fix this issue, I replaced the lambda with functools.partial, which binds the current value of entity to the dynamically created method. This ensures that each method maintains an independent reference to the correct entity value.

If you have any suggestions, or need additional details about the fix, please feel free to contact me. I'm happy to provide further clarification or make additional changes if necessary.

Looking forward to your feedback and I hope this contribution can help improve the project!

@antechrestos
Copy link
Member

@lightedcode thank for the report!

Great solution indeed. ANother solution would have been (lambda e: lambda :e)(entity) (must be what functools.partial does under the wood.

Thank you

@antechrestos antechrestos merged commit ec7d14c into cloudfoundry-community:main Jan 14, 2025
5 checks passed
@antechrestos
Copy link
Member

@lightedcode released in 1.38.2.

Thank you again

@lightedcode
Copy link
Contributor Author

@lightedcode released in 1.38.2.

Thank you again

You're very welcome! I'm glad to see the fix released in 1.38.2. It was a pleasure contributing. Your solution also gave me new insights and ideas — thank you for sharing it!

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.

2 participants