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 failure to load SecretStr from JSON (regression in v2.4) #7729

Merged
merged 5 commits into from Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Expand Up @@ -32,7 +32,7 @@ jobs:

- name: install
run: |
pdm use -f $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G linting -G email

- uses: pre-commit/action@v3.0.0
Expand All @@ -56,7 +56,7 @@ jobs:
# Because the secret for accessing the library is not accessible from forks, but we still want to run
# this job on public CI runs.
run: |
pdm venv create --with-pip $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G docs

- run: pdm run python -c 'import docs.plugins.main'
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:

- name: install deps
run: |
pdm use -f $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G testing -G testing-extra -G email -G memray
pdm add pytest-memray

Expand Down Expand Up @@ -130,7 +130,7 @@ jobs:

- name: install deps
run: |
pdm use -f $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G testing -G email

- run: pdm info && pdm list
Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:

- name: install deps
run: |
pdm use -f $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G testing

- name: install example plugin
Expand Down Expand Up @@ -240,7 +240,7 @@ jobs:

- name: install deps
run: |
pdm use -f $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G testing -G mypy

- name: install mypy
Expand Down Expand Up @@ -337,7 +337,7 @@ jobs:

- name: install deps
run: |
pdm use -f $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G testing -G email

- name: install typing-extensions
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs-update.yml
Expand Up @@ -72,7 +72,7 @@ jobs:

- name: install
run: |
pdm venv create --with-pip $PYTHON
pdm venv create --with-pip --force $PYTHON
pdm install -G docs
pdm run python -m pip install https://files.scolvin.com/${MKDOCS_TOKEN}/mkdocs_material-9.4.2+insiders.4.42.0-py3-none-any.whl
env:
Expand Down
24 changes: 14 additions & 10 deletions pydantic/types.py
Expand Up @@ -1448,16 +1448,20 @@ def get_json_schema(_core_schema: core_schema.CoreSchema, handler: GetJsonSchema
)
return json_schema

s = core_schema.union_schema(
[
core_schema.is_instance_schema(source),
core_schema.no_info_after_validator_function(
source, # construct the type
inner_schema,
),
],
strict=True,
custom_error_type=error_kind,
json_schema = core_schema.no_info_after_validator_function(
source, # construct the type
inner_schema,
)
s = core_schema.json_or_python_schema(
python_schema=core_schema.union_schema(
[
core_schema.is_instance_schema(source),
json_schema,
],
strict=True,
custom_error_type=error_kind,
),
json_schema=json_schema,
Copy link
Contributor

Choose a reason for hiding this comment

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

This might want to have a custom_error_schema and use custom_error_type in some form on this branch. Possibly worth testing the JSON validation error with the wrong type.

Copy link
Contributor

@dmontagu dmontagu Oct 3, 2023

Choose a reason for hiding this comment

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

(As discussed elsewhere,) I think given the specific format of the errors seems good (added to the test below), this isn't necessary.

serialization=core_schema.plain_serializer_function_ser_schema(
serialize,
info_arg=True,
Expand Down
18 changes: 15 additions & 3 deletions tests/test_types.py
Expand Up @@ -3921,13 +3921,25 @@ class Foobar(BaseModel):
]


def test_secretstr():
@pytest.mark.parametrize('validate_json', [True, False])
def test_secretstr(validate_json):
class Foobar(BaseModel):
password: SecretStr
empty_password: SecretStr

# Initialize the model.
f = Foobar(password='1234', empty_password='')
if validate_json:
f = Foobar.model_validate_json('{"password": "1234", "empty_password": ""}')
with pytest.raises(ValidationError) as exc_info:
Foobar.model_validate_json('{"password": 1234, "empty_password": null}')
else:
f = Foobar(password='1234', empty_password='')
with pytest.raises(ValidationError) as exc_info:
Foobar(password=1234, empty_password=None)

assert exc_info.value.errors(include_url=False) == [
{'type': 'string_type', 'loc': ('password',), 'msg': 'Input should be a valid string', 'input': 1234},
{'type': 'string_type', 'loc': ('empty_password',), 'msg': 'Input should be a valid string', 'input': None},
]

# Assert correct types.
assert f.password.__class__.__name__ == 'SecretStr'
Expand Down