Skip to content

Nullable props (OpenAPI v2)

Ferdi Koomen edited this page Dec 21, 2023 · 1 revision

In the OpenAPI v3 spec you can create properties that can be NULL, by providing a nullable: true in your schema. However, the v2 spec does not allow you to do this. You can use the unofficial x-nullable in your specification to generate nullable properties in OpenApi v2.

{
    "ModelWithNullableString": {
        "required": [
            "requiredProp"
        ],
        "description": "This is a model with one string property",
        "type": "object",
        "properties": {
            "prop": {
                "description": "This is a simple string property",
                "type": "string",
                "x-nullable": true
            },
            "requiredProp": {
                "description": "This is a simple string property",
                "type": "string",
                "x-nullable": true
            }
        }
    }
}

Generated code:

export type ModelWithNullableString = {
    prop?: string | null;
    requiredProp: string | null;
};
Clone this wiki locally