简体   繁体   中英

How can I describe this complex json model in swagger

I'm trying to use swagger to describe a rest api I added to some code.

This is one of my simple returns.

Saw a couple good examples but didn't have any luck trying to make sense how to apply it to my problem.

The different API calls will contain different content.

If I know what this one should look like I aught to be able to figure the others out.

Can anyone tell me what this description should look like?

{    
    "result" : {
      "content" : {
         "UNTAINTED_HOST" : "www.google.com",
         "DATA" : [
            "216.58.217.36"
         ],
         "IP_or_NAME" : "NAME",
         "RC" : "true",
         "FAULT_MSG" : "No Faults"
      },
      "detail" : "Sucessfully terminated your request",
      "short" : "Done"    } 
}

Or this even simpler one:

{
   "result" : {
      "short" : "Done",
      "detail" : "Sucessfully terminated your request",
      "content" : "Running"
   }
}

Think it would look like -

{
  "definitions": {
    "result": {
      "type": "object",
      "required": [
        "short", "detail", "content"
      ],
      "properties": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
                    "content": {
                      "type": "string"
                    },
                    "detail": {
                      "type": "string"
                    },
                    "short": {
                      "type": "string"
                    },
          }
        }
      }
    }
  } 
}

Tried this in the swagger editor... seems I missed something.

definitions: 
  results: 
    type: object
    required: [ result ]
    properties: 
      result: 
        type: array
        items: 
          $ref: #/definitions/result

  result: 
    type: object
    properties: 
      short: 
        type: string
      detail: 
        type: string
      content: 
        type: string

This seemed to work:

  results: 
    type: object
    required: [ result ]
    properties: 
      result: 
        type: array
        items: 
          type: object
          required: [ short, detail, content ]
          properties: 
            short: 
              type: string
            detail: 
              type: string
            content: 
              type: string

Thanks

This worked for me:

definitions:
  result: 
    type: object
    properties: 
      short: 
        type: string
      detail: 
        type: string
      content: 
        type: string

  results: 
    type: object
    required: [result]
    properties: 
      result: 
        type: array
        items: 
          $ref: '#/definitions/result'

Notice the single quotes around the value of $ref . I didn't see those in your original post. However, you didn't mention the error you are seeing. If this answer doesn't help, please update your question with more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM