question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using TypeDict with djangorestframework_camel_case sets required fields to optional

See original GitHub issue

Hi, when using TypeDict in combination with the drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields hook all fields that are converted from snake_case to camelCase are set to optional.

Serializer:

class RankData(TypedDict):
    value: int
    created_at: str
    name: str
    rank_by_value: bool


class RankSerializer(serializers.Serializer):
    rank_data = serializers.SerializerMethodField()

    def get_rank_data(self) -> RankData:
        return RankData(
            value=290,
            created_at="2022-01-01",
            name="Checkpoint A",
            rank_by_value=False,
        )

Settings:

SPECTACULAR_SETTINGS = {
    "POSTPROCESSING_HOOKS": [
        "drf_spectacular.hooks.postprocess_schema_enums",
        "drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields",
    ],
}

This produces the following schema: Screenshot 2022-11-16 at 08 54 12

-> All names that are converted from snake_case to camelCase are set to optional.

Removing "drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields" from the settings produces the correct schema (all fields are required): Screenshot 2022-11-16 at 08 56 05

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
saschametzcommented, Nov 17, 2022

Everything works perfectly now 👍

1reaction
tfranzelcommented, Nov 17, 2022

did a drive-by bugfix that came to my attention. this should work now as expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Camel case JSON support for Django REST framework. - GitHub
GitHub - vbabiy/djangorestframework-camel-case: Camel case JSON support for Django REST ... To use the second case, specify it in your django settings file....
Read more >
Validate Python TypedDict at runtime - Stack Overflow
The simplest solution I found works using pydantic. from typing import cast, TypedDict import pydantic class SomeDict(TypedDict): val: int ...
Read more >
PEP 655 – Marking individual TypedDict items as required or ...
The difficulty is that the best word for marking a potentially-missing key, Optional[] , is already used in Python for a completely different...
Read more >
T. Franzel - drf-spectacular
Sane and flexible OpenAPI 3 schema generation for Django REST framework. Documentation is an integral part of API development and OpenAPI 3 ...
Read more >
Making a field required for POST, but optional for PUTs?
The POST includes username and password, along with some optional fields (e.g. ... case, you just need to use PATCH instead of PUT,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found