Skip to content

Fix failure to load SecretStr from JSON (regression in v2.4)#7729

Merged
dmontagu merged 5 commits intomainfrom
fix-secretstr-from-json-validation
Oct 3, 2023
Merged

Fix failure to load SecretStr from JSON (regression in v2.4)#7729
dmontagu merged 5 commits intomainfrom
fix-secretstr-from-json-validation

Conversation

@sydney-runkle
Copy link
Copy Markdown
Contributor

@sydney-runkle sydney-runkle commented Oct 3, 2023

Change Summary

Ensure that a SecretStr type can be populated from json.

Related issue number

Fix #7720

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @samuelcolvin

@sydney-runkle sydney-runkle added the relnotes-fix Used for bugfixes. label Oct 3, 2023
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Oct 3, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 662e0ed
Status: ✅  Deploy successful!
Preview URL: https://ae388ae4.pydantic-docs2.pages.dev
Branch Preview URL: https://fix-secretstr-from-json-vali.pydantic-docs2.pages.dev

View logs

@sydney-runkle
Copy link
Copy Markdown
Contributor Author

Please review 😄

@dmontagu dmontagu changed the title Fix SecretStr from json bug introduced in v2.4 Fix failure to load SecretStr from JSON (regression in v2.4) Oct 3, 2023
@dmontagu dmontagu changed the title Fix failure to load SecretStr from JSON (regression in v2.4) Fix failure to load SecretStr from JSON (regression in v2.4) Oct 3, 2023
Copy link
Copy Markdown
Collaborator

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Overall looks like the right fix to me, two quick thoughts:

Comment thread tests/test_types.py Outdated
Comment on lines +3930 to +3933
f1 = Foobar(password='1234', empty_password='')
f2 = Foobar.model_validate_json('{"password": "1234", "empty_password": ""}')

for f in [f1, f2]:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It may be slightly nicer to turn this into a parameterized test rather than have one of f1 or f2 fail inside the loop

Comment thread pydantic/types.py
strict=True,
custom_error_type=error_kind,
),
json_schema=json_schema,
Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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.

@dmontagu dmontagu merged commit 8ff235a into main Oct 3, 2023
@dmontagu dmontagu deleted the fix-secretstr-from-json-validation branch October 3, 2023 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON validation of model with SecretStr field fails

4 participants