fix(csharp): render const properties as valid, compilable C##2592
Open
dl-ezo wants to merge 1 commit into
Open
fix(csharp): render const properties as valid, compilable C##2592dl-ezo wants to merge 1 commit into
dl-ezo wants to merge 1 commit into
Conversation
`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>
✅ Deploy Preview for modelina canceled.
|
Contributor
There was a problem hiding this comment.
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.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
constproperties produced non-compiling C# in two independent places. This PR fixes both so that schemas usingconst(common for discriminators / tagged messages) generate compilable C#, including under--csharpNewtonsoft/--csharpAutoImplement.Invalid declaration with auto-implemented properties (
ClassRenderer): aconstproperty was emitted asA C#
constis a field and cannot have property accessors, so this does not compile. It is now rendered as a plain const field (matching therecordrenderer):Assignment to read-only
constin the NewtonsoftReadJson(NewtonsoftSerializerPreset): the generatedJsonConverter.ReadJsonassigned to the read-onlyconstproperty, e.g.The assignment is now skipped for
constproperties, since their value is fixed by the schema and there is nothing to deserialize into them.Related Issue
constproperties emit invalidpublic const T X { get; } = ...with auto-implemented properties #2591 — invalidpublic const T X { get; } = ...declaration with auto-implemented properties.constproperties (assigns to getter-only property → CS0200) #2589 — NewtonsoftReadJsonassigns to the read-onlyconstproperty (CS0200/CS0131).The fix for the Newtonsoft part follows exactly the workaround described by @tkrisztian95 in #2589 (skip the
value.<Prop> = ...;assignment inReadJsonforconstproperties). Thanks for the clear analysis and reproduction there. 🙏Checklist
npm run lint).npm run test).Additional Notes
constproperties under the Newtonsoft preset.CSharpGenerator.spec.ts: aconstproperty withautoImplementedPropertiesrenders as a valid const field (no accessors).NewtonsoftSerializerPreset.spec.ts:ReadJsondoes not assign toconstproperties (plus snapshot update).