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 values check in extra_data #891

Merged
merged 1 commit into from
Mar 7, 2024

Conversation

igorgaming
Copy link
Contributor

@igorgaming igorgaming commented Feb 29, 2024

Proposed changes

Currently, there is a bug in getting extra_data from OAuth providers. For example, if a field from the OAuth API in the extra_data method returns integers (such as 1 or 0), then this will not work correctly and save an incorrect value (in my case null instead of 0):

{'test': 0}.get('test') or 'other expression'  # returns `other expression` instead of `0`

So this can be fixed like that {'test': 0}.get('test', 'other expression')

Types of changes

Please check the type of change your PR introduces:

  • Release (new release request)
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (PEP8, lint, formatting, renaming, etc)
  • Refactoring (no functional changes, no api changes)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Build related changes (build process, tests runner, etc)
  • Other (please describe):

Checklist

Put an x in the boxes that apply. You can also fill these out after creating
the PR. If you're unsure about any of them, don't hesitate to ask. We're here to
help! This is simply a reminder of what we are going to look for before merging
your code.

Summary by CodeRabbit

  • Refactor
    • Improved the method for retrieving extra data in social authentication to handle multiple fallbacks more efficiently.

Copy link

coderabbitai bot commented Feb 29, 2024

Walkthrough

The update primarily enhances the resilience and flexibility in fetching additional data within the authentication backend. By refining the method to retrieve data, it introduces a more robust approach to handle potential variations in the data source, ensuring that the necessary information is accessible even with different fallback mechanisms.

Changes

Files Change Summary
.../backends/base.py Modified extra_data to use nested get for data retrieval with fallbacks from response, details, and alias.

🐇✨
In the code's woven burrow, deep,
A change was made, not just to keep.
With nested gets, so sleek, so fine,
We fetch the data, line by line.
Through response and details, we hop,
Not missing a beat, nor a drop.
🌟🐾

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dbc880c and fa1cf67.
Files selected for processing (1)
  • social_core/backends/base.py (1 hunks)

@@ -146,7 +146,7 @@ def extra_data(self, user, uid, response, details=None, *args, **kwargs):
elif size == 1:
name = alias = entry[0]
discard = False
value = response.get(name) or details.get(name) or details.get(alias)
value = response.get(name, details.get(name, details.get(alias)))
Copy link

@coderabbitai coderabbitai bot Feb 29, 2024

Choose a reason for hiding this comment

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

The change in the assignment of value using a nested get method call is intended to fix the bug related to handling integer values in extra_data. However, there's a potential issue with the current implementation:

value = response.get(name, details.get(name, details.get(alias)))

This line attempts to address the bug by using a fallback mechanism for retrieving data. However, the use of details.get(name, details.get(alias)) as the default value in the first get call might not be entirely correct. This approach assumes that details will always have the name or alias keys, which might not be the case, potentially leading to None being assigned to value even when response has a relevant entry.

A more robust solution would be to explicitly check for the presence of keys in response and details and then decide the value of value. This could involve a conditional check or a more nuanced approach to ensure that integer values like 0 are correctly handled and not overridden by None or missed due to the fallback logic not being triggered as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, in the end value can be None, its ok and handled in one line below.

Copy link

Choose a reason for hiding this comment

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

@igorgaming: Thank you for your patience. After reviewing the lines immediately following the modification, it's clear that the code does indeed handle cases where value might be None. The conditional check:

if discard and not value:
    continue

ensures that if discard is True and value is None (or evaluates to False, which includes 0, False, [], etc.), the loop will continue without assigning None to data[alias]. This mechanism effectively addresses the concern about None values being incorrectly processed, aligning with your explanation. It seems the implementation is designed to accommodate None values appropriately, confirming the robustness of the bug fix. Thank you for clarifying this aspect of the code change.

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Extract the lines immediately following the modified line to understand how `None` values are handled.
sed -n '148,160p' social_core/backends/base.py

Length of output: 697

Copy link

codecov bot commented Mar 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.77%. Comparing base (dbc880c) to head (fa1cf67).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #891   +/-   ##
=======================================
  Coverage   77.77%   77.77%           
=======================================
  Files         347      347           
  Lines       10606    10606           
  Branches      699      699           
=======================================
  Hits         8249     8249           
  Misses       2200     2200           
  Partials      157      157           
Flag Coverage Δ
unittests 77.77% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@nijel nijel merged commit 51359ba into python-social-auth:master Mar 7, 2024
9 checks passed
@nijel
Copy link
Member

nijel commented Mar 7, 2024

Merged, thanks for your contribution!

@nijel nijel self-assigned this Mar 7, 2024
@igorgaming igorgaming deleted the patch-1 branch March 8, 2024 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants