Skip to content

Commit

Permalink
Fix failure to load SecretStr from JSON (regression in v2.4) (#7729)
Browse files Browse the repository at this point in the history
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
  • Loading branch information
sydney-runkle and dmontagu committed Oct 3, 2023
1 parent 082ea6c commit 8ff235a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
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,
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

0 comments on commit 8ff235a

Please sign in to comment.