Skip to content

Support field level examples #452

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

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

Support field level examples #452

wants to merge 2 commits into from

Conversation

zak10
Copy link

@zak10 zak10 commented Sep 10, 2019

Add support for field-level examples from meta class …

This code allows us to add field-level examples. Usage:

class SomeSerializer(serializers.Serializer):
    some_date = serializers.DateField()

    class Meta:
        swagger_schema_examples = {
            'some_date': '2000-01-15'
        }

Zach Kauffman and others added 2 commits September 10, 2019 13:47
This code allows us to add field-level `example`s. Usage:
```python
class SomeSerializer(serializers.Serializer):
    some_date = serializers.DateField()

    class Meta:
        swagger_schema_examples = {
            'some_date': '2000-01-15'
        }
```
@mic159
Copy link

mic159 commented Apr 1, 2020

I think something like this would be amazing.

Currently to add an example, you have to manually specify the entire schema using swagger_schema_fields. And if you have nested objects... good luck.

class TitleSerializer(serializers.Serializer):
    title = serializers.CharField()
    slug = serializers.SlugField(required=False)

    class Meta:
        swagger_schema_fields = {
            "type": openapi.TYPE_OBJECT,
            "title": "Title",
            "properties": {
                "title": openapi.Schema(
                    title="Title of page",
                    type=openapi.TYPE_STRING,
                    example="Custom Example Data"
                ),
                "slug": openapi.Schema(
                    title="Slug of page",
                    type=openapi.TYPE_STRING,
                    example="custom-example-data"
                ),
            },
            "required": ["title"],
        }

Unless someone else knows how to add an example directly to a field?

@mic159
Copy link

mic159 commented May 3, 2020

Just found this: https://pypi.org/project/drf-yasg-examples/

It basically achieves this with an inspector that you can put into your default inspectors list.

class ExampleInspector(SerializerInspector):
    def process_result(self, result, method_name, obj, **kwargs):
        has_examples = hasattr(obj, "Meta") and hasattr(obj.Meta, "examples")
        if isinstance(result, openapi.Schema.OR_REF) and has_examples:
            schema = openapi.resolve_ref(result, self.components)
            if "properties" in schema:
                properties = schema["properties"]
                for name in properties.keys():
                    if name in obj.Meta.examples:
                        properties[name]["example"] = obj.Meta.examples[name]

        return result
SWAGGER_SETTINGS = {
      "DEFAULT_FIELD_INSPECTORS": [
            # The new inspector
            "xx.yy.ExampleInspector",
            # Defaults from drf-yasg (unmodified)
            "drf_yasg.inspectors.CamelCaseJSONFilter",
            "drf_yasg.inspectors.RecursiveFieldInspector",
            "drf_yasg.inspectors.ReferencingSerializerInspector",
            "drf_yasg.inspectors.ChoiceFieldInspector",
            "drf_yasg.inspectors.FileFieldInspector",
            "drf_yasg.inspectors.DictFieldInspector",
            "drf_yasg.inspectors.JSONFieldInspector",
            "drf_yasg.inspectors.HiddenFieldInspector",
            "drf_yasg.inspectors.RelatedFieldInspector",
            "drf_yasg.inspectors.SerializerMethodFieldInspector",
            "drf_yasg.inspectors.SimpleFieldInspector",
            "drf_yasg.inspectors.StringDefaultFieldInspector",
      ],
}

@JoelLefkowitz JoelLefkowitz changed the base branch from master to 1.21.x July 17, 2022 17:21
@JoelLefkowitz JoelLefkowitz added 1.21.x Release target in 1.21.x feature labels Jul 17, 2022
@JoelLefkowitz
Copy link
Collaborator

@zak10 thanks for the time put into this PR. Could you bring the branch up to date with the 1.21.x branch or enable me to push to it?

@JoelLefkowitz JoelLefkowitz added 1.22.x Release target in 1.22.x and removed 1.21.x Release target in 1.21.x labels Jul 17, 2022
@JoelLefkowitz JoelLefkowitz deleted the branch axnsan12:master October 17, 2024 11:55
@JoelLefkowitz JoelLefkowitz reopened this Oct 17, 2024
@JoelLefkowitz JoelLefkowitz changed the base branch from 1.21.x to master October 17, 2024 12:08
@JoelLefkowitz JoelLefkowitz added the enhancement Enhancement label Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.22.x Release target in 1.22.x enhancement Enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants