-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Generated JSON schema should contain $schema properties #1478
Comments
Makes sense to me. @tiangolo what do think? |
Yeah, I think it makes sense. Some extra comments and info: The current implementation is based on In OpenAPI, the Some extra notes: The currently generated schemas are also compatible with Here's the migration guide: https://json-schema.org/draft/2019-09/release-notes.html The main changes, e.g. making Other things to note in version There is now an official The last change that affects here is that Another note is that OpenAPI version 3.1.0 (still under development) is based on JSON Schema 2019-09. That means that once that is released, all the JSON Schemas that can be generated with Pydantic will also be valid with OpenAPI. The main 2 specific points that currently differ are:
|
The latest version in the jsonschema library is now draft2020-12 which causes the validation against the Pydantic models (which are based on draft-07) to break. Adding in the |
As a work-around in the meantime, it is easy to add import json
# Assuming MainModel is a subclass of pydantic.BaseModel
schema_obj = MainModel.schema()
schema_obj["$schema"] = "http://json-schema.org/draft-07/schema#"
print(json.dumps(schema_obj, indent=2)) |
I guess an alternative:
|
Any updates to this? Why doesn’t the generated schema specify which standard it adheres to? |
Would a PR be accepted to add |
Based on https://json-schema.org/understanding-json-schema/reference/schema.html#id4 I'm assuming we'll want to add |
Agreed, also easier to see and remove/change it than remember to add it. Therefore #5029 will close this. |
Will it always be |
For now it'll always be 2020-12, if someone wants to try and write a wrapper that infers the oldest compatible draft, happy to review a PR. But honestly, that sounds like a library in itself, and doesn't need to be included in pydantic. |
@JonathanPlasse is there a use case you have in mind for inferring the oldest compatible draft? I think @samuelcolvin is right that it's probably not worth including in pydantic proper, but I'm curious about if there is a use case where this would be particularly beneficial |
Schemastore.org has this recommendation in its documentation. |
That's fine for them, but their schemas are mostly hand written, so that recommendation is reasonable actionable. |
I just merged #5029 which adds the JSON schema generation approach we'll use in v2. As part of merging that, I did add a property to the pydantic/pydantic/json_schema.py Line 141 in 73373c3
but I did not include it in the generated schemas automatically. Considering the language in the specification says "SHOULD" not "MUST" include, I wasn't sure it was worth "polluting" the generated schemas (and tests) with that draft value everywhere by default. For what it's worth, I did explicitly include a test showing how you can easily override the schema generation to include the draft version: pydantic/tests/test_json_schema.py Lines 3090 to 3116 in 73373c3
It would also be straightforward to actually enable this by default, by uncommenting the last of the following lines: pydantic/pydantic/json_schema.py Lines 230 to 232 in 73373c3
If anyone feels strongly that the current behavior on the main branch (shown above) merits changes, please comment here; otherwise I'm inclined to leave it as is. |
I'm confused, what was the motivation for not enabling For other affected users suffering in the meantime, here's the best workaround I came up with, a slight variation on #1478 (comment) using json_schema_extra: from pydantic.json_schema import GenerateJsonSchema
class MyConfig(BaseModel):
model_config = ConfigDict(
json_schema_extra={
'$schema': GenerateJsonSchema.schema_dialect
}) still a little strange though that I need to be manually reaching in to feed pydantic its own metadata when it already knows which |
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:But I miss the
$schema
property and therefore it's not clear which version of JSON schema pydantic is generating. The documentation refers to the latest version, but this version could change over time so it's not clear to me.Also the specification says:
The documentation of the jsonschema libarary says
But this means that you have to specify the validator class (draft version) yourself or the jsonschema library uses their latest known draft version which is currently draft-07. But the latest version of the specification is draft2019-09.
So it would be great to add the used JSON schema draft/version ($schema property) when generating the JSON schema to help everyone in understand which version of the JSON schema specification the schema refers to.
As far as I know the currently available versions are
and I'm not sure which version pydantic is currently using
The text was updated successfully, but these errors were encountered: