-
-
Notifications
You must be signed in to change notification settings - Fork 60
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
NameSpaceRequiredException when reusing child class #246
Comments
You're welcome. The error from dataclasses_avroschema.avrodantic import AvroBaseModel
class ChildA(AvroBaseModel):
id: int
class Meta:
namespace = "com.example"
class ChildB(AvroBaseModel):
id: int
class Meta:
namespace = "com.example"
class Parent(AvroBaseModel):
x: ChildA | ChildB
y: ChildA | ChildB
d = Parent(x=ChildA(id=42), y=ChildB(id=43))
print(d.serialize(serialization_type="avro-json"))
# >>> b'{"x": {"com.example.ChildA": {"id": 42}}, "y": {"com.example.ChildA": {"id": 43}}}' |
Thanks for the swift reply! Maybe I reduced the example above too much: I'm using the model generator to generate ChildA/ChildB/Parent from an .asvc file (which in turn is generated from an .avdl definition). In this case, only the top-level record has a Meta class with a namespace.
I guess avro-tools's behavior is pretty much set in stone, so the question is whether 2) or 3) should be fixed? It feels like 3) is the culprit here (hence my minimal example focusing on this), but I'm happy to be convinced otherwise. :) |
You're welcome. The problem here is that when there are "schema relationships" and a |
I see that the namespace is required at the moment, but I don't think I don't exactly understand why that's the case? It seems like the existing type could just be reused then? If I apply the patch below, things just work for me (except for a test that basically matches the problem here and expects an error). patch.diff
Here's a full repro that matches more closely what I'm doing: from pathlib import Path
from subprocess import run
import json
if False:
# generate avsc
Path("example.avdl").write_text("""
@namespace("com.example")
protocol MyProtocol {
record Child {
int id;
}
record Parent {
union { null, Child } x;
union { null, Child } y;
}
}
""")
run(["java", "-jar", "avro-tools-1.11.1.jar", "idl2schemata", "./example.avdl"], check=True)
else:
# same as above, just hardcoded.
Path("Parent.avsc").write_text("""
{
"type" : "record",
"name" : "Parent",
"namespace" : "com.example",
"fields" : [ {
"name" : "x",
"type" : [ "null", {
"type" : "record",
"name" : "Child",
"fields" : [ {
"name" : "id",
"type" : "int"
} ]
} ]
}, {
"name" : "y",
"type" : [ "null", "Child" ]
} ]
}
""")
# Convert avsc to Python
from dataclasses_avroschema import ModelGenerator
model_generator = ModelGenerator()
schema = json.loads(Path("Parent.avsc").read_text())
result = model_generator.render(schema=schema)
Path("generated.py").write_text(result)
# Try to serialize generated model
from generated import Parent, Child
d = Parent(
x=Child(id=42),
y=Child(id=43),
)
print(d.serialize(serialization_type="avro-json")) # NameSpaceRequiredException |
Yes, the existing type can be reused so you don鈥檛 have to repeat the whole schema again, instead use the |
Hi there! First off, thank you for the fantastic library. 馃嵃
I'm running into the following issue in a migration to dataclasess-avroschema:
Removing
y
fixes the issue and correctly produces:The problem seems to be that
exist_type
turnsTrue
, but I don't understand enough of the codebase to know what the correct fix is. 馃槂The text was updated successfully, but these errors were encountered: