-
Notifications
You must be signed in to change notification settings - Fork 0
Feature data sorting in route handler #7
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
Conversation
- Updates the GET /api/v1/data route to parse and validate `sortBy` and `sortOrder` query parameters. - Passes the sorting parameters to the underlying data repository's `readAllByQuery` method. - Throws a `BadRequestException` if `sortOrder` is provided with a value other than "asc" or "desc".
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.
Summary of Changes
Hello @fulleni, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a new feature to the API that enables flexible data sorting. It allows clients to specify the field to sort by and the sort order (ascending or descending) through query parameters. The changes involve modifying the route handler to parse these parameters, validate the sort order, and pass them to the data repository for execution.
Highlights
- Feature: Implemented flexible data sorting in the route handler, allowing ordering of data by various fields in ascending or descending order.
- Query Parameters: Added 'sortBy' and 'sortOrder' query parameters to the API to control the sorting of results.
- Sort Order Validation: Introduced validation for the 'sortOrder' parameter, ensuring it is either 'asc' or 'desc', and returns a BadRequestException for invalid values.
- Repository Call: Modified the repository calls for various data models (headline, category, source, country, user, user_app_settings, user_content_preferences, app_config) to include 'sortBy' and 'sortOrder' parameters.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request introduces a new feature for sorting data in the /api/v1/data
route handler. The implementation correctly adds sortBy
and sortOrder
parameters and passes them to the data layer.
My review includes a critical security suggestion to validate the sortBy
parameter against an allowlist to prevent potential abuse. I've also provided several suggestions to improve code maintainability and readability by refactoring the parameter parsing and filtering logic to be more idiomatic and by pointing out significant code duplication that should be addressed in the future. Overall, the changes are a good step forward, but the validation of sortable fields is a must-have.
@@ -109,12 +109,34 @@ Future<Response> _handleGet( | |||
final queryParams = context.request.uri.queryParameters; | |||
final startAfterId = queryParams['startAfterId']; | |||
final limitParam = queryParams['limit']; | |||
final sortBy = queryParams['sortBy']; |
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.
The sortBy
query parameter is passed directly to the data layer without validation. This is a potential security and performance risk, as clients could request sorting on un-indexed or sensitive fields. You should validate the sortBy
value against an allowlist of sortable fields for each model.
This validation should be performed within the switch (modelName)
block, where the context of the model is known.
For example, for the headline
model:
case 'headline':
const allowedSortFields = {'createdAt', 'title'}; // Example allowlist
if (sortBy != null && !allowedSortFields.contains(sortBy)) {
throw BadRequestException(
'Invalid "sortBy" parameter for model "headline". '
'Allowed values are: ${allowedSortFields.join(', ')}.',
);
}
allowedKeys = {'categories', 'sources', 'q'};
// ... rest of the case
SortOrder? sortOrder; | ||
if (sortOrderRaw != null) { | ||
if (sortOrderRaw == 'asc') { | ||
sortOrder = SortOrder.asc; | ||
} else if (sortOrderRaw == 'desc') { | ||
sortOrder = SortOrder.desc; | ||
} else { | ||
throw const BadRequestException( | ||
'Invalid "sortOrder" parameter. Must be "asc" or "desc".', | ||
); | ||
} | ||
} |
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.
The if-else if-else
chain for parsing sortOrderRaw
is functional, but a switch
expression is more idiomatic and concise in modern Dart. Using a switch
expression improves readability by clearly mapping input values to outcomes.
SortOrder? sortOrder;
if (sortOrderRaw != null) {
sortOrder = switch (sortOrderRaw) {
'asc' => SortOrder.asc,
'desc' => SortOrder.desc,
_ => throw const BadRequestException(
'Invalid "sortOrder" parameter. Must be "asc" or "desc".',
),
};
}
(k) => | ||
k != 'model' && | ||
k != 'startAfterId' && | ||
k != 'limit' && | ||
k != 'sortBy' && | ||
k != 'sortOrder', |
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.
The chain of &&
conditions to exclude standard query parameters is becoming long and can be hard to read. Using a Set
and !contains
is more concise and scalable if more parameters are added in the future. You can create the set inline for a succinct change.
(k) => !{'model', 'startAfterId', 'limit', 'sortBy', 'sortOrder'}
.contains(k),
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); |
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.
This switch
statement contains significant code duplication. Every case
block calls repo.readAllByQuery
with the exact same set of arguments, differing only by the repository type. This makes the code harder to maintain, as any change to this call must be replicated in all case
blocks.
While a full refactor might be beyond the scope of this PR, consider exploring ways to reduce this duplication in the future. One approach could be to enhance the ModelConfig
to include a method that handles data retrieval, abstracting away the need for this large switch
statement in the route handler.
Status
READY/IN DEVELOPMENT/HOLD
Description
Type of Change