microsoft / microsoft/typespec

[http-server-csharp] Optional properties from generic base model dropped when using 'extends' or 'is'

Open
#10,400 0 comments 0 reactions 1 assignee Claimed by @markcowl View on GitHub
bug emitter:service:csharp triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

### Describe the bug

When a model extends a generic base model that contains optional properties, the emitter drops the optional properties from the generated C# class. Required properties are emitted correctly.

### Reproduction

```typescpec
import "@typespec/http";
using TypeSpec.Http;

model Message {
Source: string;
Type: string;
Reference?: string;
Timestamp?: offsetDateTime;
Data: TData;
}

model Report {
value: string;
}

@service(#{ title: "TestService" })
namespace TestService;

model ReportMessage is Message;
```

tspconfig.yaml

```yaml
emit:
- "@typespec/http-server-csharp"
options:
"@typespec/http-server-csharp":
output-type: "models"
emit-mocks: "none"
skip-format: true
```

Expected generated C#

```csharp
public partial class ReportMessage
{
public string Source { get; set; }
public string Type { get; set; }
public string? Reference { get; set; } // missing
public DateTimeOffset? Timestamp { get; set; } // missing
public Report Data { get; set; }
}
```

Actual generated C#

```csharp
public partial class ReportMessage
{
public string Source { get; set; }
public string Type { get; set; }
public Report Data { get; set; }
}
```

Environment

`@typespec/http-server-csharp`: 0.58.0-alpha.27
`@typespec/compiler`: 1.11.0
OS: Windows

### Checklist

- [x] Follow our [Code of Conduct](https://github.com/microsoft/typespec/blob/main/CODE_OF_CONDUCT.md)
- [x] Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/Microsoft/typespec/discussions).
- [x] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.