Skip to content

Commit

Permalink
Adds ability to override schema for an endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Godfrey committed Feb 20, 2017
1 parent 3a6ed23 commit 1e0ad5c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/router.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
}
Expand Down Expand Up @@ -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`.
Expand All @@ -200,6 +205,7 @@ additional configuration:
'decorators': [admin_required],
'logic': create_note,
'response': 'note',
'schema': 'special_note.yaml',
'title': 'Create Note',
}
}
Expand Down
8 changes: 7 additions & 1 deletion doctor/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
6 changes: 6 additions & 0 deletions test/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down

0 comments on commit 1e0ad5c

Please sign in to comment.