question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for application/x-ndjson

See original GitHub issue

Content & 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:

image

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
                        }

image

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:open
  • Created 4 years ago
  • Reactions:11
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
hhromiccommented, May 26, 2021

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 😦

2reactions
resetocommented, Dec 5, 2019

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is difference between application/x-ndjson and ...
This tutorial will help you to understand the difference between application/x-ndjson and application/json. This guide is written by well ...
Read more >
in_http: Support for Content-Type application/x-ndjson · Issue ...
Implement support for Content-Type application/x-ndjson for newline-delimited JSON objects to be summited via the in_http input plugin. https:// ...
Read more >
Angular HttpClient GET parsing error for application/x-ndjson
The api produces application/x-ndjson and I am not sure if HttpClient is unable to parse the response. My Service Class:
Read more >
Newline Delimited JSON (ndjson) Format
This format supports the following MIME types. MIME Type. application/x-ndjson. application/x-ldjson ...
Read more >
ndjson
This page describes the ndjson format, also called Newline delimited JSON. ... This means '\r\n' is also supported because trailing white space is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found