Skip to content

Add required keyword and nullable annotations to http-server-csharp model properties#9439

Open
aleksator wants to merge 2 commits intomicrosoft:mainfrom
aleksator:generate_required_property_with_nullable_annotations
Open

Add required keyword and nullable annotations to http-server-csharp model properties#9439
aleksator wants to merge 2 commits intomicrosoft:mainfrom
aleksator:generate_required_property_with_nullable_annotations

Conversation

@aleksator
Copy link
Copy Markdown

Hi folks!

Trying out TypeSpec with http-server-csharp generation in one of the projects - enjoying the generated code so far. Great job!

The most important improvement for me is to have required and nullable annotations for C# model properties as described in #7710, which this PR implements.

@aleksator
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@aleksator
Copy link
Copy Markdown
Author

I found that after adding required to exception properties for the definition like

@error
model ConflictError {
  @statusCode statusCode: 409;
  message: string;
}

creating an instance of the exception

throw new ConflictError("message");

produces error

Error CS9035 : Required member 'ConflictError.MessageProp' must be set in the object initializer or attribute constructor.

Fixed this by adding [SetsRequiredMembers] annotation to exception constructors.

@markcowl
Copy link
Copy Markdown
Contributor

@aleksator This is one of the changes we really want to make to the emitter, Unfortunately, it has impacts on the reusability of resource types in PATCH operations and other places where visibility transforms apply to requests and responses. We will need to coordinate this with changes to the controllers and implementation interfaces, and creating new types when they are used in instances where optionality changes. You can track this change here: #5988

Copy link
Copy Markdown
Contributor

@markcowl markcowl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aleksator This is one of the changes we really want to make to the emitter, Unfortunately, it has impacts on the reusability of resource types in PATCH operations and other places where visibility transforms apply to requests and responses. We will need to coordinate this with changes to the controllers and implementation interfaces, and creating new types when they are used in instances where optionality changes. You can track this change here: #5988

@microsoft-github-policy-service microsoft-github-policy-service Bot added the stale Mark a PR that hasn't been recently updated and will be closed. label Mar 29, 2026
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

Hi @@aleksator. Your PR has had no update for 30 days and it is marked as a stale PR. If it is not updated within 30 days, the PR will automatically be closed. If you want to refresh the PR, please remove the stale label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Mark a PR that hasn't been recently updated and will be closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants