4

Creating a python flask rest plus server application, I'm trying to create a model for input body (in POST operation) with 'allOf' operator, which is equivalent to the following example, taken from swagger.yaml I've created with the swagger editor:

definitions:
  XXXOperation:
    description: something...
    properties:
      oper_type:
      type: string
        enum:
          - oper_a
          - oper_b
          - oper_c
      operation:
        allOf:
          - $ref: '#/definitions/OperA'
          - $ref: '#/definitions/OperB'
          - $ref: '#/definitions/OperC'

It should be something like (just in my crazy imagination):

xxx_oper_model = api.model('XXXOperation', {
    'oper_type': fields.String(required=True, enum['oper_a', 'oper_b', 'oper_c']),
    'operation': fields.Nested([OperA, OperB, OperC], type='anyof')
})

when OperA, OperB, OperC are also defined as models. How can I do that?

Actually, I prefer to use 'oneOf', but as I understand it's not supported even in the swagger editor, so I try to use the 'allOf' with not required fields.

Versions: flask restplus: 0.10.1, flask: 0.12.2, python: 3.6.2

Thanks a lot

Karthick Nagarajan
  • 1,327
  • 2
  • 15
  • 27
Amir
  • 391
  • 2
  • 3
  • 10

1 Answers1

-1

You need to use api.inherit. As mentioned in page 30 of documentation example;

parent = api.model('Parent', {
  'name': fields.String,
  'class': fields.String(discriminator=True)
})

child = api.inherit('Child', parent, {
  'extra': fields.String
})

this way, Child will have all properties of Parent + its own additional property extra

{
  "Child": {
    "allOf": [
      {
        "$ref": "#/definitions/Parent"
      },
      {
        "properties": {
          "extra": {
            "type": "string"
          }
        }
      }
    ]
  }
}
Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54