swagger-api / swagger-editor

Swagger Editor
https://editor.swagger.io
Apache License 2.0
8.94k stars 2.26k forks source link

Malformed JSON #958

Closed stcrestrada closed 8 years ago

stcrestrada commented 8 years ago

I am currently using the latest swagger-editor version (forked a few days ago).

I am getting an error when I try to post my response in the swagger editor, "Malformed JSON", I have a hunch as to why this is happening. When I submit data on swagger-ui the json appears like this in the body:

[
  {
    "firstName": "string",
    "lastName": "string",
    "dob": "string",
    "Address": {
      "street1": "string",
      "city": "string",
      "state": "string",
      "country": "string"
    },
    "phone": 0,
    "email": "string"
  }
] 

and I've had success on swagger-ui, but in swagger-editor preview my json appears w/o the brackets like so:

  {
    "firstName": "string",
    "lastName": "string",
    "dob": "string",
    "Address": {
      "street1": "string",
      "city": "string",
      "state": "string",
      "country": "string"
    },
    "phone": 0,
    "email": "string"
  }

My Swagger

  swagger: '2.0'
  info:
    title: API
    version: 1.0.0
  host: custom.com
  schemes:
    - https
  produces:
    - application/json
  paths:
    /e3ServicesRest2_3/ingest:
      post:
        summary: Due Diligence (Advanced)
        parameters:
          - name: apiKey
            in: header
            description: Client API key.
            required: true
            type: string
            format: string
          - name: services
            in: header
            description: Person  Services
            required: true
            type: string
            format: string
          - name: body
            in: body
            description: Person data.
            required: true
            schema:
              $ref: '#/definitions/Person'
        tags:
          - Person
        responses:
          '200':
            description: An array of products
            schema:
              type: array
              items:
                $ref: '#/definitions/Person'
          default:
            description: Unexpected error
            schema:
              $ref: '#/definitions/Error'
  definitions:
    Person:
      type: object
      properties:

        firstName:
          type: string
          description: Person of interest first name.
        lastName:
          type: string
          description: Person of interest last name.
        dob:
          type: string
          description: Person of interest date of birth.
        Address:
          type: object
          properties:
            street1:
              type: string
              description: Person of interest street
            city:
              type: string
              description: Person of interest city
            state:
              type: string
              description: Person of interest state
            zipcode:
              type: string
              description: Person of interest zipcode
            country:
              type: string
              description: Person of interest country
        email:
          type: string
          description: Person of interest email.
        phone:
          type: string
          description: Person of interest email.
    Error:
      type: object
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
        fields:
          type: string

I want to format my json like the former json I pasted above; however, I am uncertain how I can achieve this, since my yaml file did not change from swagger-ui to swagger-editor

saharj commented 8 years ago

Your body parameter schema is an object, and Swagger Editor is showing it correctly. It should not have brackets around, when the schema in not an array.

If you want to have an array of Person objects you can use type:array and set items: to the Person.

webron commented 8 years ago

There seems to be a confusion here. The body of the request is described as an object, and the response is described as an array. You're describing that you're taking the response you get from the operation and try to use it for the body - obviously, that's not going to work.

webron commented 8 years ago

@stcrestrada - does this explain the problem?

dolmen commented 8 years ago

I don't see any issue in swagger-editor 2.10.1:

webron commented 8 years ago

Closing this due to lack of activity. @stcrestrada please reopen if still relevant.