Skip to content
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

Normalizing an example name that starts with a special character. #1356

Open
Antoliny0919 opened this issue Dec 21, 2024 · 1 comment
Open

Comments

@Antoliny0919
Copy link

Describe the bug
Using the build_examples_list function, the name of each example is converted to camelcase. The converted value then becomes the key for that specific example.
To be precise, the conversion to camelCase is implemented through the inflection library, and since the second argument is not passed to the camelize function, it converts the first letter of each word, including the first one, to uppercase, which results in PascalCase.
However, when using a special character as the first character in the example name, unexpected results occurred.

To Reproduce

>>> from inflection import camelize
>>> camelize("-example_string", True)
>>> "-exampleString"

The first time I encountered this issue was when I override the descending order example for Django's ordering query string using extend_schema.

@extend_schema_view(
    list=extend_schema(
        parameters=[
            ...
            OpenApiParameter(
                name="ordering",
                ...
                examples=[
                    OpenApiExample(
                        "post_count",
                        description="Ascending order based on the number of posts.",
                        value="post_count",
                    ),
                    OpenApiExample(
                        "-post_count",
                        description="Descending order based on the number of posts.",
                        value="-post_count",
                    ),
                ],
            ),
        ],
        ...
    )
)
class CategoryViewSet(ListModelMixin, GenericViewSet):

In the case of post_count, it is converted to PostCount using the camelize function. However, -post_count is returned as -postCount instead of the expected -PostCount.
Actually, I'm not sure how the conversion to PascalCase should work when special characters are included in the string.

Expected behavior
As explained in the "To Reproduce" section, I expected that when using a string like "{special_character}hello_world", it would be converted to "{special_character}HelloWorld".

@Antoliny0919
Copy link
Author

There is one thing I'm curious about. Why drf_spectacular perform the following normalization on the example names? 🧐
normalized_name = inflection.camelize(example.name.replace(' ', '_'))
If it doesn't relate to the displayed part, I feel that the normalization process might not be necessary.

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

No branches or pull requests

1 participant