Repository: frappe/frappe

Issue:
Social login cannot be finished while a new user is logging in with without gender attribute provided
<!--
Welcome to the Frappe Framework issue tracker! Before creating an issue, please heed the following:

1. This tracker should only be used to report bugs and request features / enhancements to Frappe
    - For questions and general support, use https://stackoverflow.com/questions/tagged/frappe
    - For documentation issues, refer to https://frappeframework.com/docs/user/en or the developer cheetsheet https://github.com/frappe/frappe/wiki/Developer-Cheatsheet
2. Use the search function before creating a new issue. Duplicates will be closed and directed to
   the original discussion.
3. When making a bug report, make sure you provide all required information. The easier it is for
   maintainers to reproduce, the faster it'll be fixed.
4. If you think you know what the reason for the bug is, share it with us. Maybe put in a PR 😉
-->

## Description of the issue

Social login cannot be finished while a new user is logging in with OAuth provider without a known gender attribute provided.
There is no default value filled, and there is a misuse of `frappe.new_doc()` causing the error.

## Context information (for bug reports)

A problem with "Gender" doctype is preventing new OAuth user logging in, after commit a2fba771 .

**Output of `bench version`**
```
erpnext 13.x.x-develop
frappe 13.0.0-dev
```

## Steps to reproduce the issue

1. Initialize a frappe site
2. Set social login with a custom provider that did not provide gender information (for example, KeyCloak)
3. Login with OAuth provider.

### Observed result

Error message shown in stacktrace listed below

### Expected result

User successfully logged in.

### Stacktrace / full error message

```
Traceback (most recent call last):
  File "(path omitted...)/apps/frappe/frappe/app.py", line 67, in application
    response = frappe.api.handle()
  File "(path omitted...)/apps/frappe/frappe/api.py", line 58, in handle
    return frappe.handler.handle()
  File "(path omitted...)/apps/frappe/frappe/handler.py", line 30, in handle
    data = execute_cmd(cmd)
  File "(path omitted...)/apps/frappe/frappe/handler.py", line 70, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "(path omitted...)/apps/frappe/frappe/__init__.py", line 1136, in call
    return fn(*args, **newargs)
  File "(path omitted...)/apps/frappe/frappe/integrations/oauth2_logins.py", line 50, in custom
    login_via_oauth2(provider, code, state, decoder=decoder_compat)
  File "(path omitted...)/apps/frappe/frappe/utils/oauth.py", line 113, in login_via_oauth2
    login_oauth_user(info, provider=provider, state=state)
  File "(path omitted...)/apps/frappe/frappe/utils/oauth.py", line 191, in login_oauth_user
    if update_oauth_user(user, data, provider) is False:
  File "(path omitted...)/apps/frappe/frappe/utils/oauth.py", line 237, in update_oauth_user
    doc = frappe.new_doc("Gender", {"gender": gender})
  File "(path omitted...)/apps/frappe/frappe/__init__.py", line 747, in new_doc
    return get_new_doc(doctype, parent_doc, parentfield, as_dict=as_dict)
  File "(path omitted...)/apps/frappe/frappe/model/create_new.py", line 27, in get_new_doc
    set_dynamic_default_values(doc, parent_doc, parentfield)
  File "(path omitted...)/apps/frappe/frappe/model/create_new.py", line 131, in set_dynamic_default_values
    doc["parent"] = parent_doc.name
AttributeError: 'dict' object has no attribute 'name'
```

## Additional information

A temporary patch providing a default value to workaround this issue is coming. Will updated after pull request created. 


Discussion:

Resolving patch:
diff --git a/frappe/utils/oauth.py b/frappe/utils/oauth.py
--- a/frappe/utils/oauth.py
+++ b/frappe/utils/oauth.py
@@ -231,14 +231,14 @@ def update_oauth_user(user, data, provider):
 		save = True
 		user = frappe.new_doc("User")
 
-		gender = (data.get("gender") or "").title()
+		gender = data.get("gender", "").title()
 
-		if not frappe.db.exists("Gender", gender):
+		if gender and not frappe.db.exists("Gender", gender):
 			doc = frappe.new_doc("Gender", {"gender": gender})
 			doc.insert(ignore_permissions=True)
 
 		user.update({
-			"doctype":"User",
+			"doctype": "User",
 			"first_name": get_first_name(data),
 			"last_name": get_last_name(data),
 			"email": get_email(data),


Test patch: