-
Notifications
You must be signed in to change notification settings - Fork 39
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
Generate jsonschema from pydantic v2 #159
Open
joellabes
wants to merge
19
commits into
main
Choose a base branch
from
generate-jsonschema-from-pydantic-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6484bab
Generate dbt_yml_files from pydantic classes
aliceliu 8ea4b77
update script, and generate pydantic for all latest files
dave-connors-3 12573aa
add env to gitignore
dave-connors-3 b99f338
add autogenerated schemas
dave-connors-3 3fd5b97
add a few more tests to tests
dave-connors-3 c684478
remove pyproject toml in favor of requirements.txt
dave-connors-3 51e3409
Incorporate Devon's custom generator on top of Alice's generated models
joellabes bce1628
Remove generation for dbt_project.yml which can't be generated
joellabes cb47ad2
Require a character in jinja strings
joellabes df5ded7
Replace optional strings with ""
joellabes fe143ae
Handle remaining incorrect defaults
joellabes eef936c
Adopt courtney's changes from 155
joellabes d9dd764
actually run codegen
joellabes 2254ebc
Merge branch 'main' into generate-jsonschema-from-pydantic-v2
joellabes 603b8f7
less confusing freshness period name
joellabes a6b4e46
$schema not schema
joellabes 7b00793
Merge branch 'generate-jsonschema-from-pydantic-v2' of https://github…
joellabes 97e850a
timegranualirty optional
joellabes 2f1b25e
set default too
joellabes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
|
||
.DS_Store | ||
node_modules/ | ||
.venv | ||
.venv | ||
env | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to move this to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Any, Dict | ||
|
||
from src.latest.dbt_cloud import DbtCloud | ||
from src.latest.dbt_yml_files import DbtYmlFiles | ||
from src.latest.dependencies import Dependencies, Packages | ||
from src.latest.selectors import Selectors | ||
from pydantic.json_schema import GenerateJsonSchema | ||
|
||
|
||
class RemoveNullsGenerateJsonSchema(GenerateJsonSchema): | ||
"""A GenerateJsonSchema which removes nullability from types. | ||
|
||
We do not want to include optional values in the json schema because | ||
that would inhibit code completion and validation. | ||
|
||
Certain properties (such as freshness overrides) need to be nullable, | ||
which can be achieved by setting the $comment value below. | ||
""" | ||
|
||
def _remove_null(self, json_schema: Dict[str, Any]): | ||
if "$comment" in json_schema and json_schema["$comment"] == "truly_nullable": | ||
return | ||
if "anyOf" in json_schema: | ||
json_schema["anyOf"] = [ | ||
item for item in json_schema["anyOf"] if item != {"type": "null"} | ||
] | ||
for v in json_schema.values(): | ||
if isinstance(v, dict): | ||
self._remove_null(v) | ||
|
||
def generate(self, schema, mode="validation"): | ||
json_schema = super().generate(schema, mode=mode) | ||
json_schema["$schema"] = "http://json-schema.org/draft-07/schema#" | ||
self._remove_null(json_schema) | ||
return json_schema | ||
|
||
|
||
if __name__ == "__main__": | ||
files = { | ||
"dbt_yml_files": DbtYmlFiles, | ||
"dependencies": Dependencies, | ||
"packages": Packages, | ||
"selectors": Selectors, | ||
"dbt_cloud": DbtCloud, | ||
} | ||
output_directory = Path("schemas/latest") | ||
for file_name, model in files.items(): | ||
schema_file = output_directory / f"{file_name}-latest.json" | ||
schema_file.parent.mkdir(parents=True, exist_ok=True) | ||
schema = model.model_json_schema( | ||
mode="validation", schema_generator=RemoveNullsGenerateJsonSchema | ||
) | ||
print("Generating", schema_file) | ||
schema_file.write_text(json.dumps(schema, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
datamodel-code-generator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be necessary, right? This package was only necessary to initially transform json-schema to Pydantic, right? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"project-id": { | ||
"type": "string" | ||
}, | ||
"defer-env-id": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["project-id"], | ||
"additionalProperties": false | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you meant to reference a directory, not a file