Problem
The current server-side OpenAPI implementation does not allow defining an endpoint that returns binary data. Any operation result is either JSON-serialized or coerced to a String before being written to the HTTP response — there is no path for streaming raw bytes (e.g. application/octet-stream, image/*, application/pdf) to the caller.
Root Cause
In com.top_logic.service.openapi.server.impl.ServiceMethodByExpression#handleRequest, the response body is always written via the character Writer:
{{{#!java resp.getWriter().write(content); // content is always a String }}}
The value of content is built in exactly two ways:
- Default (no Response wrapper): content = JSON.toString(result) — any return value is JSON-serialized.
- With Response wrapper (HTTPResponse TL-Script function): if the MIME type is application/json, then content = JSON.toString(response.getResult()); otherwise content = String.valueOf(response.getResult()).
HttpServletResponse#getOutputStream() is never used for user endpoint responses. There is no special-casing for BinaryData, byte[], or InputStream return values — they fall into the else branch and are either JSON-serialized or coerced via String.valueOf(...), which destroys the binary content.
The Javadoc of com.top_logic.service.openapi.server.impl.Response already documents this limitation:
> "written to HttpServletResponse#getWriter()" — i.e., character output only.
Schema vs. Runtime Mismatch
com.top_logic.service.openapi.server.parameter.ParameterFormat defines a BINARY entry, which is reused by OperationResponse#getFormat(). BINARY is emitted into the generated OpenAPI spec document as a schema annotation, but there is no corresponding runtime handling in the response writer. The published contract therefore lies about what the endpoint can actually deliver.
Asymmetry with the Client Side
The client side (consuming a remote API) does support binary responses: com.top_logic.service.openapi.client.registry.impl.response.ResponseHandlerByExpression calls EntityUtils.toByteArray(entity) for BINARY format and returns a byte[] to TL-Script. Only the server side (producing a response) is broken.
Proposed Fix
In ServiceMethodByExpression#handleRequest, branch on the result type:
- If the result is a BinaryData, byte[], or InputStream (or if the declared/response ParameterFormat is BINARY), write raw bytes via resp.getOutputStream() instead of resp.getWriter().
- Preserve the current JSON/string path for all other cases.
Additionally, Response (the HTTPResponse TL-Script wrapper) should accept binary payloads so that a script can explicitly return application/octet-stream, application/pdf, image/*, etc., with the correct Content-Type and Content-Length set.
Affected Files
- com.top_logic.service.openapi.server/src/main/java/com/top_logic/service/openapi/server/impl/ServiceMethodByExpression.java (lines 79–112)
- com.top_logic.service.openapi.server/src/main/java/com/top_logic/service/openapi/server/impl/Response.java (Javadoc + possibly new binary payload field)
- com.top_logic.service.openapi.server/src/main/java/com/top_logic/service/openapi/server/parameter/ParameterFormat.java (runtime hook for BINARY on the response side)
Migration
The response handling in ServiceMethodByExpression has changed. Previously, all endpoint results were unconditionally serialized as JSON (application/json). Now the content type is determined by the result type:
| Result type | Before | After |
| Map, Collection | application/json | application/json (unchanged) |
| BinaryData, byte[], InputStream | application/json (broken) | Binary stream with correct content type |
| String, other primitives | application/json (quoted JSON string) | text/plain (raw string, no JSON quoting) |
Who is affected?
Endpoints whose TL-Script expression returns a String (or other non-Map, non-Collection, non-binary value) and whose clients are configured to parse the response as JSON.
Endpoints returning Map or Collection values are not affected — they continue to be serialized as application/json.
Endpoints using response(statusCode, content) without the contentType argument are affected as well: its former default application/json is gone, the content type is now derived from content as in the table above. Pass "application/json" explicitly to keep the old behaviour.
How to migrate
Option 1: Change the client response format to TEXT
In the OpenAPI client configuration (Service Editor > TL-Script-Funktionen für externe APIs), navigate to:
Funktionsdefinitionen > name > Antwort-Bearbeitung > Format
Change from JSON to Text. The client will then receive the raw string directly without JSON parsing.
Option 2: Return structured data instead of a String
Change the server-side TL-Script expression to return a Map or List instead of a concatenated string. For example, replace:
.sort()
.reduce("", subresult -> element -> $subresult == "" ? $element : ($subresult + ", " + $element))
with:
.sort()
The sorted list (a Collection) is automatically serialized as a JSON array (["A1", "A3"]). Update the client assertion accordingly.
Option 3: Explicitly set application/json on the server response
Use the response() TL-Script function to force JSON content type:
response(200, $result, "application/json")
This makes the endpoint behave exactly as before.
Detection
grep -rln -e 'OpenApiServer' -e 'response(' --include=*.xml .