-
Notifications
You must be signed in to change notification settings - Fork 176
Fix required+readonly properties incorrectly included in initialization constructors #5424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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 <[email protected]>
Verify that required+readonly properties are correctly excluded from initialization constructors in generated code. Co-authored-by: ArcturusZhang <[email protected]>
The fix is validated by existing test cases in FirstTest-TypeSpec project. Co-authored-by: ArcturusZhang <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these files are incorrectly generated.
We should remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in commit 66575ab.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, we should remove this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in commit 66575ab.
ArcturusZhang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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 <[email protected]>
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. |
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.