Repository navigation
test_json_schema #452
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
Open
Niccolum
wants to merge
6
commits into
reagento:develop
Choose a base branch
from
Niccolum:test_json_schema
base: develop
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
test_json_schema #452
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,187 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| import pytest | ||
| from tests_helpers import raises_exc | ||
| from tests_helpers.morphing import JSONSchemaOptItem, assert_morphing | ||
|
|
||
| from adaptix import Retort, name_mapping | ||
| from adaptix._internal.definitions import Direction | ||
| from adaptix._internal.morphing.facade.func import ( | ||
| DIALECT_2020_12, | ||
| generate_json_schema, | ||
| generate_json_schemas_namespace, | ||
| load_json_schema, | ||
| ) | ||
| from adaptix._internal.morphing.json_schema.definitions import JSONSchema | ||
| from adaptix._internal.morphing.json_schema.mangling import IndexRefMangler | ||
| from adaptix._internal.morphing.json_schema.ref_generator import BuiltinRefGenerator | ||
| from adaptix._internal.morphing.json_schema.resolver import BuiltinJSONSchemaResolver, RefGenerator | ||
| from adaptix._internal.morphing.json_schema.schema_model import JSONSchemaBuiltinFormat | ||
| from adaptix.load_error import AggregateLoadError, ExtraFieldsLoadError | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ["raw", "expected"], | ||
| [ | ||
| ("date-time", JSONSchemaBuiltinFormat.DATE_TIME), | ||
| ("my-custom-format", "my-custom-format"), | ||
| ], | ||
| ) | ||
| def test_load_json_schema_format_uses_builtin_enum_or_falls_back_to_str(raw, expected): | ||
| schema = load_json_schema({"type": "string", "format": raw}) | ||
|
|
||
| assert schema.format == expected | ||
|
|
||
|
|
||
| def test_load_json_schema_strict_unknown_field_raises(): | ||
| data = {"type": "string", "x-custom": "value"} | ||
|
|
||
| raises_exc( | ||
| AggregateLoadError( | ||
| f"while loading model {JSONSchema}", | ||
| [ExtraFieldsLoadError({"x-custom"}, data)], | ||
| ), | ||
| lambda: load_json_schema(data, error_on_extra=True), | ||
| ) | ||
|
|
||
|
|
||
| def test_load_json_schema_lax_unknown_field_goes_to_extra_keywords(): | ||
| schema = load_json_schema({"type": "string", "x-custom": "value"}, error_on_extra=False) | ||
|
|
||
| assert schema.extra_keywords == {"x-custom": "value"} | ||
|
|
||
|
|
||
| @dataclass | ||
| class SimpleModel: | ||
| name: str | ||
| value: int | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "with_dialect_uri", | ||
| [True, False], | ||
| ids=["with_uri", "without_uri"], | ||
| ) | ||
| def test_generate_json_schema_dialect_uri(with_dialect_uri: bool): # noqa: FBT001 | ||
| schema = generate_json_schema(Retort(), SimpleModel, Direction.INPUT, with_dialect_uri=with_dialect_uri) | ||
|
|
||
| assert ("$schema" in schema) == with_dialect_uri | ||
|
|
||
|
|
||
| def test_generate_json_schema_matches_full_expected_shape(): | ||
| assert_morphing( | ||
| retort=Retort(), | ||
| tp=SimpleModel, | ||
| data={"name": "foo", "value": 1}, | ||
| loaded=SimpleModel(name="foo", value=1), | ||
| json_schema={ | ||
| "$ref": "#/$defs/SimpleModel", | ||
| "$schema": DIALECT_2020_12, | ||
| "$defs": { | ||
| "SimpleModel": { | ||
| "title": "SimpleModel", | ||
| "type": "object", | ||
| "required": ["name", "value"], | ||
| "properties": { | ||
| "name": {"type": "string"}, | ||
| "value": {"type": "integer"}, | ||
| }, | ||
| "additionalProperties": JSONSchemaOptItem(input=True), | ||
| }, | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def test_generate_json_schema_custom_ref_prefix(): | ||
| schema = generate_json_schema( | ||
| Retort(), SimpleModel, Direction.INPUT, | ||
| local_ref_prefix="#/components/schemas/", | ||
| ) | ||
|
|
||
| assert schema["$ref"] == "#/components/schemas/SimpleModel" | ||
|
|
||
|
|
||
| class _PrefixRefGenerator(RefGenerator): | ||
| def generate_ref(self, json_schema, loc_stack): | ||
| return "Custom" + BuiltinRefGenerator().generate_ref(json_schema, loc_stack) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ["resolver", "occupied_refs", "expected_ref"], | ||
| [ | ||
| pytest.param( | ||
| BuiltinJSONSchemaResolver(BuiltinRefGenerator(), IndexRefMangler()), | ||
| ("SimpleModel",), | ||
| "SimpleModel-1", | ||
| id="mangling_of_occupied_ref", | ||
| ), | ||
| pytest.param( | ||
| BuiltinJSONSchemaResolver(_PrefixRefGenerator(), IndexRefMangler()), | ||
| (), | ||
| "CustomSimpleModel", | ||
| id="custom_ref_generator", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_generate_json_schema_custom_resolver(resolver, occupied_refs, expected_ref): | ||
| schema = generate_json_schema( | ||
| Retort(), SimpleModel, Direction.INPUT, | ||
| resolver=resolver, | ||
| occupied_refs=occupied_refs, | ||
| ) | ||
|
|
||
| assert schema["$ref"] == f"#/$defs/{expected_ref}" | ||
|
|
||
|
|
||
| @dataclass | ||
| class ListItem: | ||
| x: int | ||
| y: int | ||
|
|
||
|
|
||
| @dataclass | ||
| class ListModel: | ||
| a: ListItem | ||
| b: ListItem | ||
|
|
||
|
|
||
| def test_generate_json_schema_prefix_items_dedupes_shared_ref(): | ||
| retort = Retort(recipe=[name_mapping(ListModel, as_list=True)]) | ||
|
|
||
| schema = generate_json_schema(retort, ListModel, Direction.INPUT) | ||
|
|
||
| assert schema["$defs"]["ListModel"]["prefixItems"] == [ | ||
| {"$ref": "#/$defs/ListItem"}, | ||
| {"$ref": "#/$defs/ListItem"}, | ||
| ] | ||
| assert "ListItem" in schema["$defs"] | ||
|
|
||
|
|
||
| @dataclass | ||
| class Tag: | ||
| label: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class Article: | ||
| title: str | ||
| tag: Tag | ||
|
|
||
|
|
||
| @dataclass | ||
| class Post: | ||
| body: str | ||
| tag: Tag | ||
|
|
||
|
|
||
| def test_generate_schemas_namespace_deduplicates_shared_types(): | ||
| retort = Retort() | ||
|
|
||
| defs, schemas = generate_json_schemas_namespace([ | ||
| (retort, Direction.INPUT, Article), | ||
| (retort, Direction.INPUT, Post), | ||
| ]) | ||
|
|
||
| assert len(schemas) == 2 | ||
| assert defs.keys() == {"Article", "Post", "Tag"} |
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.
What was this change necessary for?
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.
It fixes a shared-state bug in JSONSchemaPatch. copy(self) is a shallow copy, so self_copy._patchers and self._patchers pointed to the same list. The old .append(patcher) therefore also added the patcher to the original object, and to every other copy made from it.
Example of the bug:
The patch methods are meant to be immutable and return a new object. With the old code, using a patch as a base for several variants produced wrong schemas. Building a new list for the copy fixes this, and the original is never mutated.