-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve model name determination by using title information #249
Improve model name determination by using title information #249
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with the changes as the test shows it does what you want it to do, and that's a good thing. However, I'm suspicious of the collect_models function (see comment below).
bravado_core/model.py
Outdated
@@ -63,12 +72,11 @@ def collect_models(container, key, path, models, swagger_spec): | |||
:param models: created model types are placed here | |||
:type swagger_spec: :class:`bravado_core.spec.Spec` | |||
""" | |||
deref = swagger_spec.deref | |||
if key == MODEL_MARKER and is_object(swagger_spec, container): | |||
if (key == MODEL_MARKER or key == 'title') and is_object(swagger_spec, container): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you didn't write this code, it is quite confusing to me what key
really is. From looking at how tag_models
and collect_models
is called, it should be either the dict key with which the model instance is stored, or a list index. How can it ever be MODEL_MARKER or 'title'? And conceptually, I don't think it should be both - it should be either one or the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added key=='title'
in order to recognize inline models (see example below), but I agree that this increases understanding complexity.
I've also noticed that bravado-core relies on MODEL_MARKER
to determine if a model type can be used (check bravado_core.model.is_model
, so the added complexity will not produce the expected behavior during response unmarshaling.
In order to keep the code simpler to read I'm removing key=='title'
from the callback (so inline schemas are not tagged as models -> status-quo), adding docstrings to better expose the expected callback environment. I'll add an extra callback (called bless_models
that will take care of tagging inline models).
Additionally, we don't need to check for key=='title'
because tag_models
will tag models that have no x-model
defined.
0c05a34
to
041966e
Compare
7d666e0
to
461422a
Compare
I've added documentation for the callbacks methods and added a new callback method called
|
bravado_core/model.py
Outdated
:type schema_object_spec: dict | ||
:param object_spec: specification for a swagger object | ||
:type object_spec: dict | ||
:param ignore_config: ignore bravado-core 'default_type_to_object' configuration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not ignoring the config, you're behaving as if the setting were False. Maybe that would be a better way to name and describe this parameter. no_default_type
? disable_default_type
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll rename this in no_default_type
bravado_core/model.py
Outdated
|
||
if ( | ||
not is_dict_like(model_spec) or | ||
not is_object(swagger_spec, model_spec, ignore_config=True) or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is very much unclear as to why you'd need to ignore the config (i.e. not use a default object
type) in this one specific instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If default_type_to_object
is set to true then is_object(swagger_spec, model_spec)
returns True
for a big variety of objects (ie. #/definitions
will always be seen an object).
Adding the parameter allows us to not duplicate the check on type=="object" or "allOf" in model_spec
I'll add a comment to better document this
@@ -150,14 +151,14 @@ def discriminator_validator(swagger_spec, validator, discriminator_attribute, in | |||
message='\'{}\' is not a recognized schema'.format(discriminator_value) | |||
) | |||
|
|||
if discriminator_value == schema['x-model']: | |||
if discriminator_value == schema[MODEL_MARKER]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using 'x-model'
instead of the constant is a feature: Changing MODEL_MARKER should break all sorts of tests. Using these unchangeable constants in test code reduces the value of the test. I do not want to make sure that the code I test uses the constant. I want to make sure it uses 'x-model'
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, commented on the wrong piece of code, but you get the idea. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack ... I'll revert this change from tests
461422a
to
711deed
Compare
This CR improves the model name determination strategy used during model discovery.
The original approach was:
key
for models indefinition
x-model
for models around the specsThe goal of this PR tries to unify this in a common approach.
The model name is determined as:
x-model
title
#/definitions
use thekey
WARNING: this does CR not try to improve detection of duplicated models, but it should simplify future work on that direction