Skip to content

Should we translate x-nullable to | null for Azure services? #3121

@MaryGao

Description

@MaryGao

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 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-nullable to | null in typespec? Do we distinguish optionality and nullable in typespec?
  • We have no-nullable linter do we expect to translate x-nullable property to optional one in typespec to bypass this linter?
- ❌Do not use: prop: string | null;
- ✅ Do use: prop?: string;

Metadata

Metadata

Assignees

No one assigned

    Labels

    design:neededA design request has been raised that needs a proposalneeds-area

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions