-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[Feature][Csharp][GenericHost] Option to be able to use object initializers for model instantiation #17467
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: master
Are you sure you want to change the base?
Conversation
…proper use of the object initializer
|
|
||
| {{/composedSchemas.oneOf}} | ||
| {{#useObjectInitializerApproach}} | ||
| {{#hasRequired}} |
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.
Does this have to be behind the new useObjectInitialzerApproach switch? I think you can use net80OrLater instead.
Why is it also behind hasRequired?
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 put it behind the hasRequired switch so it is only generated when the normal constructor does have some mandatory arguments to prevent ambiguity.
And I put it behind the useObjectInitializerApproach switch to not generate this constructor without also adding the required keyword to properties. This is necessary to prevent the instantiation of the model without specifying the required fields.
Yes, the useObjectInitializerApproach could be replaced by the net80OrLater or even the net70OrLater switch, but I didn't want to introduce any changes to currently generated code. Should I change 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.
I haven't used the feature myself, but I don't see any reason to not use net70OrLater since it appears to just be a syntax change.
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.
Yes its basically a syntax change and the old syntax can still be used.
The only thing I see that is different using the new approach is, that OnCreated is not called. I didn't include it in the parameter less constructor, because it would get called before the required properties are set in contrast to the current constructor where it gets called after all properties are set.
|
Ok, so based on the review I will remove the |
|
@Irame can you please resolve the merge conflicts when you've time? if no further feedback from anyone, I'll merge it before the upcoming 7.3.0 release. cc @devhl-labs |
This PR adds useObjectInitializerApproach to
additional-propertiesfor the csharp generator when using thegenerichostlibrary. With this flag enabled the properties of generated model classes have therequiredkeyword if they are required and use aninitsetter if they are read-only. It also generates a parameter less constructor if there are required properties in the model to allow the use of the object initializer and adds theSetsRequiredMembersattribute to the other constructors.This should fix the issue #17147 at least when using the
generichostlibrary.PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)@mandrean, @shibayan, @Blackclaws, @lucamazzanti, @iBicha