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 using local types when using discriminators #189

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions mashumaro/core/meta/types/unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ def _get_variants_attr(self, spec: ValueSpec) -> str:
def _get_variants_map(self, spec: ValueSpec) -> str:
variants_attr = self._get_variants_attr(spec)
if spec.builder.is_nailed:
return f"{type_name(spec.builder.cls)}.{variants_attr}"
typ_name = spec.builder.get_type_name_identifier(spec.builder.cls)
return f"{typ_name}.{variants_attr}"
else:
return f"{spec.cls_attrs_name}.{variants_attr}"

Expand All @@ -266,11 +267,14 @@ def _get_variant_names(self, spec: ValueSpec) -> List[str]:
if self.discriminator.include_subtypes:
spec.builder.ensure_object_imported(iter_all_subclasses)
variant_names.extend(
f"*iter_all_subclasses({type_name(base_variant)})"
f"*iter_all_subclasses("
f"{spec.builder.get_type_name_identifier(base_variant)})"
for base_variant in base_variants
)
if self.discriminator.include_supertypes:
variant_names.extend(map(type_name, base_variants))
variant_names.extend(
map(spec.builder.get_type_name_identifier, base_variants)
)
return variant_names

def _get_variant_names_iterable(self, spec: ValueSpec) -> str:
Expand Down
133 changes: 132 additions & 1 deletion tests/test_jsonschema/test_jsonschema_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
JSONSchemaStringFormat,
)
from mashumaro.jsonschema.schema import UTC_OFFSET_PATTERN, EmptyJSONSchema
from mashumaro.types import SerializationStrategy
from mashumaro.types import Discriminator, SerializationStrategy
from tests.entities import (
CustomPath,
GenericNamedTuple,
Expand Down Expand Up @@ -1243,3 +1243,134 @@ class Config(BaseConfig):
required=["x"],
)
assert build_json_schema(DataClass) == schema


def test_jsonschema_with_discriminator_for_local_types():
@dataclass
class A:
value = "a"

@dataclass
class B:
value = "b"

@dataclass
class Main:
value: Annotated[
Union[A, B], Discriminator(field="value", include_supertypes=True)
]

schema = JSONObjectSchema(
title="Main",
properties={
"value": JSONSchema(
anyOf=[
JSONObjectSchema(
type=JSONSchemaInstanceType.OBJECT,
title="A",
additionalProperties=False,
),
JSONObjectSchema(
type=JSONSchemaInstanceType.OBJECT,
title="B",
additionalProperties=False,
),
],
),
},
additionalProperties=False,
required=["value"],
)
assert build_json_schema(Main) == schema


def test_jsonschema_with_discriminator_with_default_for_local_types():
@dataclass
class A:
value = "a"

@dataclass
class B:
value = "b"

@dataclass
class Main:
value: Annotated[
Union[A, B, None],
Discriminator(field="value", include_supertypes=True),
] = None

schema = JSONObjectSchema(
title="Main",
properties={
"value": JSONSchema(
anyOf=[
JSONObjectSchema(
type=JSONSchemaInstanceType.OBJECT,
title="A",
additionalProperties=False,
),
JSONObjectSchema(
type=JSONSchemaInstanceType.OBJECT,
title="B",
additionalProperties=False,
),
JSONSchema(
type=JSONSchemaInstanceType.NULL,
),
],
default=None,
),
},
additionalProperties=False,
)
assert build_json_schema(Main) == schema


def test_jsonschema_with_optional_discriminator_and_default_for_local_types():
@dataclass
class A:
value = "a"

@dataclass
class B:
value = "b"

@dataclass
class Main:
value: Optional[
Annotated[
Union[A, B],
Discriminator(field="value", include_supertypes=True),
]
] = None

schema = JSONObjectSchema(
title="Main",
properties={
"value": JSONSchema(
anyOf=[
JSONSchema(
anyOf=[
JSONObjectSchema(
type=JSONSchemaInstanceType.OBJECT,
title="A",
additionalProperties=False,
),
JSONObjectSchema(
type=JSONSchemaInstanceType.OBJECT,
title="B",
additionalProperties=False,
),
]
),
JSONSchema(
type=JSONSchemaInstanceType.NULL,
),
],
default=None,
),
},
additionalProperties=False,
)
assert build_json_schema(Main) == schema