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 the blueprint for pydantic version 2 #1030

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

caarmen
Copy link
Contributor

@caarmen caarmen commented Jul 18, 2023

Use the proper ref_template, specifying the {model} in the template.

An example is documented here: https://github.com/pydantic/pydantic/blob/main/docs/usage/json_schema.md#schema-customization

We need to specify ref_template = '#/components/schemas/{model}', not ref_template = '#/components/schemas/' (need that extra {model} at the end).

model_json_schema generates something like this, for a Parent model with a child field of type Child:

{
    "$defs": {
        "Child": {
            "properties": {
                "foo": {
                    "default": 5,
                    "title": "Foo",
                    "type": "integer"
                }
            },
            "title": "Child",
            "type": "object"
        }
    },
    "properties": {
        "child": {
            "anyOf": [
                {
                    "$ref": "#/components/schemas/Child"
                },
                {
                    "type": "null"
                }
            ],
            "default": null
        }
    },
    "title": "Parent",
    "type": "object"
}

The definitions are in a key $defs (not definitions), and the references start with #/components/schemas as expected.

Use the proper `ref_template`, specifying the `{model}` in the template.

An example is documented here: https://github.com/pydantic/pydantic/blob/main/docs/usage/json_schema.md#schema-customization

We need to specify `ref_template = '#/components/schemas/{model}'`, not `ref_template = '#/components/schemas/'` (need that extra `{model}` at the end).

`model_json_schema` generates something like this, for a `Parent` model with a `child` field of type `Child`:

```json
{
    "$defs": {
        "Child": {
            "properties": {
                "foo": {
                    "default": 5,
                    "title": "Foo",
                    "type": "integer"
                }
            },
            "title": "Child",
            "type": "object"
        }
    },
    "properties": {
        "child": {
            "anyOf": [
                {
                    "$ref": "#/components/schemas/Child"
                },
                {
                    "type": "null"
                }
            ],
            "default": null
        }
    },
    "title": "Parent",
    "type": "object"
}
```

The definitions are in a key `$defs` (not `definitions`), and the references start with `#/components/schemas` as expected.
@caarmen
Copy link
Contributor Author

caarmen commented Jul 18, 2023

Please feel free to test it out. I only tested on a very simple case specific to my project.

Thanks! 🙏🏻

@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (b7f79b7) 98.55% compared to head (d1b874a) 98.55%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1030   +/-   ##
=======================================
  Coverage   98.55%   98.55%           
=======================================
  Files          68       68           
  Lines        8367     8367           
=======================================
  Hits         8246     8246           
  Misses        121      121           

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@devanubis
Copy link

devanubis commented Nov 13, 2023

Hi, this seems to partly fix my problems (#1105).

But it's not picking up nested models, e.g. the example test case from this old comment.

I'm going to try to see if I can get the extension to dive into nested $defs and add them as components too.

Turns out it works just fine 🎉 I had two extensions in scope from debugging attempts 🤦.

@tfranzel
Copy link
Owner

I suppose I'll merge it then. Almost forgot about it.

@tfranzel tfranzel merged commit fbbcab3 into tfranzel:master Nov 13, 2023
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants