swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.56k stars 8.96k forks source link

Support for application/x-ndjson #5342

Open philrz opened 5 years ago

philrz commented 5 years ago

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.

shockey commented 5 years ago

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

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

philrz commented 5 years ago

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

reseto commented 4 years ago

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?

mxkalinowski commented 4 years ago

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

hhromic commented 3 years ago

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

honza-zidek commented 8 months ago

Hello guys, any progress here, please?

hhromic commented 8 months ago

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

theosanderson commented 4 months ago

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.

onacit commented 3 months ago

Do we have any progress here? Thanks.