Skip to content
This repository has been archived by the owner on Mar 18, 2019. It is now read-only.

fixing #37 issue support of definitions for nested objects #38

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

xxmatyuk
Copy link

  • added definitions
  • arrays handled properly

@xxmatyuk
Copy link
Author

@tomchristie I think it's ready to be merged.


return swagger


def _get_definitions(document):

definitions = dict()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it should be an OrderedDict

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true that, will update

}
else:
definition_data['properties'][f_name] = {
'type': _get_field_type(f_schema),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type can also be an array here. Now items is missing and the swagger-ui (version 2) breaks.

Copy link

@tuky tuky Aug 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suppose it needs something like

            elif _get_field_type(f_schema) == 'array':
                _item_name = '{}_item'.format(f_name)
                defs[_item_name] = get_field_def_data(f_schema.items, defs)
                definition_data['properties'][f_name] = {
                    'type': 'array',
                    'items': {'$ref': '#/definitions/{}'.format(_item_name)}
                }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of f_name confuses me here. There can be multiple fields with the same name (in different paths/objects) but different array member type in a schema, so this approach will definitely fail in such case.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. name clashes shoudl be a problem in all of this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let me figure that out. I thought it was useful and nobody even start it, but as soon I did it turns out everyone, but me knows how to do it better =)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xxmatyuk No offense =), I tried 2 other PRs for the same issue, and yours is still the best although still not quite ready.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuffnatty no problems, that was looong hard day) I'm willing to fix it, so stay tuned. And yeah, thanks )

Copy link
Author

@xxmatyuk xxmatyuk Jan 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuky @tuffnatty
guys, I'm making the changes as per you proposed to do, but could you please help me to find an example of the same name for definitions? So that I can write tests and perform changes real quick. Thanks.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say e.g. you have 4 serializers in 2 different files:

class AuthorSerializer(Serializer):
    name = CharField()

class Song(Serializer):
    author = AuthorSerializer()
    co_authors = AuthorSerializer(many=True)
class AuthorSerializer(Serializer):
    birthday = DateField()

class Song(Serializer):
    author = AuthorSerializer()

The 2 different AuthorSerializer should get the same names when generated into CoreAPI format. Then we have a name clash, when reusing them as '#/definitions/author' or '#/definitions/co_authors_item' in OpenAPI.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuky, thanks a lot! I wrote a test, reproduced the clash, working on fix.

}

if isinstance(field_item, coreschema.Object):
props = field_item.properties
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the recursion can also be an array of "simple" types, i would also suggest to add here:

        elif isinstance(field_item, (coreschema.String, coreschema.Boolean, coreschema.Integer, coreschema.Number)):
            return {'type': _get_field_type(field_item), 'description': _get_field_description(field_item)}

@joshowen
Copy link

joshowen commented Nov 8, 2017

hey @xxmatyuk is this still inflight? It would be immensely helpful for a problem I'm running into - please let me know if I can help out at all!

@xxmatyuk
Copy link
Author

xxmatyuk commented Nov 8, 2017

hey @joshowen! Now you gave me a strong reason to finish it =) I'll try to squeeze my duties and make it done, and for sure will let you know If any help's needed!

@joshowen
Copy link

Hey @xxmatyuk Please let us know if we can help out at all with this issue.

@xxmatyuk
Copy link
Author

xxmatyuk commented Jan 18, 2018

test is failing due to some python3.X difference I'm looking into now (filter object is a problem). Though, works like a charm on python2.x.

@xxmatyuk
Copy link
Author

xxmatyuk commented Jan 18, 2018

@tuffnatty @tuky @tovmeod now it's really ready for review, so feel free.

if _get_field_type(f_schema) is 'object':
def_data = _get_or_update_definitions(
_get_field_definition_data(f_schema, defs),
'{}_def_item'.format(f_schema.name),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to play with this.

It works for a super simple API using nested definitions, but when I tried to apply it to our whole site I'm getting a 500 and the traceback is giving an AttributeError here saying the name property doesn't exist. I'll try to look into it later, but just a heads up. (I cloned your branch to nexleaf/python-openapi-codec and may contribute that way)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm going to be switching to drf-yasg as it already supports nested definitions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for heads up. I'd rather close this one and switch to it either.

@kolanos
Copy link

kolanos commented Jan 10, 2019

@xxmatyuk Did you find a solution for the field name definition conflict?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants