Django-rest-framework: KeyError: 'required' in openapi.py

Created on 19 Sep 2019  路  10Comments  路  Source: encode/django-rest-framework

Checklist

  • [yes] I have verified that that issue exists against the master branch of Django REST framework.
  • [yes] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [yes] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [yes] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [yes] I have reduced the issue to the simplest possible case.
  • [no] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

  • Create serializer that has no required fields
  • Try to generate openapi docs for it

Expected behavior

Docs are generated

Actual behavior

KeyError: 'required'

rest_framework.schemas.openapi.AutoSchema._get_request_body has code:

        # No required fields for PATCH
        if method == 'PATCH':
            del content['required']

But key required is generated by rest_framework.schemas.openapi.AutoSchema._map_serializer:

        if len(required) > 0:
            result['required'] = required

So if there is no required fields in serializer KeyError will be raised. I think _get_request_body should handle deletion in this way:

        # No required fields for PATCH
        if method == 'PATCH and 'required' in content:
            del content['required']
Schema Generation

Most helpful comment

i still have this issue on 3.10.3

you can fix this specifying the schema "DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema"

All 10 comments

Yep, looks vaild at first glance.

If you could put a test case together show how it comes up, that would be super. Very happy to accept a PR!

Thanks for the report! 馃憤

Looks like there are related PRs already: #6944 and #6920

Yep, looks vaild at first glance.

If you could put a test case together show how it comes up, that would be super. Very happy to accept a PR!

Thanks for the report! 馃憤

hey @carltongibson! my PR has the test case and solve the issue 馃槈

Am having this issue as well!

@helrond is this fixed in any of the releases?

i still have this issue on 3.10.3

i still have this issue on 3.10.3

you can fix this specifying the schema "DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema"

@guimunarolo thanks!

Is there a reason it's not fixed in the default autoschema generator?
And is there a reason for openapi.AutoSchema to exist? Is it different from coreapi.AutoSchema?

The problem is that I've already subclassed openapi.AutoSchema as the docs suggested and provided my implementations for some of the methods. I really don't understand why both of these classes exist and whichever should be used in what case.

I ended up specifying my custom AutoSchema based off openaapi.AutoSchema as a default one and overriding _get_request_body with the same code bit with a fix suggested by @FeroxTL

Okay, it got even more confusing as v3.10 says that coreapi is basically deprecated and we should use openapi that still has a bug. Is it scheduled to be fixed and released?

Was this page helpful?
0 / 5 - 0 ratings