-
-
Notifications
You must be signed in to change notification settings - Fork 179
Add appropriate nullable flag depending on OpenAPI version #197
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,23 @@ def field2length(field): | |
return attributes | ||
|
||
|
||
def field2nullable(field, spec): | ||
"""Returns the dictionary with an appropriate nullable key depending on | ||
the OpenAPI version. Returns an empty dict if the field is not nullable | ||
|
||
:param Field field: A marshmallow field. | ||
:param spec: apispec.core.APISpec instance | ||
:rtype: dict | ||
""" | ||
if not field.allow_none: | ||
return {} | ||
|
||
if spec and spec.openapi_version.version[0] == 3: | ||
return {'nullable': True} | ||
|
||
return {'x-nullable': True} | ||
|
||
|
||
# Properties that may be defined in a field's metadata that will be added to the output | ||
# of field2property | ||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject | ||
|
@@ -293,8 +310,7 @@ def field2property(field, spec=None, use_refs=True, dump=True, name=None): | |
if field.dump_only: | ||
ret['readOnly'] = True | ||
|
||
if field.allow_none: | ||
ret['x-nullable'] = True | ||
ret.update(field2nullable(field, spec)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit bothered by the fact that the Same concern for Maybe we should make We should give this a thought and maybe refactor a bit. Check what Anyway, this issue is much wider than this specific PR, so I suppose we can merge this and open a separate issue for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making spec mandatory everywhere is probably the right first step. Practically the entry points to all of these functions ( I think there is definitely some opportunity for refactoring. We have quite a bit of complexity around multiple ways of handling nested schemas (using refs, inlining, waiting to see if a ref gets added later, immediately resolving a name). Simplifying that would probably help in other areas. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, totally. I'm not sure the whole spec needs to be carried about, but I think the version should be everywhere, as it may affect anything, so it should be provided to all helpers. Anyway, we can merge this PR and focus on the refactor later on. |
||
|
||
ret.update(field2range(field)) | ||
ret.update(field2length(field)) | ||
|
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.
spec.openapi_version.version[0]
This is accessed in two places, and more to come. Maybe we should add a shortcut for it.
For instance properties
spec.major_version
,spec.minor_version
.Nothing blocking, just a thought.