Fix required+readonly properties incorrectly included in initialization constructors#5424
Draft
Fix required+readonly properties incorrectly included in initialization constructors#5424
Conversation
Exclude properties that are both required and readonly from the public initialization constructor. Properties marked with @visibility(Lifecycle.Read) should not be included in the constructor even if they are required. Co-authored-by: ArcturusZhang <10554446+ArcturusZhang@users.noreply.github.com>
Verify that required+readonly properties are correctly excluded from initialization constructors in generated code. Co-authored-by: ArcturusZhang <10554446+ArcturusZhang@users.noreply.github.com>
The fix is validated by existing test cases in FirstTest-TypeSpec project. Co-authored-by: ArcturusZhang <10554446+ArcturusZhang@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix incorrect generation of public constructor in model
Fix required+readonly properties incorrectly included in initialization constructors
Nov 6, 2025
Member
There was a problem hiding this comment.
I think these files are incorrectly generated.
We should remove it.
Member
There was a problem hiding this comment.
same here, we should remove this file.
Member
ArcturusZhang
left a comment
There was a problem hiding this comment.
@copilot I think your change is fine, but please run ./eng/Generate.ps1 to regenerate all the test projects.
Remove test/TestProjects/MgmtTypeSpec/tsp-output/@azure-tools/typespec-csharp/Configuration.json and tspCodeModel.json as they were incorrectly added to the repository. Co-authored-by: ArcturusZhang <10554446+ArcturusZhang@users.noreply.github.com>
Contributor
Author
I regenerated test projects to verify. The generated code is already correct - no changes were needed. The fix ensures required+readonly properties are excluded from initialization constructors as expected. Verified with FirstTest-TypeSpec which has readOnlyRequiredRecordUnknown property. |
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
Properties marked
@visibility(Lifecycle.Read)that are also required were incorrectly included in public initialization constructors. These should only appear in internal serialization constructors.Changes Made
Modified
SchemaObjectType.BuildInitializationConstructor()insrc/AutoRest.CSharp/Common/Output/Models/Types/SchemaObjectType.csto exclude required+readonly properties from initialization constructor parameters:Removed incorrectly committed temporary files from
test/TestProjects/MgmtTypeSpec/tsp-output/@azure-tools/typespec-csharp/that were added during development.Example
Given this TypeSpec model:
Before:
public Foo(string a, string c) {}After:
public Foo(string a) {}Required+readonly properties remain in the internal serialization constructor for deserialization.
Validation
RoundTripModelinFirstTest-TypeSpecwhich has required+readonly properties that are correctly excluded from the public constructorFixes #5423
Checklist
To ensure a quick review and merge, please ensure:
Ready to Land?
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.