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 JSON serialization error for UUID primary keys when excluded from list #553

Merged
merged 6 commits into from
Jul 8, 2024

Conversation

alg
Copy link
Contributor

@alg alg commented Jun 28, 2024

Addresses #552

When PK is not listed among fields it needs proper serialization in case it's not of a JSON-serializable type already (for example, UUID).

  • starlette_admin.BaseModelView returns the PK value unchanged (for backwards compatibility)
  • starlette_admin.contrib.sqla.ModelView uses PK field for proper serialization

Copy link
Owner

@jowilf jowilf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, i added some comments. can you take a look?

Comment on lines 885 to 887
async def get_serialized_pk_value(
self, request: Request, obj: Any, action: RequestAction
) -> Any:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The request action is now accessible via request.state.action. I'd prefer new methods to use request.state.action directly

Suggested change
async def get_serialized_pk_value(
self, request: Request, obj: Any, action: RequestAction
) -> Any:
async def get_serialized_pk_value(
self, request: Request, obj: Any
) -> Any:

tests/sqla/test_view_serialization.py Outdated Show resolved Hide resolved
Comment on lines 49 to 72
def req(app: Starlette):
return Request(
{
"app": app,
"router": app.router,
"type": "http",
"headers": [],
}
)


@pytest.mark.asyncio
async def test_ensuring_pk(req: Request, user_view: UserView):
"""
Ensures PK is present in the serialized data and properly serialized as a string.
"""

user_id = uuid.uuid1()
user = User(id=user_id, name="Jack")

data = await user_view.serialize(user, req, RequestAction.LIST)

assert data["id"] == str(user_id)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starlette recommends testclient to avoid creating the request object. I think It's better to simply call the listing API ,which will, in turn, invoke the serialize method. Here is an example ->

async def test_api(client: AsyncClient):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This poses the question, why the serialization is dependent on the HTTP request object? Maybe consider decoupling them via repackaging parts of the request the serialization is dependent on into a separate DTO (the serialization context). This would give you exactly what I've done -- testing serialization thoroughly without executing fake HTTP requests.

I'll see how doing it through testclient looks for now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The request is used to generate some metadata as you can see here

request.url_for(route_name + ":detail", identity=self.identity, pk=pk)

Maybe consider decoupling them via repackaging parts of the request the serialization is dependent on into a separate DTO (the serialization context).

This a good idea, can you open a discussion to present your approach?

Copy link
Contributor Author

@alg alg Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm seeing your commits on this. I was cleaning it up when seen them. )

In fact, I already implemented simple UUID support for Sqlite in tests.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry I thought you were struggling with the test, and I was trying to help. your implementation with the platform-independent UUID is better, the only thing missing is the relationship (for foreign_model.get_ser...) that you can copy and paste from my commit. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ok. I was just busy yesterday. Will finish the PR changes today.

starlette_admin/views.py Show resolved Hide resolved
# Make sure the primary key is always available
pk_attr = not_none(self.pk_attr)
if pk_attr not in obj_serialized:
pk_value = await self.get_serialized_pk_value(request, obj, action)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use get_serialized_pk_value at these lines as well.

obj_serialized[field.name] = await foreign_model.get_pk_value(

(await foreign_model.get_pk_value(request, obj))

Copy link

codecov bot commented Jul 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (2540c51) to head (90016d3).
Report is 16 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #553   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        76    +1     
  Lines         5817      5897   +80     
=========================================
+ Hits          5817      5897   +80     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alg
Copy link
Contributor Author

alg commented Jul 2, 2024

@jowilf here's the gist of my changes that are now conflicting with yours -- https://gist.github.com/alg/b03907a4edcd44fc5adbfd27c90fcf76

Let me know how you want to deal with it. Are you taking over, or me integrating your changes?

@alg alg force-pushed the serializable-pk branch 2 times, most recently from d58e84d to 2cef782 Compare July 2, 2024 09:34
@alg
Copy link
Contributor Author

alg commented Jul 2, 2024

@jowilf Pushed the updates to tests incorporating your changes. Please review.

@alg
Copy link
Contributor Author

alg commented Jul 3, 2024

I guess, I need to provide the UUID for MySQL now for the tests of serialization to pass. :) See what I meant? Will have a look.

When PK is not listed among fields it needs proper serialization in case
it's not of a JSON-serializable type already (for example, UUID).
@alg
Copy link
Contributor Author

alg commented Jul 3, 2024

Fixed the issue with tests and verified locally by running tests for MySQL.

tests/sqla/utils.py Outdated Show resolved Hide resolved
tests/sqla/utils.py Outdated Show resolved Hide resolved
tests/sqla/utils.py Outdated Show resolved Hide resolved
Copy link
Owner

@jowilf jowilf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @alg 🎉

@jowilf jowilf changed the title Add forced PK serialization Fix JSON serialization error for UUID primary keys when excluded from list Jul 8, 2024
@jowilf jowilf merged commit 056d976 into jowilf:main Jul 8, 2024
10 checks passed
@jowilf jowilf mentioned this pull request Jul 12, 2024
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