How do I define array of objects without using anyOf in swagger?

462 views Asked by At

I've already done the coding and the API response is something like the following:

"response": {
    "meta": [ .. this is an array that contains arrays of objects
        [
            {}
        ]
    ]
    "javascript": [
        {
            "attribute": "type",
            "value": "applicaion/ld+json"
        },
        {
            "@context": "http://schema.org",
            "@graph": {
                "organization": {
                    "@type": "Organization",
                    "additionalType": "Organization",
                    "@id": "https://www.example.com/home",
                    "name": " Example name",
                    "sameAs": [
                        "https://twitter.com",
                        "https://www.facebook.com/",
                        "https://www.instagram.com/",
                        "https://www.linkedin.com/company/company/",
                        "https://en.wikipedia.org/wiki/_Group"
                    ],
                    "telephone": "083135",
                    "contactPoint": {
                    "@type": "ContactPoint",
                    "telephone": "083135",
                    "areaServed": {
                    "@type": "Country",
                    "name": "Example name"
                    }
                    },
                    "logo": {
                    "@type": "ImageObject",
                    "representativeOfPage": "True",
                    "url": "https://example.com/sites/default/files/_logo_4.svg"
                    }
                }
            }
        }
    ]
}

However, I need to write the YML for this endpoint, and I have done so using swagger openapi: 3.0.0.

With attention to the javascript key, the response looks something like this:

pageResponse:
  allOf:
    - required:
      - pageAlias
      properties:
        statusCode:
          type: string
          example: 200
        statusMessage:
          type: string
          example: OK
        supportMessage:
          type: string
          example: Content returned
        response:
          type: object
          properties:
            content:
              type: object
              allOf:
              - $ref: "#/components/schemas/mainContent"
            meta:
              type: array
              items: 
                allOf:
                - $ref: "#/components/schemas/metaAttribute"
            javascript:
              type: array
              items:
                anyOf:
                - $ref: "#/components/schemas/javaScriptAttribute"
                - $ref: "#/components/schemas/javaScriptSchema"

Question: Is there a way I can define the two objects inside the javascript key without using anyOf? The reason for this is because both objects must always exist. It is not optional.

I hope that the question is well phrased.

1

There are 1 answers

0
Jeremy Fiel On

Your use of allOf is not necessary for most of your schemas. allOf is mostly used when combining multiple schemas into a larger schema or to work around the limitation of OpenAPI 3.x.x allowing siblings to a $ref where you may want a unique description or title for a property.

e.g.

type: object
properties:
  some-prop:
    allOf:
      - title: this is a title
      - $ref: #/components/schemas/myschema

regarding your javascript array. if you put the references into an array of items, it will act like a tuple and validate the array instance to have these two schemas as index 0 and 1 for each instance of the javascript array.

regarding your comment about meta.. array of array of objects, I updated your schema to handle that.

FYI, this schema will always fail because you have a required: pageAlias but it's not apart of this schema.

pageResponse:
  type: object
  required:
    - pageAlias
  properties:
    statusCode:
      type: string
      example: 200
    statusMessage:
      type: string
      example: OK
    supportMessage:
      type: string
      example: Content returned
    response:
      type: object
      properties:
        content:
          $ref: "#/components/schemas/mainContent"
        meta:
          type: array
          items:
            type: array
            items:
              - $ref: "#/components/schemas/metaAttribute"
        javascript:
          type: array
          items:
            - $ref: "#/components/schemas/javaScriptAttribute"
            - $ref: "#/components/schemas/javaScriptSchema"