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

BGP state monitor sets Event values of incorrect types, causing strange Pydantic serialization errors #312

Closed
lunkwill42 opened this issue Jul 16, 2024 · 0 comments · Fixed by #313
Assignees
Labels
bug Something isn't working

Comments

@lunkwill42
Copy link
Member

When running Zino 2.0.0-beta.1 on real world devices, it intermittently logs a bunch of errors like this:

/home/mvold/.virtualenvs/zino2/lib/python3.11/site-packages/pydantic/main.py:415: UserWarning: Pydantic serializer warnings:
  Expected `Union[PortStateEvent, BGPEvent, BFDEvent, ReachabilityEvent, AlarmEvent, Event]` but got `BGPEvent` - serialized value may not be as expected
  Expected `enum` but got `str` - serialized value may not be as expected
  Expected `enum` but got `str` - serialized value may not be as expected
  Expected `Union[PortStateEvent, BGPEvent, BFDEvent, ReachabilityEvent, AlarmEvent, Event]` but got `BGPEvent` - serialized value may not be as expected
  Expected `enum` but got `str` - serialized value may not be as expected
  Expected `enum` but got `str` - serialized value may not be as expected
...

This appears to be mainly be caused by how this dataclass is used:

@dataclass
class BaseBGPRow:
peer_state: BGPOperState
peer_admin_status: BGPAdminStatus
peer_remote_address: IPAddress
peer_remote_as: int
peer_fsm_established_time: int

Instances of this class are fed raw string values (as collected through SNMP and parsed through the BGP mibs) as attribute values, not values of the actual annotated types.

Later, these values are fed directly into BGPEvent object attributes: But these objects are Pydantic model instances and do not expect strings as values for attributes such as bgpos and bgpas. As the data is serialized, it complains that these fields aren't the expected enum types BGPOperState and BGPAdminStatus:

class BGPEvent(Event):
type: Literal["bgp"] = "bgp"
remote_addr: Optional[IPAddress] = None
remote_as: Optional[int] = None
peer_uptime: Optional[int] = None
bgpos: Optional[BGPOperState] = None
bgpas: Optional[BGPAdminStatus] = None

@lunkwill42 lunkwill42 added the bug Something isn't working label Jul 16, 2024
lunkwill42 added a commit that referenced this issue Jul 16, 2024
This requires Pydantic >= 2.7.0, as this is the first version that
enables serialization warnings to be turned into errors/exceptions,
which will trigger this test.
lunkwill42 added a commit that referenced this issue Jul 16, 2024
This fixes #312 by type-casting incoming attributes on BaseBGPRow.
The expected Enum types have values designed to match the state strings
from the BGP MIBs, so coercing the strings as early as possible
should do.
@lunkwill42 lunkwill42 self-assigned this Jul 16, 2024
johannaengland pushed a commit that referenced this issue Jul 23, 2024
This requires Pydantic >= 2.7.0, as this is the first version that
enables serialization warnings to be turned into errors/exceptions,
which will trigger this test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant