https://github.com/timakro/apispec-oneofschema
Plugin for apispec providing support for Marshmallow-OneOfSchema schemas
https://github.com/timakro/apispec-oneofschema
apispec marshmallow openapi polymorphsim swagger
Last synced: 5 months ago
JSON representation
Plugin for apispec providing support for Marshmallow-OneOfSchema schemas
- Host: GitHub
- URL: https://github.com/timakro/apispec-oneofschema
- Owner: timakro
- License: mit
- Created: 2018-06-12T14:52:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-07-28T06:08:06.000Z (6 months ago)
- Last Synced: 2025-07-28T06:27:44.926Z (6 months ago)
- Topics: apispec, marshmallow, openapi, polymorphsim, swagger
- Language: Python
- Homepage: https://pypi.org/project/apispec-oneofschema/
- Size: 40 KB
- Stars: 11
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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
```python
from apispec import APISpec
from marshmallow import Schema, fields
from marshmallow_oneofschema import OneOfSchema
from apispec_oneofschema import MarshmallowPlugin
class TreeSchema(Schema):
leaves = fields.Int(required=True)
class FlowerSchema(Schema):
blooming = fields.Bool(required=True)
class PlantSchema(OneOfSchema):
type_schemas = {
'tree': TreeSchema,
'flower': FlowerSchema
}
# def get_obj_type(self, obj):
# ...
spec = APISpec(
title='Botany',
version='1.0.0',
openapi_version='3.0.0',
plugins=[
MarshmallowPlugin(),
]
)
spec.components.schema('Plant', schema=PlantSchema)
print(spec.to_yaml())
```
Resulting OpenAPI spec:
```yaml
components:
parameters: {}
responses: {}
schemas:
Flower:
properties:
blooming: {type: boolean}
required: [blooming]
type: object
Plant:
discriminator:
mapping: {flower: '#/components/schemas/Flower', tree: '#/components/schemas/Tree'}
propertyName: type
oneOf:
- {$ref: '#/components/schemas/Flower'}
- {$ref: '#/components/schemas/Tree'}
Tree:
properties:
leaves: {format: int32, type: integer}
required: [leaves]
type: object
securitySchemes: {}
info: {title: Botany, version: 1.0.0}
openapi: 3.0.0
paths: {}
tags: []
```
## Installation
pip install apispec-oneofschema