Skip to content

Commit

Permalink
python: Don't override message_schema for json schemas (#271)
Browse files Browse the repository at this point in the history
I watched @amacneil today as he did this:

```python
chan = Channel("topic", message_encoding="msgpack", schema={ ... })
```

That doesn't work as expected, because we automatically override the
message_encoding to be json when you provide a schema that looks like a
dict jsonschema. Which is a silly thing to do, in retrospect. The schema
encoding does not imply a message encoding.
  • Loading branch information
gasmith authored Mar 5, 2025
1 parent 3f782c4 commit 1b2fd87
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions python/foxglove-sdk/python/foxglove/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
Create a new channel for logging messages on a topic.
:param topic: The topic name.
:param message_encoding: The message encoding. Optional and ignored if
:param message_encoding: The message encoding. Optional if
:py:param:`schema` is a dictionary, in which case the message
encoding is presumed to be "json".
:param schema: A definition of your schema. Pass a :py:class:`Schema`
Expand Down Expand Up @@ -118,11 +118,13 @@ def _normalize_schema(
elif isinstance(schema, dict):
if schema.get("type") != "object":
raise ValueError("Only object schemas are supported")

return "json", Schema(
name=schema.get("title", "json_schema"),
encoding="jsonschema",
data=json.dumps(schema).encode("utf-8"),
return (
message_encoding or "json",
Schema(
name=schema.get("title", "json_schema"),
encoding="jsonschema",
data=json.dumps(schema).encode("utf-8"),
),
)
else:
raise ValueError(f"Invalid schema type: {type(schema)}")

0 comments on commit 1b2fd87

Please sign in to comment.