Swagger-ui: Print json null values correctly in the api documentation panel

Created on 21 May 2018  路  4Comments  路  Source: swagger-api/swagger-ui

_From @specialtactics on May 16, 2018 10:03_

Q&A (please complete the following information)

  • Method of installation: docker
  • Swagger-Editor version: 3.5.5
  • Swagger/OpenAPI version: OpenAPI 3.0

    Content & configuration

Swagger/OpenAPI definition:
yaml components: examples: exampleModel: value: { "name": "Adam Jacobson", "email": null, }

Is your feature request related to a problem?

YAML is a superset of JSON and hence supports "null" as a value of a json property. It is actually a fairly common use-case for a field to be null (for example, patch a property as null to remove it, default values being null).

However the editor will display a null value in a json example as empty object { }, which is not accurate and is very annoying.

Describe the solution you'd like

Editor to print json null as null in the example dialogues - for instance, as it would appear in postman.

Describe alternatives you've considered

I have not, I think it should be consistent with the json standard and other tools.

Additional context

None

Thank you

_Copied from original issue: swagger-api/swagger-editor#1758_

Good first issue rendering lock-bot bug

Most helpful comment

@shockey Actually the issue is not specific to media type examples - example: null also does not work properly in schema examples.

openapi: 3.0.0
info:
  title: test
  version: 0.0.1
paths:
  /foo:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  foo:
                    type: string
                    nullable: true
                    example: null
                # object-level example
                #example:
                #  foo: null

Expected result:

{
  "foo": null
}

Actual result:

{
  "foo": {}
}

All 4 comments

Hey @specialtactics, this is actually because we aren't currently respecting example values at all, so the type: object schema definition for the property is creating that empty object you see, I believe.

Here's what I tested with, based on your snippet:

openapi: "3.0.0"

paths:
  /:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                email:
                  type: object
            example:
              $ref: "#/components/examples/exampleModel"

components:
 examples:
   exampleModel:
     value:
      name: "Adam Jacobson"
      email: null

As luck would have it, example/examples support is up for work very soon, so this will be fixed in short order 馃槃

Good to hear @shockey, thanks !

@shockey Actually the issue is not specific to media type examples - example: null also does not work properly in schema examples.

openapi: 3.0.0
info:
  title: test
  version: 0.0.1
paths:
  /foo:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  foo:
                    type: string
                    nullable: true
                    example: null
                # object-level example
                #example:
                #  foo: null

Expected result:

{
  "foo": null
}

Actual result:

{
  "foo": {}
}

Locking due to inactivity.
This is done to avoid resurrecting old issues and bumping long threads with new, possibly unrelated content.
If you think you're experiencing something similar to what you've found here: please open a new issue, follow the template, and reference this issue in your report.
Thanks!

Was this page helpful?
0 / 5 - 0 ratings