You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: developer_manual/client_apis/OCS/ocs-openapi.rst
+63-41Lines changed: 63 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,61 +95,83 @@ For details take a look at :ref:`OCS <ocscontroller>`.
95
95
}
96
96
}
97
97
98
-
PREFER to use ``null`` to represent empty data
99
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
+
CAREFULLY handle empty values in JSON Responses
99
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100
100
101
-
When an endpoint conceptually returns an object, prefer ``null`` to represent "no data".
101
+
When defining API responses, it’s important to make explicit whether an empty value should be ``null``, an empty object (``{}``), or an empty array (``[]``) in the resulting JSON. The PHP type you return determines this, and using the wrong one can easily lead to confusing or inconsistent results for your API consumers.
102
102
103
-
There is a problem with PHP and arrays that get converted to JSON.
104
-
JSON has lists and objects while PHP only has arrays.
105
-
If you were to return an empty array in PHP it will always turn into ``[]`` in JSON.
106
-
This is not a problem for endpoints that always return lists, but most endpoints return a single JSON object.
107
-
For those endpoints returning ``[]`` in PHP is a problem because the consumer will either get ``[]`` or ``{...}`` which is hard to handle.
103
+
.. note::
108
104
109
-
If you are not able to use ``null`` for whatever reason, use ``new \stdClass()`` instead.
110
-
It will get correctly converted into ``{}`` in the JSON response on Nextcloud 28 and later.
105
+
In PHP, ``null``, ``[]``, and ``new \stdClass()`` are distinct types and will be serialized to different values in JSON. This is especially important for OpenAPI consumers, which often expect a consistent type.
111
106
112
-
If you are working with an existing API where you can not break compatibility, you can also type the result as ``list<empty>``.
107
+
Here is how PHP values are serialized to JSON:
108
+
109
+
+-------------------+--------------------+
110
+
| PHP Value | JSON Output |
111
+
+===================+====================+
112
+
| null | null |
113
+
+-------------------+--------------------+
114
+
| new \stdClass() | {} |
115
+
+-------------------+--------------------+
116
+
| [] | [] |
117
+
+-------------------+--------------------+
118
+
119
+
- Use ``null`` to indicate that a value is explicitly absent. This should be preferred for most “empty” responses.
120
+
- Use ``new \stdClass()`` **if and only if** the client expects an empty object (`{}`) rather than `null`. This is sometimes required by schema contracts that always expect an object shape, even if empty.
121
+
122
+
.. important::
123
+
124
+
Returning ``new \stdClass()`` as an API response requires at least Nextcloud 28 to reliably serialize to ``{}``.
125
+
126
+
- **Avoid returning ``[]``** for endpoints expected to yield a JSON object, as this will serialize to a JSON array (`[]`), causing downstream consumers to deal with unpredictable types.
127
+
128
+
If you are modifying or extending existing APIs and are unable to use ``null`` or ``\stdClass()`` without breaking backward compatibility, you may type the result as ``list<empty>`` to signal an empty array is expected.
113
129
114
130
.. collapse:: Examples
115
131
116
132
.. code-block:: php
117
-
:caption: Bad
133
+
:caption: Incorrect (returns empty array instead of empty object or null)
0 commit comments