cancel
Showing results for 
Search instead for 
Did you mean: 

How to express an array in a swagger definition

SOLVED
Highlighted
Occasional Contributor

How to express an array in a swagger definition

How can I express an array of objects in a defition.

Here's the sample json

{
	"resourceType": "Patient",
	"extension": [{
			"url": "http://hl7.org/fhir/StructureDefinition/us-core-race",
			"extension": [{
				"url": "ombCategory",
				"valueCoding": {
					"system": "http://hl7.org/fhir/v3/Race",
					"code": "2106-3",
					"display": "White"
				}
			}]
		},
		{
			"url": "http://hl7.org/fhir/StructureDefinition/us-core-ethnicity",
			"extension": [{
				"url": "ombCategory",
				"valueCoding": {
					"system": "http://hl7.org/fhir/v3/Ethnicity",
					"code": "2135-2",
					"display": "Hispanic or Latino"
				}
			}]
		}
	]
}
Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Moderator

Re: How to express an array in a swagger definition

extension:
  type: array
  items:
    oneOf:
      - $ref: '#/definitions/Race'
      - ...
      

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓

View solution in original post

5 REPLIES 5
Moderator

Re: How to express an array in a swagger definition

Please follow https://swagger.io/docs/specification/data-models/data-types/#array for information on how to define arrays.


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Occasional Contributor

Re: How to express an array in a swagger definition

I'm almost there.

extension:
        type: array
        items:
          type: object
          properties:
            race:
              $ref: '#/definitions/Race'
            ethnicity:
              $ref: '#/definitions/Ethnicity'
            sex:
              $ref: '#/definitions/Sex'

This includes an extraneous key before each definition

  "extension": [{
    "race": {
      "url": "http://hl7.org/fhir/StructureDefinition/us-core-race",
      "extension": {
        "url": "ombCategory",
        "valueCoding": {
          "system": "http://hl7.org/fhir/v3/Race",
          "code": "2106-3",
          "display": "White"
        }
      }
    },
    "ethnicity": {
      "url": "http://hl7.org/fhir/StructureDefinition/us-core-ethnicity",
      "extension": {
        "url": "ombCategory",
        "valueCoding": {
          "system": "http://hl7.org/fhir/v3/Ethnicity",
          "code": "2135-2",
          "display": "Hispanic or Latino"
        }
      }
    },
    "sex": {
      "url": "http://hl7.org/fhir/StructureDefinition/us-core-birthsex",
      "valueCode": "M"
    }
  }]

But I need it to look like this:

"extension": [
    {
      "url": "http://hl7.org/fhir/StructureDefinition/us-core-race",
      "extension": {
        "url": "ombCategory",
        "valueCoding": {
          "system": "http://hl7.org/fhir/v3/Race",
          "code": "2106-3",
          "display": "White"
        }
      }
    },
    {
      "url": "http://hl7.org/fhir/StructureDefinition/us-core-ethnicity",
      "extension": {
        "url": "ombCategory",
        "valueCoding": {
          "system": "http://hl7.org/fhir/v3/Ethnicity",
          "code": "2135-2",
          "display": "Hispanic or Latino"
        }
      }
    },
    {
      "url": "http://hl7.org/fhir/StructureDefinition/us-core-birthsex",
      "valueCode": "M"
    }
  ]

Thanks @RonRatovsky  for any additional help you might be able to offer.

 

Moderator

Re: How to express an array in a swagger definition

Instead of defining the array as an object containing the three properties, try defining the array value as 'oneOf' the three objects.


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Occasional Contributor

Re: How to express an array in a swagger definition

@RonRatovsky 

many thanks

could you kindly give me a snippet of what that would look like?

Moderator

Re: How to express an array in a swagger definition

extension:
  type: array
  items:
    oneOf:
      - $ref: '#/definitions/Race'
      - ...
      

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓

View solution in original post