C#: Newtonsoft serializer preset emits non-compiling ReadJson for `const` properties (assigns to getter-only property → CS0200)
- Dominant language
- TypeScript
- Stars
- 448
- Forks
- 242
- Avg merge
- 15m
- Merged PRs (30d)
- 7
Description
## 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
```js
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
```csharp
public partial class AuthConfigMediation
{
private const string type = "mediation";
public string Type // getter-only
{
get { return type; }
}
}
public class AuthConfigMediationConverter : JsonConverter
{
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(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. = ...;` 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)
Contributor guide
Research direction
Start at CSharpGenerator and CSHARP_NEWTONSOFT_SERIALIZER_PRESET, then run the reproduction schema from the issue to inspect the generated model and JsonConverter.ReadJson. The generated C# should compile without assigning to the getter-only const-backed property, while preserving correct round-tripping behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100