|
1 | | -type MyTypeAliasType = int | str |
| 1 | +from mashumaro.core.meta.types.common import clean_id |
2 | 2 | from mashumaro.jsonschema import build_json_schema |
| 3 | +from mashumaro.jsonschema.models import Context |
| 4 | +from mashumaro.jsonschema.schema import _type_alias_definition_name |
| 5 | + |
| 6 | +type JSON = int | str | float | bool | None | list[JSON] | dict[str, JSON] |
| 7 | +type X = int | str |
| 8 | +type A = int | list[B] |
| 9 | +type B = str | list[A] |
3 | 10 |
|
4 | 11 |
|
5 | 12 | def test_type_alias_type_with_jsonschema(): |
6 | | - schema = build_json_schema(MyTypeAliasType) |
| 13 | + schema = build_json_schema(X) |
7 | 14 | assert schema.to_dict() == { |
8 | 15 | "anyOf": [{"type": "integer"}, {"type": "string"}] |
9 | 16 | } |
| 17 | + |
| 18 | + |
| 19 | +def test_jsonschema_for_recursive_union() -> None: |
| 20 | + schema = build_json_schema(JSON) |
| 21 | + assert schema.to_dict() == { |
| 22 | + "$ref": "#/$defs/JSON", |
| 23 | + "$defs": { |
| 24 | + "JSON": { |
| 25 | + "anyOf": [ |
| 26 | + {"type": "integer"}, |
| 27 | + {"type": "string"}, |
| 28 | + {"type": "number"}, |
| 29 | + {"type": "boolean"}, |
| 30 | + {"type": "null"}, |
| 31 | + {"type": "array", "items": {"$ref": "#/$defs/JSON"}}, |
| 32 | + { |
| 33 | + "type": "object", |
| 34 | + "additionalProperties": {"$ref": "#/$defs/JSON"}, |
| 35 | + "propertyNames": {"type": "string"}, |
| 36 | + }, |
| 37 | + ] |
| 38 | + } |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def test_jsonschema_for_mutual_recursive_type_aliases_without_refs() -> None: |
| 44 | + schema = build_json_schema(A) |
| 45 | + assert schema.to_dict() == { |
| 46 | + "$ref": "#/$defs/A", |
| 47 | + "$defs": { |
| 48 | + "A": { |
| 49 | + "anyOf": [ |
| 50 | + {"type": "integer"}, |
| 51 | + { |
| 52 | + "type": "array", |
| 53 | + "items": { |
| 54 | + "anyOf": [ |
| 55 | + {"type": "string"}, |
| 56 | + { |
| 57 | + "type": "array", |
| 58 | + "items": {"$ref": "#/$defs/A"}, |
| 59 | + }, |
| 60 | + ] |
| 61 | + }, |
| 62 | + }, |
| 63 | + ] |
| 64 | + } |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +def test_jsonschema_for_mutual_recursive_type_aliases_with_refs() -> None: |
| 70 | + schema = build_json_schema(A, all_refs=True) |
| 71 | + assert schema.to_dict() == { |
| 72 | + "$ref": "#/$defs/A", |
| 73 | + "$defs": { |
| 74 | + "A": { |
| 75 | + "anyOf": [ |
| 76 | + {"type": "integer"}, |
| 77 | + {"type": "array", "items": {"$ref": "#/$defs/B"}}, |
| 78 | + ] |
| 79 | + }, |
| 80 | + "B": { |
| 81 | + "anyOf": [ |
| 82 | + {"type": "string"}, |
| 83 | + {"type": "array", "items": {"$ref": "#/$defs/A"}}, |
| 84 | + ] |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | +def test_type_alias_non_recursive_inlines_when_all_refs_false() -> None: |
| 91 | + schema = build_json_schema(X, all_refs=False) |
| 92 | + assert schema.to_dict() == { |
| 93 | + "anyOf": [ |
| 94 | + {"type": "integer"}, |
| 95 | + {"type": "string"}, |
| 96 | + ] |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +def test_type_alias_non_recursive_uses_defs_when_all_refs_true() -> None: |
| 101 | + schema = build_json_schema(X, all_refs=True) |
| 102 | + assert schema.to_dict() == { |
| 103 | + "$ref": "#/$defs/X", |
| 104 | + "$defs": { |
| 105 | + "X": { |
| 106 | + "anyOf": [ |
| 107 | + {"type": "integer"}, |
| 108 | + {"type": "string"}, |
| 109 | + ] |
| 110 | + } |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | +def test_type_alias_placeholder_not_leaking_into_context_defs() -> None: |
| 116 | + # Ensure that for non-recursive aliases with all_refs=False we don't leave |
| 117 | + # unused entries in Context.definitions. |
| 118 | + ctx = Context() |
| 119 | + schema = build_json_schema(X, context=ctx, all_refs=False) |
| 120 | + assert schema.to_dict() == { |
| 121 | + "anyOf": [ |
| 122 | + {"type": "integer"}, |
| 123 | + {"type": "string"}, |
| 124 | + ] |
| 125 | + } |
| 126 | + assert ctx.definitions == {} |
| 127 | + |
| 128 | + |
| 129 | +def test_type_alias_definition_name_falls_back_to_clean_id_when_name_empty() -> ( |
| 130 | + None |
| 131 | +): |
| 132 | + class NamelessAlias: |
| 133 | + __name__ = "" |
| 134 | + |
| 135 | + alias = NamelessAlias() |
| 136 | + |
| 137 | + assert _type_alias_definition_name(alias) == clean_id(str(id(alias))) |
0 commit comments