Programming

How to define a property that can be string or null in OpenAPI Swagger

25 September 2026 · 7 min read

How to define a property that can be string or null in OpenAPI Swagger

Building robust and well-documented APIs is paramount in today’s interconnected digital landscape. A critical aspect of this involves precise data type definition, especially when dealing with properties that might not always have a value. One common scenario developers encounter is needing to define a property that can be string or null in OpenAPI (Swagger). This isn’t just about technical correctness; it’s about clarity for API consumers, preventing errors, and ensuring smooth data exchange. Misinterpreting how to represent optional string data can lead to frustrating debugging sessions and brittle integrations. This guide delves into the nuances of handling such properties across different OpenAPI specifications, providing clear examples and best practices to ensure your API documentation is both accurate and unambiguous.

Understanding Nullability in API Design

When designing APIs, the concept of nullability often causes confusion. It’s crucial to distinguish between a property being “missing” (not present in the payload) and being “explicitly null” (present with a null value). While an optional property can simply be omitted from a JSON payload if no value exists, explicitly defining a property as null conveys different semantic meaning. For instance, a user’s middleName might be explicitly set to null to indicate they have no middle name, rather than simply omitting it which might imply the information is unknown or not provided.

OpenAPI, formerly known as Swagger, provides powerful mechanisms for defining your API’s schema, including its data types and constraints. Properly signaling whether a string property can accept a null value is vital for client-side validation, code generation, and overall API usability. Without clear documentation, client applications might either expect a string where a null is provided, leading to runtime errors, or fail to correctly handle a legitimate null value. This precision in API documentation is a hallmark of high-quality API design, significantly enhancing the developer experience.

According to research by SmartBear, consistent and accurate API documentation is a top priority for developers, with nullability definitions being a key component of that accuracy. Ignoring these details can lead to unexpected behavior in client applications and increased integration effort. Therefore, understanding and correctly implementing nullability for string properties is not merely a technicality but a fundamental aspect of creating reliable and user-friendly APIs.

Implementing Nullable Strings in OpenAPI 3.0+

OpenAPI Specification 3.0 and newer versions introduced a dedicated nullable keyword, making it straightforward to define properties that can be string or null. This keyword applies directly to a schema object. When nullable: true is specified alongside type: string, it explicitly communicates that the property can accept either a valid string value or the JSON null literal. This is a significant improvement over previous versions, which relied on vendor extensions or less explicit methods.

For example, if you have a user profile endpoint where a user’s optional bio field might be empty or explicitly unset, you would define it as follows:

components: schemas: UserProfile: type: object properties: id: type: string format: uuid description: Unique user identifier username: type: string description: User's chosen username bio: type: string nullable: true description: Optional user biography, can be null 

In this schema, the bio property is clearly defined as a string that can also accept null. This explicit declaration helps client SDKs and API consumers understand that they should be prepared to handle both string values and null. For OpenAPI 3.1, the specification aligned with JSON Schema’s approach for multiple types, allowing you to define a property using an array for the type keyword:

components: schemas: ProductDetails: type: object properties: productId: type: string description: Unique product ID productName: type: string description: Name of the product promoCode: type: [string, "null"] description: An optional promotional code, can be a string or null (OpenAPI 3.1+) 

This type: [string, "null"] syntax is equivalent to type: string, nullable: true in OpenAPI 3.0.x and fully aligns with JSON Schema draft 2019-09 and newer. When designing your API with OpenAPI 3.0+, leveraging the nullable: true keyword is the standard and most recommended approach for declaring properties that can explicitly be null, ensuring clear and consistent API documentation. This method ensures that all consumers understand the expected data structure, reducing potential integration issues and improving overall API usability.

Infographic here
Handling Nullable Strings in Swagger 2.0 (OpenAPI 2.0) ------------------------------------------------------

Before OpenAPI 3.0 introduced the standardized nullable keyword, developers using Swagger 2.0 (which is synonymous with OpenAPI 2.0) had to rely on a vendor extension to define properties that could be string or null. The most common approach involved using the x-nullable: true extension. Vendor extensions in Swagger 2.0 are custom keywords prefixed with x- that allow you to add extra information not explicitly defined in the specification itself. While functional, it’s important to remember that these extensions might not be universally supported by all tools that process Swagger definitions.

Here’s how you would typically define a nullable string property in a Swagger 2.0 schema:

definitions: ContactInfo: type: object properties: email: type: string format: email description: Contact email address phoneNumber: type: string x-nullable: true description: Optional phone number, can be null (Swagger 2.0) 

In this example, the phoneNumber property is defined as a string, and the x-nullable: true extension indicates that it can also accept a null value. It’s important to note that without this extension, a Swagger 2.0 parser would typically assume that a property defined as type: string must always contain a string value and would not implicitly allow null. While x-nullable served its purpose, it lacked the official backing and consistent tool support that the native nullable keyword in OpenAPI 3.0+ now provides.

For projects still maintaining Swagger 2.0 APIs, using x-nullable: true is the established method. However, if you are starting a new project or considering an API upgrade, migrating to OpenAPI 3.0+ is highly recommended. This move allows you to leverage the standardized nullable keyword, leading to more consistent documentation, better tool compatibility, and clearer API contracts. The official OpenAPI Specification provides a comprehensive guide on various data types and their definitions, which is an excellent resource for deeper understanding.

Best Practices for Defining Nullable Properties

When you define a property that can be string or null in OpenAPI (Swagger), it’s not just about syntax; it’s about making deliberate design choices that enhance your API’s usability and maintainability. One crucial best practice is to always be explicit. If a property can truly be null, declare it as such. Don’t rely on Question & Answer :

I have JSON schema file where one of the properties is defined as either string or null:

"type":["string", "null"] 

When converted to YAML (for use with OpenAPI/Swagger), it becomes:

type: - 'null' - string 

but the Swagger Editor shows an error:

Schema “type” key must be a string

What is the correct way to define a nullable property in OpenAPI?

This depends on the OpenAPI version.

OpenAPI 3.1

Your example is valid in OpenAPI 3.1 (published 2021-02-15), which is fully compatible with JSON Schema 2020-12.

type: - 'null' # Note the quotes around 'null' - string # same as type: ['null', string] 

The above is equivalent to:

oneOf: - type: 'null' # Note the quotes around 'null' - type: string 

The nullable keyword used in OAS 3.0.x (see below) does not exist in OAS 3.1, it was removed in favor of the 'null' type.

OpenAPI 3.0.x

Nullable strings are defined as follows:

type: string nullable: true 

This is different from JSON Schema syntax because OpenAPI versions up to 3.0.x use their own flavor of JSON Schema (“extended subset”). One of the differences is that the type must be a single type and cannot be a list of types. Also there’s no 'null' type; instead, the nullable keyword serves as a type modifier to allow null values.

OpenAPI 2.0

Version 2 does not support 'null' as the data type, so you are out of luck. You can only use type: string. But some tools support the vendor extension x-nullable: true to indicate nullable properties.

Consider migrating to OpenAPI v. 3 to get proper support for nulls.