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
Description
When generating C# models for a property declared with
constin the schema, the C# generator with auto-implemented properties enabled (autoImplementedProperties: true, e.g. the--csharpAutoImplementCLI flag, commonly combined with--csharpNewtonsoft) renders an invalid property declaration that does not compile:A C#
constfield cannot have property accessors ({ get; }). This is a compile error (e.g.CS0107/syntax error) — aconstis 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
constproperties to compile under the Newtonsoft preset (see "Related" below).How to reproduce
CLI equivalent:
Generated output — does not compile
Expected behavior
A
constproperty should be rendered as a plain const field (matching what therecordrenderer already produces):Root cause
In
src/generators/csharp/renderers/ClassRenderer.ts, the auto-implemented-properties branch of thepropertypreset emits theconstdeclaration with{ ${getter} }accessors. Therecordpreset (RecordRenderer.ts) already renders the same case correctly without accessors.Related
constproperties (assigns to getter-only property → CS0200) #2589 — NewtonsoftReadJsonassigns to the (read-only)constproperty →CS0200/CS0131. Even after fixing this declaration bug, the Newtonsoft converter still emits a non-compiling assignment, so both need to be addressed for--csharpNewtonsoft+constoutput to compile.Environment