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

Support IsRequired attribute #129

Open
mdrgeniebelt opened this issue Feb 12, 2021 · 1 comment
Open

Support IsRequired attribute #129

mdrgeniebelt opened this issue Feb 12, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@mdrgeniebelt
Copy link

mdrgeniebelt commented Feb 12, 2021

Hi, me again with another question.

Is it possible to enforce nullability in schema of reference types?

This is our contract in code:

[DataContract(Name = "accountEvent")]
public class AccountEvent
{
    [DataMember(Name = "id")]
    public Guid Id { get; set; }
    
    [DataMember(Name = "name")]
    public string Name { get; set; }
    
    [DataMember(Name = "email")]
    public string Email { get; set; }
    
    [DataMember(Name = "confirmed")]
    public bool Confirmed { get; set; }

    [DataMember(Name = "phoneNumber", IsRequired = false)]
    public string? PhoneNumber { get; set; } = null;
}

Schema registered in registry:

{
    "type": "record",
    "name": "accountEvent",
    "fields": [
        {
            "name": "confirmed",
            "type": "boolean"
        },
        {
            "name": "email",
            "type": "string"
        },
        {
            "name": "id",
            "type": {
                "type": "string",
                "logicalType": "uuid"
            }
        },
        {
            "name": "name",
            "type": "string"
        },
        {
            "name": "phoneNumber",
            "type": "string"
        }
    ]
}

I was trying to use IsRequired but it didn't change anything (as expected after reading its documentation ;)).

What we would like to achieve is:

{
    "name": "phoneNumber",
    "type": [ "string", "null" ]
}

I'd like to help with implementing it - if you want help of course. Just let me know if that is possible or not because you'll probably know :)

@dstelljes
Copy link
Member

We’re hoping to ship support for nullable reference types with 8.0.0 soon (see #102). Currently, it’s all-or-nothing—you can set the resolver to follow .NET’s nullable semantics...

using Chr.Avro.Abstract;
using Chr.Avro.Confluent;
using Chr.Avro.Resolution;
using Confluent.Kafka;
using Confluent.SchemaRegistry;

var registry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { ... });

var typeResolver = new ReflectionResolver(
  resolveReferenceTypesAsNullable: true  // schema builder will produce ["null", ...] for all reference types
);

var schemaBuilder = new SchemaBuilder(typeResolver: typeResolver);
var serializerBuilder = new SchemaRegistrySerializerBuilder(registry, schemaBuilder: schemaBuilder)
var producerBuilder = new ProducerBuilder<Ignore, SomeValue>();

await builder.SetAvroValueSerializer(serializerBuilder, "test-subject", registerAutomatically: AutomaticRegistrationBehavior.Always);

... but there isn’t a way to pick and choose fields short of modifying the abstract schema.

It’d be nice to support [IsRequired] as well. I’m leery of possible contradictions with nullable types (e.g. https://stackoverflow.com/a/7767083), but I think they can both be implemented with an IsNullable property on FieldResolution.

@dstelljes dstelljes linked a pull request Feb 10, 2022 that will close this issue
@dstelljes dstelljes changed the title [Question] Nullable reference types Support IsRequired attribute Feb 10, 2022
@dstelljes dstelljes added the enhancement New feature or request label Feb 10, 2022
@dstelljes dstelljes modified the milestone: 10.0.0 Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants