From 00ada8371a167c2c0f012a8928a4b4d9c4e49497 Mon Sep 17 00:00:00 2001 From: theirix Date: Tue, 15 Aug 2017 03:10:12 +0300 Subject: [PATCH 1/2] Added use_instances parameter to Marshmallow plugin It allows to support partial Marshmallow schemas because partial fields can be specified only for Marshmallow instance. --- apispec/ext/marshmallow/__init__.py | 7 +++++-- apispec/ext/marshmallow/swagger.py | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/apispec/ext/marshmallow/__init__.py b/apispec/ext/marshmallow/__init__.py index c1be88c8..67e7637c 100644 --- a/apispec/ext/marshmallow/__init__.py +++ b/apispec/ext/marshmallow/__init__.py @@ -147,13 +147,16 @@ def resolve_parameters(spec, parameters): return resolved -def resolve_schema_dict(spec, schema, dump=True): +def resolve_schema_dict(spec, schema, dump=True, use_instances=False): if isinstance(schema, dict): if (schema.get('type') == 'array' and 'items' in schema): schema['items'] = resolve_schema_dict(spec, schema['items']) return schema plug = spec.plugins[NAME] if spec else {} - schema_cls = resolve_schema_cls(schema) + if isinstance(schema, marshmallow.Schema) and use_instances: + schema_cls = schema + else: + schema_cls = resolve_schema_cls(schema) if schema_cls in plug.get('refs', {}): ref_schema = {'$ref': '#/definitions/{0}'.format(plug['refs'][schema_cls])} if getattr(schema, 'many', False): diff --git a/apispec/ext/marshmallow/swagger.py b/apispec/ext/marshmallow/swagger.py index fd5016bf..7b302586 100644 --- a/apispec/ext/marshmallow/swagger.py +++ b/apispec/ext/marshmallow/swagger.py @@ -360,7 +360,8 @@ def schema2parameters(schema, **kwargs): def fields2parameters(fields, schema=None, spec=None, use_refs=True, - default_in='body', name='body', required=False): + default_in='body', name='body', required=False, + use_instances=False): """Return an array of OpenAPI parameters given a mapping between field names and :class:`Field ` objects. If `default_in` is "body", then return an array of a single parameter; else return an array of a parameter for each included field in @@ -372,7 +373,7 @@ def fields2parameters(fields, schema=None, spec=None, use_refs=True, if schema is not None: # Prevent circular import from apispec.ext.marshmallow import resolve_schema_dict - prop = resolve_schema_dict(spec, schema, dump=False) + prop = resolve_schema_dict(spec, schema, dump=False, use_instances=use_instances) else: prop = fields2jsonschema(fields, spec=spec, use_refs=use_refs, dump=False) From 24d194066b845696ffa57687fc6b82ee0e389ace Mon Sep 17 00:00:00 2001 From: theirix Date: Tue, 15 Aug 2017 15:22:26 +0300 Subject: [PATCH 2/2] Added theirix to AUTHORS.rst --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 1e4ff97d..c4029b87 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -37,3 +37,4 @@ Contributors (chronological) - Douglas Anderson `@djanderson `_ - Marat Sharafutdinov `@decaz `_ - Daniel Radetsky `@dradetsky `_ +- Evgeny Seliverstov `@theirix `_