From bcfc8e6eeb6ccfc8d079afab43385c82275d4db3 Mon Sep 17 00:00:00 2001 From: jtc42 Date: Thu, 2 Jan 2020 23:52:46 +0000 Subject: [PATCH] Added update_spec function --- .../common/flask_labthings/decorators.py | 10 ++++------ openflexure_microscope/common/flask_labthings/spec.py | 5 +++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/openflexure_microscope/common/flask_labthings/decorators.py b/openflexure_microscope/common/flask_labthings/decorators.py index c17ef5c9..40e0eb3e 100644 --- a/openflexure_microscope/common/flask_labthings/decorators.py +++ b/openflexure_microscope/common/flask_labthings/decorators.py @@ -3,6 +3,7 @@ from functools import wraps, update_wrapper from flask import make_response from .utilities import rupdate +from .spec import update_spec def unpack(value): """Return a three tuple of data, code, and headers""" @@ -34,8 +35,7 @@ class marshal_with(object): def __call__(self, f): # Pass params to call function attribute for external access - f.__apispec__ = f.__dict__.get('__apispec__', {}) - f.__apispec__["_schema"] = self.schema + update_spec(f, {"_schema": self.schema}) # Wrapper function @wraps(f) def wrapper(*args, **kwargs): @@ -60,8 +60,7 @@ class use_args(object): def __call__(self, f): # Pass params to call function attribute for external access - f.__apispec__ = f.__dict__.get('__apispec__', {}) - f.__apispec__["_params"] = self.schema + update_spec(f, {"_params": self.schema}) # Wrapper function update_wrapper(self.wrapper, f) return self.wrapper(f) @@ -82,8 +81,7 @@ class doc(object): def __call__(self, f): # Pass params to call function attribute for external access - f.__apispec__ = f.__dict__.get('__apispec__', {}) - f.__apispec__.update(self.kwargs) + update_spec(f, self.kwargs) return f diff --git a/openflexure_microscope/common/flask_labthings/spec.py b/openflexure_microscope/common/flask_labthings/spec.py index fef8fd84..3bbf0633 100644 --- a/openflexure_microscope/common/flask_labthings/spec.py +++ b/openflexure_microscope/common/flask_labthings/spec.py @@ -11,6 +11,11 @@ from marshmallow import Schema as BaseSchema from collections import Mapping +def update_spec(obj, spec): + obj.__apispec__ = obj.__dict__.get('__apispec__', {}) + rupdate(obj.__apispec__, spec) + return obj.__apispec__ + def view2path(rule: str, view: Resource, spec: APISpec): params = { "path": rule, # TODO: Validate this slightly (leading / etc)