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)
Description
For a property declared with
constin the schema, the C# generator with the Newtonsoft serializer preset renders the property as getter-only (backed by aprivate constfield) but its generatedJsonConverter.ReadJsonstill 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 170CS0200errors, making the whole output uncompilable.How to reproduce
Generated output (abridged) — does not compile
Compiler error
Expected behavior
Generated code should compile. Either:
const-backed properties with a settable accessor (e.g.private set/init), orconstproperties in the NewtonsoftReadJson(the value is fixed, so there is nothing to read into it).Impact / workaround
Every
constdiscriminator property yields aCS0200. Workaround we are using: a custom preset that strips thevalue.<Prop> = ...;block fromReadJsonfor properties whose schema hasconstset — which compiles and round-trips correctly. Happy to share the preset if useful.Environment
@asyncapi/modelina5.10.1 (also reproduces via@asyncapi/cli6.0.2, which bundles Modelina)