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

Conversation

sydney-runkle
Copy link
Member

@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-pages
Copy link

cloudflare-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
Member 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
Contributor

@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 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
Contributor

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

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.

@dmontagu dmontagu merged commit 8ff235a into main Oct 3, 2023
59 checks passed
@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