Skip to content
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

[csharp] Mark optional fields in generated client properties as nullable #17266

Conversation

ryanisaacg
Copy link

Resolves #4816

Mark all optional properties of in the generated C# client as nullable, with the ?marker. For reference types, this enables C# 8's nullable reference types feature which is a nice bonus.

For value types, this fixes an issue with optional values. By default, an optional integer will be expressed as an int in C# with EmitDefaultValue = false. This means that a value of 0 is used to indicate the property is absent, which prevents actually indicating "set this value to 0". This PR emits an int?, which uses null to indicate absence. 0 can now be properly expressed.

This change seems like a breaking change, unless it's put behind some kind of generator flag. I'm perfectly happy to add a generator flag or try to retarget this PR to the 8.0.x branch, whichever would be better.

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

@devhl-labs
Copy link
Contributor

This is an improvement, so I don't object, but I think it is a work around. The better fix is to use wrap not required properties and parameters in a struct to track whether or not it was set ie var foo = new Optional<int?>(); This allows you to explicitly set things to 0 and null. If the value was never set (as tracked by the Optional struct), then serializing should not add the property to the output. I already implemented this in generichost #16810 but it is not a small change.

@wing328
Copy link
Member

wing328 commented Mar 31, 2024

I've filed #18258 to set primitive types to nullable. If that works for the use cases described, then we can skip this PR.

@ryanisaacg
Copy link
Author

I'm no longer working on the project that prompted this PR and an alternative was merged, so I'll close it.

@ryanisaacg ryanisaacg closed this May 15, 2024
@ryanisaacg ryanisaacg deleted the nullable-property-annotations branch May 15, 2024 03:22
@wing328
Copy link
Member

wing328 commented May 15, 2024

@ryanisaacg that's ok. Thanks again for the PR. All the best to your projects.

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.

[BUG][C#] Should use nullable types for non-required properties
3 participants