microsoft / microsoft/typespec

Add a decorator to make optional properties required

Open
#1,513 0 comments 0 reactions 0 assignees View on GitHub
design:needed
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

I spent some time today trying to implement the data modeling scheme described in [this ASC 2022 talk](https://youtu.be/6ukZEUBRpqo) in Cadl. I think Cadl handles this pretty well except that it does not have a means of marking properties of composed models as "required" when they are optional in their source.

There are probably a number of ways to provide this capability, but I think the most straightforward way would be to add a decorator on a model that could name properties to be made required.

An example of how this could be used:
```
model WidgetProperties {
weight?: int32;
color?: "red" | "blue";
}

// Request body for "createWidget"
@required("weight")
model NewWidget {
...WidgetProperties
}

// Request body for "updateWidget"
model WidgetPatch {
...WidgetProperties
}

// Response body for createWidget, getWidget, and updateWidget
@required("weight")
model Widget {
id: string;
state: "available" | "backordered" | "cancelled";
...WidgetProperties
}
```

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.