项目作者: timakro

项目描述 :
Plugin for apispec providing support for Marshmallow-OneOfSchema schemas
高级语言: Python
项目地址: git://github.com/timakro/apispec-oneofschema.git
创建时间: 2018-06-12T14:52:27Z
项目社区:https://github.com/timakro/apispec-oneofschema

开源协议:MIT License

下载


apispec-oneofschema

Plugin for apispec providing support for Marshmallow-OneOfSchema schemas

Can only be used with OpenAPI version 3.0.0 or greater which introduced a
name to definition mapping for the discriminator.

Example

  1. from apispec import APISpec
  2. from marshmallow import Schema, fields
  3. from marshmallow_oneofschema import OneOfSchema
  4. from apispec_oneofschema import MarshmallowPlugin
  5. class TreeSchema(Schema):
  6. leaves = fields.Int(required=True)
  7. class FlowerSchema(Schema):
  8. blooming = fields.Bool(required=True)
  9. class PlantSchema(OneOfSchema):
  10. type_schemas = {
  11. 'tree': TreeSchema,
  12. 'flower': FlowerSchema
  13. }
  14. # def get_obj_type(self, obj):
  15. # ...
  16. spec = APISpec(
  17. title='Botany',
  18. version='1.0.0',
  19. openapi_version='3.0.0',
  20. plugins=[
  21. MarshmallowPlugin(),
  22. ]
  23. )
  24. spec.components.schema('Plant', schema=PlantSchema)
  25. print(spec.to_yaml())

Resulting OpenAPI spec:

  1. components:
  2. parameters: {}
  3. responses: {}
  4. schemas:
  5. Flower:
  6. properties:
  7. blooming: {type: boolean}
  8. required: [blooming]
  9. type: object
  10. Plant:
  11. discriminator:
  12. mapping: {flower: '#/components/schemas/Flower', tree: '#/components/schemas/Tree'}
  13. propertyName: type
  14. oneOf:
  15. - {$ref: '#/components/schemas/Flower'}
  16. - {$ref: '#/components/schemas/Tree'}
  17. Tree:
  18. properties:
  19. leaves: {format: int32, type: integer}
  20. required: [leaves]
  21. type: object
  22. securitySchemes: {}
  23. info: {title: Botany, version: 1.0.0}
  24. openapi: 3.0.0
  25. paths: {}
  26. tags: []

Installation

  1. pip install apispec-oneofschema