1

I try to find how to define a model that can be represented by one of the following objects:

{
  "name": "John Doe",
  "additional_info": {
    "scheme": {
      "id": 1
    },
    "source": "direct"
  }
}
{
  "name": "John Doe",
  "additional_info": {
    "scheme": {
      "id": 1
    },
    "source": "direct",
    "optional_key": "something"
  }
}
{
  "name": "John Doe",
  "additional_info": {}
}

So I need to make sure that the additional_info object OR maybe empty, OR must contain at least two required keys scheme (AND the scheme object must contain id key) and source, AND may contain any optional keys.

I tried the following scheme:

    model:
      description: ''
      content:
        application/json:
          schema:
            allOf:
              - type: object
                properties:
                  name:
                    type: string
              - oneOf:
                  - type: object
                    properties:
                      additional_info:
                        type: object
                        properties:
                          scheme:
                            type: object
                            required:
                              - id
                            properties:
                              id:
                                type: integer
                          source:
                            type: string
                        required:
                          - scheme
                          - source
                  - type: object
                    properties:
                      additional_info:
                        type: object
                        additionalProperties: false
          examples:
            example-1:
              name: John Doe
              additional_info:
                scheme:
                  id: 1
                source: direct
            example-2:
              name: John Doe
              additional_info: {}
            example-3:
              name: John Doe
              additional_info:
                scheme:
                  id: 1
                source: direct
                optional_key: something

But I'm not sure if it is correct.

fallincode
  • 379
  • 4
  • 14
  • What you're asking for is similar to parameter dependencies but for models. [Here's](https://stackoverflow.com/questions/63209596/how-to-indicate-that-a-parameter-is-conditionally-required-when-another-paramete) a similar one for parameters. I would suggest doing custom validation of the data models – Debargha Roy Oct 18 '21 at 15:33
  • @DebarghaRoy, do you think this is not possible in OpenAPI 3.0? Or did you mean something else? – fallincode Oct 25 '21 at 06:32

1 Answers1

0

I tried different options and found the right one, it works. Perhaps it is redundant and in some places you do not need to use the minProperties and maxProperties keywords.

Here it is:

    model:
      description: ''
      content:
        application/json:
          schema:
            allOf:
              - type: object
                properties:
                  name:
                    type: string
                required:
                  - name
              - oneOf:
                  - type: object
                    properties:
                      additional_info:
                        type: object
                        minProperties: 2
                        properties:
                          scheme:
                            type: object
                            required:
                              - id
                            properties:
                              id:
                                type: integer
                          source:
                            type: string
                        required:
                          - scheme
                          - source
                  - type: object
                    properties:
                      additional_info:
                        type: object
                        additionalProperties: false
                        minProperties: 0
                        maxProperties: 0
          examples:
            example-1:
              name: John Doe
              additional_info:
                scheme:
                  id: 1
                source: direct
            example-2:
              name: John Doe
              additional_info: {}
            example-3:
              name: John Doe
              additional_info:
                scheme:
                  id: 1
                source: direct
                optional_key: something
fallincode
  • 379
  • 4
  • 14