Skip to content

Commit

Permalink
Allow explicitly-configured fields to be included in output when comb…
Browse files Browse the repository at this point in the history
…ined with `signature_only=True`.
  • Loading branch information
seandstewart committed Jan 27, 2022
1 parent 2c2db19 commit 6049917
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions typic/ext/schema/field.py
Expand Up @@ -79,7 +79,7 @@ class SchemaType(str, enum.Enum):

class _Serializable:
__slots__ = ()
__serde_flags__ = SerdeFlags(exclude={"proto", "primitive", "tojson"})
__serde_flags__ = SerdeFlags(signature_only=True)

proto: SerdeProtocol
primitive: SerializerMethodT
Expand Down Expand Up @@ -139,7 +139,7 @@ class StringFormat(str, enum.Enum):
class BaseSchemaField(_Serializable):
"""The base JSON Schema Field."""

__serde_flags__ = SerdeFlags(omit=(None, NotImplemented))
__serde_flags__ = SerdeFlags(fields=("type",), omit=(None, NotImplemented))

type: ClassVar[SchemaType] = NotImplemented
format: Optional[str] = None
Expand Down
18 changes: 9 additions & 9 deletions typic/serde/resolver.py
Expand Up @@ -303,6 +303,11 @@ def _get_configuration(self, origin: Type, flags: SerdeFlags) -> SerdeConfig:
# This is probably a builtin and has no signature
fields: Dict[str, Annotation] = {}
hints = util.cached_type_hints(origin)
include = (
flags.fields
if isinstance(flags.fields, Mapping)
else {x: x for x in flags.fields}
)
for name, t in hints.items():
fields[name] = self.annotation(
t,
Expand All @@ -312,17 +317,12 @@ def _get_configuration(self, origin: Type, flags: SerdeFlags) -> SerdeConfig:
)

# Filter out any annotations which aren't part of the object's signature.
# Make sure to include any fields explicitly listed
if flags.signature_only:
fields = {x: fields[x] for x in fields.keys() & params.keys()}
keys = (fields.keys() & params.keys()) | include.keys()
fields = {x: fields[x] for x in keys if x in fields}
# Create a field-to-field mapping
fields_out = {x: x for x in fields}
# Make sure to include any fields explicitly listed
include = flags.fields
if include:
if isinstance(include, Mapping):
fields_out.update(include)
else:
fields_out.update({x: x for x in include})
fields_out = {x: include.get(x, x) for x in fields.keys() | include.keys()}
# Transform the output fields to the correct case.
if flags.case:
case = Case(flags.case)
Expand Down

0 comments on commit 6049917

Please sign in to comment.