oxan / djangorestframework-dataclasses

Dataclasses serializer for Django REST framework
BSD 3-Clause "New" or "Revised" License
429 stars 28 forks source link

Swagger doc generation with composite nested fields #14

Closed errietta closed 4 years ago

errietta commented 4 years ago

If you have something like:

@dataclass
class Person():
    name: str

@dataclass
class Company():
    people: List[Person]

class PersonSerializer(DataclassSerializer):
    class Meta:
        dataclass = Person

class CompanySerializer(DataclassSerializer):
    class Meta:
        dataclass = Company

If I use get_schema_view to generate an openapi schema, the result for a view using CompanySerializer is this:

              properties:
                people:
                  type: array
                  items:
                    type: object # <---- no typing for the child elements!
                  writeOnly: true
              required:
              - people

However, if I use PeopleSerializer directly, it generates a schema for all fields as expected. I'm not sure if this is a problem with DRF dataclass serializer or somewhere else, because get_fields() on the serializer is definitely returning all fields as expected, and not sure how to debug this. Any ideas on why this is happening?

oxan commented 4 years ago

This seems to be a bug in DRF, fixed by encode/django-rest-framework@98c8af5291ac366b3030c4091284091ca63943ac. Can you check if you still see the same behaviour with the master version of DRF?

errietta commented 4 years ago

You're right @oxan that has been fixed in DRF master. I hope they will make a release soon, I'm not too big on using master 🙄 Thanks!