swift - How to specify an array of a type in your OpenAPI schema but to allow for malformed elements to be ignored? - Stack Over

admin2025-04-18  6

I inherited a codebase using OpenAPI. Basically the most important part of our app is this events array. We have this one API call we make that gets this events array and without this events array there is nothing you can really do in the app.

responses:
    "200":
      description: Successful response
      content:
        application/json:
          schema:
            type: object
            required:
              - pagination
              - data
            properties:
              data:
                type: array
                items:
                  $ref: "#/components/schemas/Event"

And then elsewhere

   Event:
      type: object
      required:
        - id
        - eventNumber
        - eventStatus
        - stateCode
        - location
        ...
      properties:
        id:
          type: integer
        eventNumber:
          type: string
        eventStatus:
          type: integer
          nullable: true
        stateCode:
          type: string
        location:
          $ref: '#/components/schemas/LocationResponse'
        ...


LocationResponse:
  type: object
  properties:
    id:
      String
    active:
      type: boolean
    name:
      type: string
    state:
      $ref: '/components/schemas/StateOrProvince'
 StateOrProvince:
   type: string
   enum:
     - AL
     - AK
     .....

I am removing a lot of complexity because I do not think it matters but suffice it to say there are a lot of layers of sub objects.

The problem is twofold. Sometimes folks like to test the system and put in purposefully wonky data and also the nature of our app is that we get some of this data from sources we really can't control.

It just will be the case that there is an occasional Event that comes in with something wrong with it.

In our web app it handles this without issue. On the iOS app clearly the previous engineer tried to handle this but unfortunately when there is a malformed object the error is thrown before our code can help it to parse it. The autogenerated code will bail out on parsing the entire array if just one element does not fit the specification.

Is there any way to write an type: array object in your schema such that if some object in the array is not up to spec it is just ignored? Or bonus points turned into like a InvalidEvent(id: ) (since we do control the ID and this is helpful for debugging.

转载请注明原文地址:http://anycun.com/QandA/1744942720a89812.html