From 262c9ea113b5cf5d1388e64968003710fd403d23 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 4 May 2016 14:24:01 +0100 Subject: [PATCH] Improve handling of $ref in swagger files It turns out that swagger merges $refed attributes with those defined inline, so do the same here. --- templating/matrix_templates/units.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/templating/matrix_templates/units.py b/templating/matrix_templates/units.py index c8716bdf..e046c82a 100644 --- a/templating/matrix_templates/units.py +++ b/templating/matrix_templates/units.py @@ -36,15 +36,19 @@ logger = logging.getLogger(__name__) def resolve_references(path, schema): if isinstance(schema, dict): - result = {} + # do $ref first + if '$ref' in schema: + value = schema['$ref'] + path = os.path.join(os.path.dirname(path), value) + with open(path) as f: + ref = yaml.load(f) + result = resolve_references(path, ref) + del schema['$ref'] + else: + result = {} + for key, value in schema.items(): - if key == "$ref": - path = os.path.join(os.path.dirname(path), value) - with open(path) as f: - schema = yaml.load(f) - return resolve_references(path, schema) - else: - result[key] = resolve_references(path, value) + result[key] = resolve_references(path, value) return result elif isinstance(schema, list): return [resolve_references(path, value) for value in schema] @@ -342,7 +346,7 @@ class MatrixUnits(Units): # have either "x-example" or a "schema" with an "example". params_missing_examples = [ p for p in single_api.get("parameters", []) if ( - "x-example" not in p and + "x-example" not in p and not Units.prop(p, "schema/example") ) ] @@ -600,7 +604,7 @@ class MatrixUnits(Units): # { # title: " key" # rows: [ - # { key: <key_name>, type: <string>, + # { key: <key_name>, type: <string>, # desc: <desc>, required: <bool> } # ] # }