Skip to content

Commit

Permalink
Merge pull request #285 from macisamuele/maci-replace-inline-model-de…
Browse files Browse the repository at this point in the history
…finitions-with-references

Replace inline models with top-level definitions
  • Loading branch information
macisamuele committed Jul 2, 2018
2 parents d5be6e9 + ffd4dd1 commit eeef802
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
41 changes: 41 additions & 0 deletions bravado_core/spec_flattening.py
Expand Up @@ -2,6 +2,7 @@
import copy
import functools
import os.path
import re
import warnings
from collections import defaultdict

Expand Down Expand Up @@ -296,6 +297,45 @@ def _rename_references_descend(value):

return _rename_references_descend(flattened_spec_dict)

def replace_inline_models_with_refs(self, flattened_spec_dict):
"""
Rename definition references to more "human" names if possible.
The used approach is to use model-name as definition key, if this does not conflict
with an already existing key.
:param flattened_spec_dict: swagger spec dict (pre-flattened)
:return: swagger spec dict equivalent to flattened_spec_dict with more human references
:rtype: dict
"""
def _set_references_to_models_descend(value, json_ref):
if is_dict_like(value):
if (
MODEL_MARKER in value and
not re.match('^#/definitions/[^/]+$', json_ref) and
value == flattened_spec_dict.get('definitions', {}).get(value[MODEL_MARKER])
):
return {
'$ref': '#/definitions/{model_name}'.format(model_name=value[MODEL_MARKER])
}

else:
return {
key: _set_references_to_models_descend(value=subval, json_ref='{}/{}'.format(json_ref, key))
for key, subval in iteritems(value)
}

elif is_list_like(value):
return [
_set_references_to_models_descend(value=subval, json_ref='{}/{}'.format(json_ref, index))
for index, subval in enumerate(value)
]

else:
return value

return _set_references_to_models_descend(flattened_spec_dict, '#')

@cached_property
def resolved_specs(self):
# Create internal copy of spec_dict to avoid external dict pollution
Expand All @@ -318,6 +358,7 @@ def resolved_specs(self):
)

resolved_spec = self.rename_definition_references(resolved_spec)
resolved_spec = self.replace_inline_models_with_refs(resolved_spec)

return resolved_spec

Expand Down
Expand Up @@ -59,17 +59,7 @@
"$ref": "#/definitions/ErrorResponse"
},
{
"properties": {
"action": {
"description": "name of the forbidden action",
"type": "string"
}
},
"required": [
"action"
],
"type": "object",
"x-model": "endpoint_v1_HTTP_FORBIDDEN_action_part"
"$ref": "#/definitions/endpoint_v1_HTTP_FORBIDDEN_action_part"
}
],
"type": "object",
Expand Down Expand Up @@ -136,33 +126,13 @@
"lfile:endpoint..v1..responses.yaml|..200": {
"description": "HTTP/200",
"schema": {
"type": "object",
"x-model": "endpoint_v1_HTTP_OK"
"$ref": "#/definitions/endpoint_v1_HTTP_OK"
}
},
"lfile:endpoint..v1..responses.yaml|..403": {
"description": "HTTP/403",
"schema": {
"allOf": [
{
"$ref": "#/definitions/ErrorResponse"
},
{
"properties": {
"action": {
"description": "name of the forbidden action",
"type": "string"
}
},
"required": [
"action"
],
"type": "object",
"x-model": "endpoint_v1_HTTP_FORBIDDEN_action_part"
}
],
"type": "object",
"x-model": "endpoint_v1_HTTP_FORBIDDEN"
"$ref": "#/definitions/endpoint_v1_HTTP_FORBIDDEN"
}
},
"lfile:endpoint..v1..responses.yaml|..default": {
Expand Down
8 changes: 7 additions & 1 deletion test-data/2.0/multi-file-recursive/aux_2.json
@@ -1,6 +1,12 @@
{
"definitions": {
"not_referenced_models_in_not_direcly_linked_file": {
"not_referenced_models_in_not_directly_linked_file": {
"properties": {
"inline_model": {
"type": "object",
"x-model": "inline_model"
}
},
"type": "object"
},
"random_integer": {
Expand Down
@@ -1,5 +1,9 @@
{
"definitions": {
"inline_model": {
"type": "object",
"x-model": "inline_model"
},
"model_with_allOf_recursive": {
"allOf": [
{
Expand All @@ -19,9 +23,14 @@
],
"x-model": "model_with_allOf_recursive"
},
"not_referenced_models_in_not_direcly_linked_file": {
"not_referenced_models_in_not_directly_linked_file": {
"properties": {
"inline_model": {
"$ref": "#/definitions/inline_model"
}
},
"type": "object",
"x-model": "not_referenced_models_in_not_direcly_linked_file"
"x-model": "not_referenced_models_in_not_directly_linked_file"
},
"not_used": {
"type": "object",
Expand Down

0 comments on commit eeef802

Please sign in to comment.