Skip to content

Conversation

@Irame
Copy link

@Irame Irame commented Dec 23, 2023

This PR adds useObjectInitializerApproach to additional-properties for the csharp generator when using the generichost library. With this flag enabled the properties of generated model classes have the required keyword if they are required and use an init setter 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 the SetsRequiredMembers attribute to the other constructors.

This should fix the issue #17147 at least when using the generichost library.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    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.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@mandrean, @shibayan, @Blackclaws, @lucamazzanti, @iBicha

@Irame Irame changed the title Adds a useObjectInitializerApproach option as proposed in #17147 [Feature][Csharp][GenericHost] Option to be able to use object initializers for model instantiation Dec 23, 2023

{{/composedSchemas.oneOf}}
{{#useObjectInitializerApproach}}
{{#hasRequired}}
Copy link
Contributor

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?

Copy link
Author

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?

Copy link
Contributor

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.

Copy link
Author

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.

@Irame
Copy link
Author

Irame commented Dec 30, 2023

Ok, so based on the review I will remove the useObjectInitializerApproach option completely and replace it with net70OrLater.

@wing328
Copy link
Member

wing328 commented Jan 31, 2024

@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

@Irame Irame marked this pull request as ready for review February 6, 2024 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants