Skip to content

[BUG] C#: const properties emit invalid public const T X { get; } = ... with auto-implemented properties #2591

Description

@dl-ezo

Description

When generating C# models for a property declared with const in the schema, the C# generator with auto-implemented properties enabled (autoImplementedProperties: true, e.g. the --csharpAutoImplement CLI flag, commonly combined with --csharpNewtonsoft) renders an invalid property declaration that does not compile:

public const string EventType { get; } = "OnEntryStarted";

A C# const field cannot have property accessors ({ get; }). This is a compile error (e.g. CS0107/syntax error) — a const is a field, not a property.

This is a separate problem from the read-only assignment issue tracked in #2589, but both must be fixed for const properties to compile under the Newtonsoft preset (see "Related" below).

How to reproduce

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

const generator = new CSharpGenerator({ autoImplementedProperties: true });

const schema = {
  $id: 'Event',
  type: 'object',
  properties: { eventType: { type: 'string', const: 'OnEntryStarted' } },
  required: ['eventType'],
};

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

CLI equivalent:

asyncapi generate models csharp ./event.yaml -o ./out --namespace MyNs --csharpAutoImplement --csharpNewtonsoft

Generated output — does not compile

public partial class Event
{
  public const string EventType { get; } = "OnEntryStarted";
}

Expected behavior

A const property should be rendered as a plain const field (matching what the record renderer already produces):

public const string EventType = "OnEntryStarted";

Root cause

In src/generators/csharp/renderers/ClassRenderer.ts, the auto-implemented-properties branch of the property preset emits the const declaration with { ${getter} } accessors. The record preset (RecordRenderer.ts) already renders the same case correctly without accessors.

Related

Environment

  • Modelina: 5.10.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions