Support for application/x-ndjson
See original GitHub issueContent & configuration
Swagger/OpenAPI definition:
---
schemes:
- http
swagger: '2.0'
host: localhost:9866
paths:
"/foo":
get:
description: foo
produces:
- application/x-ndjson
summary: foo
operationId: foo
responses:
'200':
description: foo
schema:
type: string
I’ve made no changes to the default Swagger-UI configuration. I’m just using the dist of v3.22.1 out-of-the-box.
Is your feature request related to a problem?
I’ve been trying to document an application that returns content-type: application/x-ndjson
. The format is spec’ed here. An example response:
{"hello": "world"}
{"goodbye": "world"}
When I render my spec in Swagger UI out-of-the-box and click Try It Out > Execute, Swagger UI seems to try and fail to treat the response data as JSON, giving an error message:
I can see by peeking in swagger-ui-bundle.js
where it seem to be treating any content-type
that contains json
as JSON:
else if (/json/i.test(n)) {
try {
m = (0, r.default)(JSON.parse(t), null, " ")
} catch (e) {
m = "can't parse JSON. Raw result:\n\n" + t
}
I’m no JavaScript pro, but I found that if I hack this to exclude my data type, now it silently renders it as text.
else if (/json/i.test(n) && !/x-ndjson/i.test(n)) {
try {
m = (0, r.default)(JSON.parse(t), null, " ")
} catch (e) {
m = "can't parse JSON. Raw result:\n\n" + t
}
Describe the solution you’d like
It would be lovely if Swagger UI actually parsed x-ndjson
according to its spec. But at minimum, it would be an improvement if it just didn’t report an error message due to trying to parse it as JSON, similar to what I’ve done with my hack.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:5 (1 by maintainers)
Top GitHub Comments
Another bump from mid-2021 😃 As of today, JSON still seems to be too broadly matched: https://github.com/swagger-api/swagger-ui/blob/92f1507408f442b9d450c986eba1d007352ec045/src/core/components/response-body.jsx#L96-L107
As per RFC 4627, the only content-type for JSON should be
application/json
. Perhaps Swagger-UI should be more strict in this regard.Our API platform also is adopting NDJSON more and more and we are also affected by this issue 😦
Hi, I’m also using the steaming json format content-type: application/x-ndjson In general it doesn’t seem a great idea to modify response content. But at least in this special case would it be possible to not include the “can’t parse JSON. Raw result:\n\n” in response?