Forum Discussion

rsiena's avatar
rsiena
New Contributor
7 years ago
Solved

What is the correct way to define a result type of array of objects

1. is this definition correct to return a variable size array of list items?

responses:
200:
description: List Returned
schema:
type: object
format: array
properties:
ListID:
type: integer
format: int64
description: ID of the list
ListName:
type: string
description: List name
ListItem:
type: string
description: List Item Text
Abbreviation:
type: string
description: Abbreviation text if applicable

 

2.when I generate the client code in c# do I need to do something to process the array.

right now I am getting a json deserialization returns null.  

It seems to me that if I define the results as an array of items it should give me a list of InlineResponsexxx's

 

thanks for you help

  • Thanks for the example. In this case, you'd describe it as follows:

     

    responses:
      200:
        description: List Returned
        schema:
          type: array
          items:
            type: object
            properties:
              ListID:
                type: integer
                format: int64
                description: ID of the list
              ListName:
                type: string
                description: List name
              ListItem:
                type: string
                description: List Item Text
              Abbreviation:
                type: string
                description: Abbreviation text if applicable

    Notice that you describe the type as 'array', and then under 'item' you describe the type that you want the array to have - in this case it's an object. Just like in other places in the definition, you can also use a $ref to reference a 'Language' definition, and reuse that elsewhere if needed.

5 Replies

  • Hello!

     

    Unfortunately, the way you're trying to represent the array is not quite valid, and that would also explain why the c# code probably behaves unexpectedly. It's a bit difficult to know what you're trying to describe exactly though - can you provide with a sample value? That'd give us a better understanding and we can provide you with the proper way to represent it.

  • rsiena's avatar
    rsiena
    New Contributor

    thanks for the reply  in this case I am requesting a Language list that is supported in the system. I pass the list name (Language) and some parms and get this list back

    So I am confused if I have created the swagger definition correctly.  

     

    Thanks

     

     

     

     

     

    [
      {
        "ListID": 258806,
        "ListName": "Language",
        "ListItem": "Albanian",
        "Abbreviation": null
      },
      {
        "ListID": 101,
        "ListName": "Language",
        "ListItem": "Arabic",
        "Abbreviation": "ar"
      },
      {
        "ListID": 201,
        "ListName": "Language",
        "ListItem": "Bahasa Indonesia",
        "Abbreviation": null
      },
      {
        "ListID": 258706,
        "ListName": "Language",
        "ListItem": "Bengali",
        "Abbreviation": null
      },
      {
        "ListID": 301,
        "ListName": "Language",
        "ListItem": "Cantonese",
        "Abbreviation": null
      },
      {
        "ListID": 401,
        "ListName": "Language",
        "ListItem": "Croatian",
        "Abbreviation": "hr"
      },
      {
        "ListID": 501,
        "ListName": "Language",
        "ListItem": "Danish",
        "Abbreviation": "da"
      },
      {
        "ListID": 601,
        "ListName": "Language",
        "ListItem": "Dutch",
        "Abbreviation": "nl"
      },
      {
        "ListID": 701,
        "ListName": "Language",
        "ListItem": "English",
        "Abbreviation": "en"
      },
      {
        "ListID": 801,
        "ListName": "Language",
        "ListItem": "Filipino",
        "Abbreviation": null
      },
      {
        "ListID": 901,
        "ListName": "Language",
        "ListItem": "Finnish",
        "Abbreviation": "fi"
      },
      {
        "ListID": 1001,
        "ListName": "Language",
        "ListItem": "French",
        "Abbreviation": "fr"
      },
      {
        "ListID": 1101,
        "ListName": "Language",
        "ListItem": "German",
        "Abbreviation": "de"
      },
      {
        "ListID": 1201,
        "ListName": "Language",
        "ListItem": "Greek",
        "Abbreviation": "el"
      },
      {
        "ListID": 1301,
        "ListName": "Language",
        "ListItem": "Hebrew",
        "Abbreviation": "he"
      },
      {
        "ListID": 1401,
        "ListName": "Language",
        "ListItem": "Hindi",
        "Abbreviation": "hi"
      },
      {
        "ListID": 1501,
        "ListName": "Language",
        "ListItem": "Hungarian",
        "Abbreviation": "hu"
      },
      {
        "ListID": 1601,
        "ListName": "Language",
        "ListItem": "Irish",
        "Abbreviation": "ga"
      },
      {
        "ListID": 1701,
        "ListName": "Language",
        "ListItem": "Italian",
        "Abbreviation": "it"
      },
      {
        "ListID": 1801,
        "ListName": "Language",
        "ListItem": "Japanese",
        "Abbreviation": "ja"
      },
      {
        "ListID": 1901,
        "ListName": "Language",
        "ListItem": "Korean",
        "Abbreviation": "ko"
      },
      {
        "ListID": 2001,
        "ListName": "Language",
        "ListItem": "Mandarin",
        "Abbreviation": null
      },
      {
        "ListID": 2101,
        "ListName": "Language",
        "ListItem": "Norwegian",
        "Abbreviation": "no"
      },
      {
        "ListID": 2201,
        "ListName": "Language",
        "ListItem": "Persian",
        "Abbreviation": "fa"
      },
      {
        "ListID": 2301,
        "ListName": "Language",
        "ListItem": "Polish",
        "Abbreviation": "pl"
      },
      {
        "ListID": 2401,
        "ListName": "Language",
        "ListItem": "Portuguese",
        "Abbreviation": "pt"
      },
      {
        "ListID": 2501,
        "ListName": "Language",
        "ListItem": "Romanian",
        "Abbreviation": "ro"
      },
      {
        "ListID": 2601,
        "ListName": "Language",
        "ListItem": "Russian",
        "Abbreviation": "ru"
      },
      {
        "ListID": 2701,
        "ListName": "Language",
        "ListItem": "Spanish",
        "Abbreviation": "es"
      },
      {
        "ListID": 258906,
        "ListName": "Language",
        "ListItem": "Swahili",
        "Abbreviation": null
      },
      {
        "ListID": 2801,
        "ListName": "Language",
        "ListItem": "Swedish",
        "Abbreviation": "sv"
      },
      {
        "ListID": 2901,
        "ListName": "Language",
        "ListItem": "Turkish",
        "Abbreviation": "tr"
      },
      {
        "ListID": 3001,
        "ListName": "Language",
        "ListItem": "Ukranian",
        "Abbreviation": null
      },
      {
        "ListID": 3101,
        "ListName": "Language",
        "ListItem": "Urdu",
        "Abbreviation": "ur"
      }
    ]
    • RonRatovsky's avatar
      RonRatovsky
      Moderator

      Thanks for the example. In this case, you'd describe it as follows:

       

      responses:
        200:
          description: List Returned
          schema:
            type: array
            items:
              type: object
              properties:
                ListID:
                  type: integer
                  format: int64
                  description: ID of the list
                ListName:
                  type: string
                  description: List name
                ListItem:
                  type: string
                  description: List Item Text
                Abbreviation:
                  type: string
                  description: Abbreviation text if applicable

      Notice that you describe the type as 'array', and then under 'item' you describe the type that you want the array to have - in this case it's an object. Just like in other places in the definition, you can also use a $ref to reference a 'Language' definition, and reuse that elsewhere if needed.

  • rsiena's avatar
    rsiena
    New Contributor

    Thanks,

     

    Yea being new to the system I wanted to create an api without adding the issues of a reference.  After I get it working I will change to a reference as you suggested.

     

    I will give it a try and let you know how I make out.

     

    thanks again.

     

    Rich

  • rsiena's avatar
    rsiena
    New Contributor

    thanks that was perfect.

     

    I recreated the asp.net core project and the asp client and have a good working model.

     

    thanks again for you help.

     

    rich