Skip to content

fix(csharp): render const properties as valid, compilable C##2592

Open
dl-ezo wants to merge 1 commit into
asyncapi:masterfrom
dl-ezo:fix/csharp-const-property-compilation
Open

fix(csharp): render const properties as valid, compilable C##2592
dl-ezo wants to merge 1 commit into
asyncapi:masterfrom
dl-ezo:fix/csharp-const-property-compilation

Conversation

@dl-ezo

@dl-ezo dl-ezo commented Jun 24, 2026

Copy link
Copy Markdown

Description

const properties produced non-compiling C# in two independent places. This PR fixes both so that schemas using const (common for discriminators / tagged messages) generate compilable C#, including under --csharpNewtonsoft / --csharpAutoImplement.

  1. Invalid declaration with auto-implemented properties (ClassRenderer): a const property was emitted as

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

    A C# const is a field and cannot have property accessors, so this does not compile. It is now rendered as a plain const field (matching the record renderer):

    public const string EventType = "OnEntryStarted";
  2. Assignment to read-only const in the Newtonsoft ReadJson (NewtonsoftSerializerPreset): the generated JsonConverter.ReadJson assigned to the read-only const property, e.g.

    value.EventType = jo["eventType"].ToObject<string>(serializer); // CS0200 / CS0131

    The assignment is now skipped for const properties, since their value is fixed by the schema and there is nothing to deserialize into them.

Related Issue

The fix for the Newtonsoft part follows exactly the workaround described by @tkrisztian95 in #2589 (skip the value.<Prop> = ...; assignment in ReadJson for const properties). Thanks for the clear analysis and reproduction there. 🙏

Checklist

  • The code follows the project's coding standards and is properly linted (npm run lint).
  • Tests have been added or updated to cover the changes.
  • Documentation has been updated to reflect the changes.
  • All tests pass successfully locally. (npm run test).

Additional Notes

  • Both bugs are independent: fixing only one still leaves the other producing non-compiling output for const properties under the Newtonsoft preset.
  • New/updated tests:
    • CSharpGenerator.spec.ts: a const property with autoImplementedProperties renders as a valid const field (no accessors).
    • NewtonsoftSerializerPreset.spec.ts: ReadJson does not assign to const properties (plus snapshot update).
  • No documentation change is needed as this fixes existing behavior of the C# generator/preset rather than introducing new behavior.

`const` properties produced non-compiling C# in two places:

- With auto-implemented properties, the declaration was emitted as
  `public const T X { get; } = ...;`. A `const` field cannot have
  property accessors, so this does not compile (asyncapi#2591).
- The Newtonsoft serializer preset assigned to the read-only `const`
  property inside `ReadJson`, causing CS0200/CS0131 (asyncapi#2589).

Render const properties as a plain `public const T X = ...;` field
(matching the record renderer) and skip the assignment for const
properties in the Newtonsoft `ReadJson`, since their value is fixed by
the schema.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@netlify

netlify Bot commented Jun 24, 2026

Copy link
Copy Markdown

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit 6a86186
🔍 Latest deploy log https://app.netlify.com/projects/modelina/deploys/6a3ba1b3054f33000867b2de

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant