Skip to content

Inline references if possible when generating schema for json_schema_input_type #10439

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

Merged
merged 3 commits into from
Sep 19, 2024

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Sep 18, 2024

Fixes #10434.
Requires #10444.

Previously, the core schema for:

{
│   'type': 'model',
│   'cls': <class '__main__.Model'>,
│   'schema': {
│   │   'type': 'model-fields',
│   │   'fields': {
│   │   │   'sub': {
│   │   │   │   'type': 'model-field',
│   │   │   │   'schema': {
│   │   │   │   │   'type': 'function-plain',
│   │   │   │   │   'function': {'type': 'no-info', 'function': <function Model.<lambda> at 0x7bd0081e94e0>},
│   │   │   │   │   'metadata': {
│   │   │   │   │   │   'pydantic_js_functions': [],
│   │   │   │   │   │   'pydantic_js_annotation_functions': [],
│   │   │   │   │   │   'pydantic_js_input_core_schema': {'type': 'definition-ref', 'schema_ref': '__main__.Sub:97821927158416'}
│   │   │   │   │   },
│   │   │   │   │   'serialization': {...}
│   │   │   │   },
│   │   │   │   'metadata': {...}
│   │   │   }
│   │   },
│   │   'model_name': 'Model',
│   │   'computed_fields': []
│   },
│   'custom_init': False,
│   'root_model': False,
│   'config': {'title': 'Model'},
│   'ref': '__main__.Model:97821929019248',
│   'metadata': {...}
}

With pydantic_js_input_core_schema holding a reference to __main__.Sub:97821927158416 that would not be present in the core schema (because for a plain validator, the source type is not relevant), JSON Schema generation naturally fails (and thankfully only for JSON Schema generation, as this pydantic_js_input_core_schema metadata field is only relevant for JSON Schemas).


My first idea was to add extra logic in the _WalkCoreSchema class to handle this metadata field and include it in schema walking, but this is truly not ideal as we are leaking JSON Schema related logic inside this class (kind of a consequence of our metadata handling which wasn't ideal in the first place, as discussed already :/).

So I decided we should inline refs in the validator class directly, in a safe way for recursive references.

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

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Sep 18, 2024
Copy link

codspeed-hq bot commented Sep 18, 2024

CodSpeed Performance Report

Merging #10439 will degrade performances by 12.42%

Comparing 10434 (8f3c4b6) with main (61818e2)

Summary

❌ 1 regressions
✅ 36 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main 10434 Change
test_field_validators_serializers 2.5 ms 2.8 ms -12.42%

Copy link
Contributor

github-actions bot commented Sep 18, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  functional_validators.py
Project Total  

This report was generated by python-coverage-comment-action

Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Great work. Thanks for the nice tests here!

Copy link

cloudflare-workers-and-pages bot commented Sep 18, 2024

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8f3c4b6
Status: ✅  Deploy successful!
Preview URL: https://c2e1881d.pydantic-docs.pages.dev
Branch Preview URL: https://10434.pydantic-docs.pages.dev

View logs

@Viicos Viicos merged commit 3a821f1 into main Sep 19, 2024
61 of 62 checks passed
@Viicos Viicos deleted the 10434 branch September 19, 2024 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plain serializer and validator in Annotated does not work with pydantic model
2 participants