swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Support for application/x-ndjson

Open philrz opened this issue 6 years ago • 9 comments

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.

philrz avatar May 03 '19 23:05 philrz

Yeah, seems like our JSON test is a little too broad.

@philrz, would rendering the response as plaintext work for you?

shockey avatar May 03 '19 23:05 shockey

@shockey: Yes, plaintext would work for me. Thanks!

philrz avatar May 04 '19 01:05 philrz

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?

reseto avatar Dec 05 '19 14:12 reseto

Giving this a friendly bump, @shockey 👍 . We have an API platform that produces x-ndjson responses and see the same "can't parse JSON. Raw Result:"

mxkalinowski avatar Jul 31 '20 20:07 mxkalinowski

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 :(

hhromic avatar May 26 '21 18:05 hhromic

Hello guys, any progress here, please?

honza-zidek avatar Mar 15 '24 21:03 honza-zidek

Seems this is still the same in current master. JSON (and XML) still seem to be too broadly matched:

https://github.com/swagger-api/swagger-ui/blob/cebccaa8745662d61cdefdbad3646034cd15d4f8/src/core/components/response-body.jsx#L97-L118

hhromic avatar Mar 15 '24 22:03 hhromic

In my opinion it would be great to get the "hack" above merged in. (https://github.com/swagger-api/swagger-ui/compare/master...theosanderson:swagger-ui:patch-1)

Happy to create a PR, depending on how much testing I would need to do.

theosanderson avatar Jul 01 '24 16:07 theosanderson

Do we have any progress here? Thanks.

onacit avatar Aug 11 '24 06:08 onacit