diff --git a/docs/router.rst b/docs/router.rst index ae0d524..69a8946 100644 --- a/docs/router.rst +++ b/docs/router.rst @@ -42,6 +42,7 @@ is an example using all configuration options. 'decorators': [admin_required], 'logic': create_note, 'response': 'note', + 'schema': 'special_note.yaml', 'title': 'Create Note', } } @@ -182,6 +183,10 @@ additional configuration: - `response` should be a key from the definitions section of the schema that identifies which definition should be used to validate the response returned by the logic function. +- `schema` is an alternative schema file that should be used for the endpoint. + This is useful for grouping related endpoints together that use different + schemas. This argument should be the name of the schema within the schema + directory. e.g. `'user.yaml'`. - `title` is a custom title to use as the header for the route/method. If a title is not provided, one will be generated based on the http method. The automatic title will be one of `Retrieve`, `Delete`, `Create`, or `Update`. @@ -200,6 +205,7 @@ additional configuration: 'decorators': [admin_required], 'logic': create_note, 'response': 'note', + 'schema': 'special_note.yaml', 'title': 'Create Note', } } diff --git a/doctor/router.py b/doctor/router.py index 19a6f67..016de6e 100644 --- a/doctor/router.py +++ b/doctor/router.py @@ -157,7 +157,13 @@ def create_routes(self, docs_group_title, schema_file, routes): params.extend(additional_args.get('required', [])) required.extend(additional_args.get('required', [])) - http_func = getattr(schema, 'http_' + method) + # Check if the schema was overridden for the method. + if opts.get('schema'): + method_schema = self.get_schema(opts['schema']) + else: + method_schema = schema + + http_func = getattr(method_schema, 'http_' + method) func = http_func( opts['logic'], params=params, required=required, response=opts.get('response'), title=opts.get('title'), diff --git a/test/test_router.py b/test/test_router.py index 3e75804..971e8ff 100644 --- a/test/test_router.py +++ b/test/test_router.py @@ -150,6 +150,12 @@ def logic_post(req1, opt1=None): 'get': { 'logic': logic_get, + # Specifying a different schema file that contains `someid` + # for the response. An example of overriding the schema + # at the http method level. This would fail without the + # schema key since someid is not defined in annotation.yaml + 'schema': 'common.yaml', + 'response': 'someid', }, 'post': { 'additional_args': {