-
Notifications
You must be signed in to change notification settings - Fork 27
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
269 Stop multi-dimensional array and email filtering from breaking #270
269 Stop multi-dimensional array and email filtering from breaking #270
Conversation
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #270 +/- ##
==========================================
- Coverage 92.68% 92.59% -0.10%
==========================================
Files 33 34 +1
Lines 2038 2052 +14
==========================================
+ Hits 1889 1900 +11
- Misses 149 152 +3 ☔ View full report in Codecov by Sentry. |
@dantownsend It's the same problem we have here because Piccolo Admin depends on the Piccolo API. FastAPI does not support nested lists via |
@sinisaos I looked into it a bit more, and it seems like OpenAPI allows arrays as query params, but not multidimensional arrays: https://swagger.io/docs/specification/describing-parameters/#query-parameters Interestingly, Axios does allow multidimensional arrays ( https://www.npmjs.com/package/qs So I think FastAPI only allows basic arrays because that's all that the OpenAPI JS interface supports. I think the options are:
array_1 = [["a", "b", "c"], ["d", "e", "f"]]
array_2 = [["g", "h", "i"], ["j", "k", "l"]]
# if the query is "a", then return array_1 I'm not concerned with getting a perfect solution - just something which kind of works without crashing, as multidimensional arrays is definitely an edgecase. |
piccolo_api/fastapi/endpoints.py
Outdated
""" | ||
if t.get_origin(type_) is list: | ||
args = t.get_args(type_) | ||
if args and t.get_origin(args[0]) is list: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be for checking a multidimensional list
if args[0] is list:
return True
because if args and t.get_origin(args[0]) is list:
always returns False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - I need to test it.
That would be easiest with something like this if _is_multidimensional_array(type_=type_):
pass
else:
# previous parameters code unchanged That works both GET and POST request in interactive docs.
That would be great, but I don't know how to get around the FastAPI error from the previous comment |
Yes, that's definitely an option. If getting filters to work is too much hassle, that's definitely what we need to do. It turns out that the SQL for filtering multidimensional arrays to check if they contain a value is fairly simple: -- Get me all rows where the array (or sub array) contains 'a'
select * from my_table where exists (select 1 from unnest(my_column) i where i = 'a'); EDIT: even select * from my_table where 'a' = any(my_column); |
Filtering nested lists is not a problem on the ORM side. Filtering works with raw SQL or with ORM query like this await MyTable.select().where(MyTable.my_column.any(1)) # this works without problem I'm talking about the FastAPI(OpenAPI) error for using |
@sinisaos For that we just need to do something like: if _is_multidimensional_array(type_):
type_ = list |
This still isn't perfect - but I think I know the solution now. Rather than changing it within |
This PR will fail until a new version of Piccolo is released which contains |
@dantownsend I tried your changes and they work without problems in FastAPI docs. A clever solution, which solves the problems I tried to explain in the my previous comments. Tests will pass when you release Piccolo ORM changes. Great job :) |
@sinisaos Thanks. I think I'll move |
Now we've added a separate Pydantic model for handling filters, I noticed that we need this not just for edge cases with multidimensional arrays, but also things like
|
I think this is basically done now ... but it could do with tests for filtering email and multidimensional array columns in GET requests. |
Resolves #269