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
Headless serialization with customs property name #105
Comments
Hello, |
Hi @Metalaka, I have written a test covering your case:
As you can see, I am using different schemas for serialization and deserialization. The client model uses the DataMember attribute and the functionality works correctly. Does it help in your case? |
Hello, Your test use the same data types so there is no reason to the deserialization to fail. I added different data type and different property order. So the test will be closer to my real case. [Fact]
public void Component_HeadlessSerializationUsingTwoModelsWithDifferentPropertyOrder_CorrectlyDeserialized()
{
//Arrange
ClientUserDto toSerialize = _fixture.Create<ClientUserDto>();
//string clientSchema = AvroConvert.GenerateSchema(typeof(ClientUserDto));
string serverSchema = AvroConvert.GenerateSchema(typeof(ServerUserDto));
//Act
var result = AvroConvert.SerializeHeadless(toSerialize, serverSchema);
var deserialized = AvroConvert.DeserializeHeadless<ServerUserDto>(result, serverSchema);
//Assert
Assert.NotNull(result);
Assert.NotNull(deserialized);
Assert.Equal(toSerialize.FirstName, deserialized.Foo);
Assert.Equal(toSerialize.Id, deserialized.Bar);
}
public class ClientUserDto
{
[DataMember(Name = "Bar")]
public int Id { get; set; }
[DataMember(Name = "Foo")]
public string FirstName { get; set; }
}
public class ServerUserDto
{
public string Foo { get; set; }
public int Bar { get; set; }
} Thanks for your time |
To give you more insights: the property order is not a problem at all. The problem are the property names. As I understand, you expect to take into consideration the DataMember attributes, but this is opposite to the Avro specification:
var result = AvroConvert.Serialize(toSerialize);
var deserialized = AvroConvert.Deserialize<ServerUserDto>(result); This is the simplest way of ensuring schema compatibility.
I will try to dig deeper into this topic, but the general rule is to stick to the writer schema and use this one in the schema registry. Please let me know if I got something wrong. |
Closed due to inactivity. |
Hello,
I want to make a headless serialization with customs properties name.
I have a DTO class with all properties of a shared schema but with custom names and different property order.
The DTO properties are bind to the schema properties with a DataMember attribute.
Because of my use case, I must use the client schema for the serialization.
In short, I have to communicate with one client in two different environment. And the order of properties is a little bit different between environments.
this is what my DTO looks like
what i do to serialize my payload
And for example, what i did for the read flow. It works because
JsonConvert
handleDataMemberAttribute
I tried to add the feature but I don't know if it's the right way: Metalaka@c29e645
Maybe you wanna use
AvroContractResolver.ResolveMembers(Type)
and add support for more options?Do you have any plan to support that kind of serialization ?
The text was updated successfully, but these errors were encountered: