Skip to content

C#: Newtonsoft serializer preset emits non-compiling ReadJson for const properties (assigns to getter-only property → CS0200) #2589

Description

@tkrisztian95

Description

For a property declared with const in the schema, the C# generator with the Newtonsoft serializer preset renders the property as getter-only (backed by a private const field) but its generated JsonConverter.ReadJson still assigns to it. The result does not compile: CS0200 — Property or indexer cannot be assigned to — it is read only.

This affects any schema that uses const, which is common for discriminated unions / tagged messages (e.g. { "type": { "const": "..." } }). Against a real AsyncAPI contract this produced 170 CS0200 errors, making the whole output uncompilable.

How to reproduce

import { CSharpGenerator, CSHARP_NEWTONSOFT_SERIALIZER_PRESET } from '@asyncapi/modelina';

const generator = new CSharpGenerator({ presets: [CSHARP_NEWTONSOFT_SERIALIZER_PRESET] });

const schema = {
  $id: 'AuthConfigMediation',
  type: 'object',
  properties: { type: { type: 'string', const: 'mediation' } },
  required: ['type'],
  additionalProperties: false,
};

const models = await generator.generate(schema);
for (const m of models) console.log(m.result);

Generated output (abridged) — does not compile

public partial class AuthConfigMediation
{
  private const string type = "mediation";

  public string Type            // getter-only
  {
    get { return type; }
  }
}

public class AuthConfigMediationConverter : JsonConverter<AuthConfigMediation>
{
  public override AuthConfigMediation ReadJson(JsonReader reader, System.Type objectType, AuthConfigMediation existingValue, bool hasExistingValue, JsonSerializer serializer)
  {
    JObject jo = JObject.Load(reader);
    AuthConfigMediation value = new AuthConfigMediation();

    if (jo["type"] != null) {
      value.Type = jo["type"].ToObject<string>(serializer);   // CS0200: 'Type' is read only
    }
    return value;
  }
  // ...
}

Compiler error

error CS0200: Property or indexer 'AuthConfigMediation.Type' cannot be assigned to -- it is read only

Expected behavior

Generated code should compile. Either:

  • render const-backed properties with a settable accessor (e.g. private set / init), or
  • omit the assignment for const properties in the Newtonsoft ReadJson (the value is fixed, so there is nothing to read into it).

Impact / workaround

Every const discriminator property yields a CS0200. Workaround we are using: a custom preset that strips the value.<Prop> = ...; block from ReadJson for properties whose schema has const set — which compiles and round-trips correctly. Happy to share the preset if useful.

Environment

  • @asyncapi/modelina 5.10.1 (also reproduces via @asyncapi/cli 6.0.2, which bundles Modelina)
  • Node v24.17.0
  • Target: .NET Framework 4.7.2, but the error is language-level (not framework-specific)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions