python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

`TupleKey` should support `relation=None`

Open jnu opened this issue 1 year ago • 4 comments

Checklist

  • [X] I have looked into the README and have not found a suitable solution or answer.
  • [X] I have looked into the documentation and have not found a suitable solution or answer.
  • [X] I have searched the issues and have not found a suitable solution or answer.
  • [X] I have upgraded to the latest version of OpenFGA and the issue still persists.
  • [X] I have searched the Slack community and have not found a suitable solution or answer.
  • [X] I agree to the terms within the OpenFGA Code of Conduct.

Description

The docs describe how to use the read() method with a TupleKey with no relation set to get a list of all relations a user has with an object. Currently the TupleKey object validation prevents the relation from being None.

Expectation

Not to throw an error when omitting relation or setting to None.

Reproduction

Essentially the code from the docs:

body = TupleKey(
            user="user:bob",
            object="document:123",

)

Working around the validation and submitting the query confirms the behavior describes from the docs (i.e., listing the relations) works as expected:

Issuing a query with a workaround:

body = TupleKey(
            user="user:bob",
            relation="",
            object="document:123",

)
body._relation = None

# call `read(body)` ...

OpenFGA SDK version

0.4.0 (but the code in main appears broken)

OpenFGA version

v1.5.0

SDK Configuration

[default options]

Logs

No response

References

No response

jnu avatar Jun 05 '24 16:06 jnu

Hi @jnu - Thanks for raising this! I will transfer this to be an issue in the docs, the SDK docs here are more up to date

https://github.com/openfga/python-sdk?tab=readme-ov-file#read-relationship-tuples

And you would want to do something like

# from openfga_sdk import OpenFgaClient, ReadRequestTupleKey

# Initialize the fga_client
# fga_client = OpenFgaClient(configuration)

# Find all relationship tuples where a certain user has a relationship as any relation to a certain document
body = ReadRequestTupleKey(
    user="user:81684243-9356-4421-8fbf-a4f8d36aa31b",
    object="document:roadmap",
)

response = await fga_client.read(body)
# response = ReadResponse({"tuples": [Tuple({"key": TupleKey({"user":"...","relation":"...","object":"..."}), "timestamp": datetime.fromisoformat("...") })]})

rhamzeh avatar Jun 06 '24 22:06 rhamzeh

Before we move it, can you clarify:

0.4.0 (but the code in main appears broken)

Is there something else not working for you?

rhamzeh avatar Jun 06 '24 22:06 rhamzeh

I also got tripped up by this. (I looked for a quick way to submit a PR to update the docs https://github.com/openfga/openfga.dev/edit/main/docs/content/interacting/relationship-queries.mdx but was unable to find where ReadRequestViewer is defined)

LLay avatar Mar 10 '25 20:03 LLay

@rhamzeh sorry, I never saw your question. Everything else was working fine, and the code works fine when you work around the initial validation for relation being a string, as I described.

It sounds like this is still an issue given @LLay's comment, though I have not personally checked recently.

jnu avatar Mar 10 '25 22:03 jnu