question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default values support in codegen

See original GitHub issue

Support for default values were introduced in version 8.0.2 for schema generation, meaning that a class such as this:

public class MyRecord
{
    [DefaultValue(null)]
    public int? Value { get; set; }
}

Produces this schema, with the expected "default": null property:

{
  "type": "record",
  "name": "MyRecord",
  "fields": [
    {
      "name": "Value",
      "type": [
        "null",
        "int"
      ],
      "default": null
    }
  ]
}

What would be great is to also have the opposite. Given schema above, have the generated code include a [DefaultValue] attribute to match the schema. At the moment, here’s what the generated class looks like for the schema above when running dotnet avro generate:

public class MyRecord
{
    public int? Value { get; set; }
}

This is a problem, because any producer who uses that class will now produce a new schema which no longer includes the default value, and potentially start creating compatibility problems in the schema registry.

Could the dotnet avro generate command be updated to support default values from the schemas?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nicodeslandescommented, Mar 7, 2022

Sorry for the late reply, I’ve been sidetracked away from Avro these last few days. I’ve got 2 possible suggestions:

  1. Handle the “happy” path only, if the default value is null of a scalar, generate the [DefaultValue] attribute correctly. If we’re not in a supported scenario, issue a warning and don’t produce anything.
  2. For the unsupported case like the one you highlighted above, still issue a warning, but decorate the property with a new attribute [DefaultSchemaValueJson], which reproduce the default value as it is defined in the originating schema, and is used when producing the schema from the record class.

Option 1 would be good enough for most cases, and would be enough to ensure we have a good story when one team generate a schema from C# code, and another team wants to produce a C# class from that same schema. The 2nd team would be able to used that class without new, potentially incompatible schemas being published to the registry.

Option 2 would even push this further by allowing a producer to handle schemas that don’t come from a generated C# class.

0reactions
dstelljescommented, Mar 7, 2022

Yeah, the schema would just be the JSON string, but DefaultSchemaValueJsonAttribute would have to live somewhere.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling of default values · Issue #138 · deepmap/oapi- ...
Hi, do we have any update on this default value handling? It seems the current version still cannot set the parameter to default...
Read more >
how to put parameter default value into generated code in ...
the rcAppKey is null,but i want it be the default value "abc",like: String rcAppKey = "abc"; Common response = api. refresh(rcAppKey); the  ......
Read more >
codegen - MATLAB Coder
The code generator uses these example values to determine that the first input ... codegen generates C library and supporting files in the...
Read more >
Codegen Options - The rustc book
Supported values for this option are: tiny - Tiny code model. small - Small code model. This is the default model for majority...
Read more >
Using the GNU Compiler Collection (GCC): Code Gen Options
The default value is ' all '. The option is needed when the program extends the lifetime of a scoped local variable or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found