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: Any invalid message in a response crashed the entire response #27

Closed
wants to merge 1 commit into from

Conversation

hoh
Copy link
Member

@hoh hoh commented Sep 22, 2022

The code serving the API may return messages that are not valid according the latest version of the schemas. Ignore them with a warning instead of failing the parsing of the entire response.

Closes aleph-im/aleph-client#108

The code serving the API may return messages that are not valid according the latest version of the schemas. Ignore them with a warning instead of failing the parsing of the entire response.
@hoh hoh added the bug Something isn't working label Sep 22, 2022
@hoh hoh requested a review from odesenfans September 22, 2022 14:18
@@ -440,5 +445,22 @@ class MessagesResponse(BaseModel):
pagination_per_page: int
pagination_item: str

@validator("messages", pre=True)
def handle_invalid_messages(cls, v: List[Dict]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't assume that v is a list of dicts coming into this function. It should be Any with an isinstance check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, not 100% sure on this: are we sure that the validator is not run a second time on the return value of this validator? I wouldn't be surprised it it's the case.

result = []
for message_raw in v:
try:
message = Message(**message_raw)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not Message.parse_obj(message_raw)? iirc that would avoid the KeyError.

Comment on lines +244 to +249
path = Path(
os.path.abspath(
os.path.join(__file__, "../messages/contains_invalid_messages.json")
)
)
with open(path) as fd:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixing Path and os.path is weird. Also, pretty sure the join would not work because you never specify to take the parent directory of __file__?

Suggested change
path = Path(
os.path.abspath(
os.path.join(__file__, "../messages/contains_invalid_messages.json")
)
)
with open(path) as fd:
path = Path(__file__).parent.parent / "messages" / "contains_invalid_messages.json"
with path.open() as fd:

@hoh
Copy link
Member Author

hoh commented Sep 23, 2022

I am wondering if this would not make more sense in aleph-client, see the linked Pull Request above.

@hoh
Copy link
Member Author

hoh commented Sep 23, 2022

Replaced by a fix in Aleph-client.

@hoh hoh closed this Sep 23, 2022
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 this pull request may close these issues.

get_messages fails if any one of the messages does not fit the specification
2 participants