Browse Source

🚸 Set format to password for fields `password` and `client_secret` in `OAuth2PasswordRequestForm`, make docs show password fields for passwords (#11032)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
pull/13793/merge
oogee 3 days ago
committed by GitHub
parent
commit
30b9dfb11c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      fastapi/security/oauth2.py
  2. 13
      tests/test_tutorial/test_security/test_tutorial003.py
  3. 13
      tests/test_tutorial/test_security/test_tutorial005.py

4
fastapi/security/oauth2.py

@ -85,7 +85,7 @@ class OAuth2PasswordRequestForm:
], ],
password: Annotated[ password: Annotated[
str, str,
Form(), Form(json_schema_extra={"format": "password"}),
Doc( Doc(
""" """
`password` string. The OAuth2 spec requires the exact field name `password` string. The OAuth2 spec requires the exact field name
@ -130,7 +130,7 @@ class OAuth2PasswordRequestForm:
] = None, ] = None,
client_secret: Annotated[ client_secret: Annotated[
Union[str, None], Union[str, None],
Form(), Form(json_schema_extra={"format": "password"}),
Doc( Doc(
""" """
If there's a `client_password` (and a `client_id`), they can be sent If there's a `client_password` (and a `client_id`), they can be sent

13
tests/test_tutorial/test_security/test_tutorial003.py

@ -163,7 +163,11 @@ def test_openapi_schema(client: TestClient):
} }
), ),
"username": {"title": "Username", "type": "string"}, "username": {"title": "Username", "type": "string"},
"password": {"title": "Password", "type": "string"}, "password": {
"title": "Password",
"type": "string",
"format": "password",
},
"scope": {"title": "Scope", "type": "string", "default": ""}, "scope": {"title": "Scope", "type": "string", "default": ""},
"client_id": IsDict( "client_id": IsDict(
{ {
@ -179,11 +183,16 @@ def test_openapi_schema(client: TestClient):
{ {
"title": "Client Secret", "title": "Client Secret",
"anyOf": [{"type": "string"}, {"type": "null"}], "anyOf": [{"type": "string"}, {"type": "null"}],
"format": "password",
} }
) )
| IsDict( | IsDict(
# TODO: remove when deprecating Pydantic v1 # TODO: remove when deprecating Pydantic v1
{"title": "Client Secret", "type": "string"} {
"title": "Client Secret",
"type": "string",
"format": "password",
}
), ),
}, },
}, },

13
tests/test_tutorial/test_security/test_tutorial005.py

@ -377,7 +377,11 @@ def test_openapi_schema(mod: ModuleType):
} }
), ),
"username": {"title": "Username", "type": "string"}, "username": {"title": "Username", "type": "string"},
"password": {"title": "Password", "type": "string"}, "password": {
"title": "Password",
"type": "string",
"format": "password",
},
"scope": {"title": "Scope", "type": "string", "default": ""}, "scope": {"title": "Scope", "type": "string", "default": ""},
"client_id": IsDict( "client_id": IsDict(
{ {
@ -393,11 +397,16 @@ def test_openapi_schema(mod: ModuleType):
{ {
"title": "Client Secret", "title": "Client Secret",
"anyOf": [{"type": "string"}, {"type": "null"}], "anyOf": [{"type": "string"}, {"type": "null"}],
"format": "password",
} }
) )
| IsDict( | IsDict(
# TODO: remove when deprecating Pydantic v1 # TODO: remove when deprecating Pydantic v1
{"title": "Client Secret", "type": "string"} {
"title": "Client Secret",
"type": "string",
"format": "password",
}
), ),
}, },
}, },

Loading…
Cancel
Save