-
Notifications
You must be signed in to change notification settings - Fork 65
Description
Background
This is a case during service linker migration. We migrate the x-nullable in swagger to | null in typespec. I'd like to clarify the interception on nullable in TypeSpec for Azure services.
- optional nullable property: https://github.com/Azure/azure-rest-api-specs/blob/main/specification/servicelinker/resource-manager/Microsoft.ServiceLinker/preview/2024-07-01-preview/servicelinker.json#L1489
- required nullble property: https://github.com/azure/azure-rest-api-specs/blob/main/specification/search/data-plane/Azure.Search/preview/2025-05-01-preview/searchservice.json#L11278-L11286
Optional vs nullable in Azure
At the REST/service API layer, nullable and optional are distinct and independent concepts. Optional indicates that the property (key/value pair) does not need to be included in the payload. Nullable means that the property’s value may explicitly be set to null.
But. Semantically, for Azure services treat a missing field and a field with a value of null as identical. Also offline confirmed with @johanste - autorest never distinguished between the two. Azure services almost universally (with the exception for json-merge-patch payloads which are sent from the client and never received by the client), are not allowed to have semantically meaningful null values. Which means that the inability to distinguish between null and no value ends up not being relevant.
Azure Guideline - https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#json-null-resquest-values
DO NOT send JSON fields with a null value from the service to the client. Instead, the service should just not send this field at all (this reduces payload size).
Semantically, Azure services treat a missing field and a field with a value of null as identical.
Open questions
I'd like to clarify followings:
- Should we tranlate
x-nullableto| nullin typespec? Do we distinguish optionality and nullable in typespec? - We have no-nullable linter do we expect to translate
x-nullableproperty to optional one in typespec to bypass this linter?
- ❌Do not use: prop: string | null;
- ✅ Do use: prop?: string;