Forum Discussion

AndyTGardner's avatar
AndyTGardner
New Contributor
7 years ago
Solved

Defining Nested Data in Response Schema

I'm trying to define a schema for a response in the Yaml for my API and this includes a field being an array of sub data. My Yaml is as follows:

    PPOdata:
      type: object
      properties:
        PPO_Id:
          type: integer
          example: 256
        PPO_Name:
          type: string
          example: PPO_1
        PPO_Modules:
          type: array
          items:
            additionalProperties:
              type: object
              properties:
                ModuleName:
                  type: string
                  example: latency
                ModuleSettings:
                  type: string
                  example: Min 0, Max 10

So, as you can see above the the sub data field is called PPO_Modules and is an array of objects containing two fields. SwaggerHub shows this as valid and I'm able to save with no errors. With the examples, the example in the documentation shows as

{
  "PPO_Id": 256,
  "PPO_Name": "PPO_1",
  "PPO_Modules": [
    null
  ]
}

As the sub data is showing as Null, does this mean I have defined this incorrectly, even though SwaggerHub says it's valid, or have I defined this in a totally incorrect manner and misunderstood the use of additional properties to define the sub fields?

  • Depends.

     

    If you will always have three entries for each vendor, then loop2 can just have an index from 1 to 3 and quit.  

     

    If the number of entries will vary, then you can keep track of CurrentVendor and LastVendor.  When you go through and the CurrentVendor is different, then you need to process a purchase order before you continue.

     

    Seems like it would be easier to have just one loop though.  Read a row, if it's a new vendor, process the purchase order and clear the totals before you continue.  Then process the row you have and loop again.

     

    If you need more detail let me know.


  • DCat1223 wrote:

    Hi william_roe...

     

    Thank you for the input.  I'm not a technical person and this is all pretty new to me so please forgive my ignorance.  I'm still muddling my way through all of this.

     

    Humor me if you will, as I reiterate what I think you are suggesting - I'm probably going to get this worng but here it goes...

     

    1) I replace any duplicate instance of Vendor# with NULL, or "" in my datasheet.

    2) When Vendor# changes, I execute the portion of the test that creates the PO, and start the loop over with the next vendor. 

    Am I close?

     

    If so, how do I tell the test that if the Vendor# changes to run the lines of the script that create the PO and restart the loop using the next vendor? 

     

    Thank you for your time.

     

    DC.

     

     


    1) Don't input "NULL' OR "" in the vend_num cell - simply delete the redundant value in the cells and in then perform an "if" operation where the value != null. (I can include a screen capture later if it would help)

    2.) You don't need to start the loop over. Finish whatever needs to be done on page 2, the po detail (after the next non null vend_num value is found and return back to page 1 (the po header) and continue on to the next record in the loop. 

     

1 Reply

  • You should add 'type: object' to the internal items array:

     

            items:
              type: object
              additionalProperties:
                type: object
                properties:
                  ModuleName:
                    type: string
                    example: latency
                  ModuleSettings:
                    type: string
                    example: Min 0, Max 10
    

    While the definition is valid, itt doesn't actually produce the result you'd expect. When you don't specify a type, it means that any type can be used (string, array, object, number...). The 'additionalProperties' will only take effect if the actual used value is an object, but in other cases it will be ignored. Having that definition does not implicitly set the type to be object.