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

fix: Do not wrap an example list value in another list #1250

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

Conversation

psamuels00
Copy link

The purpose of this PR is to allow a list of values (more than one) to be presented as the value of an Open API Example. In all the examples I found online, only a single value is supplied, which value is automatically turned into a list. Also, there are no unit tests for handling a list of values.

Background

In 0.22.0, a change was made, reflected by the following entry in the release notes:

Examples are now wrapped in pagination/lists when endpoint/serializer is many=True

Based on the following decorator...

@extend_schema(
    examples = [
        OpenApiExample(
            "name",
            value={"pet": "dog"}
        ),
    ]
)

...the Swagger example renders like this:

[
  {
    "pet": "dog"
  }
]

So far, so good.

The Problem

If a list is explicitly supplied for the example value, it is still wrapped in a list. Given the following...

@extend_schema(
    examples = [
        OpenApiExample(
            "name",
            value=[
                {"pet": "dog"}
                {"pet": "cat"}
            ],
        ),
    ]
)

...the Swagger example renders like this:

[
  [
    {
      "pet": "dog"
    },
    {
      "pet": "cat"
    }
  ]
]

The Expectation

I would expect the rendered example to not be nested in a list, for example:

[
  {
    "pet": "dog"
  },
  {
    "pet": "cat"
  }
]

The Solution

With the proposed changes, if a list is supplied as the value of an example, it is not wrapped in a new list.

@psamuels00 psamuels00 changed the title Do not wrap an example list value in another list fix: Do not wrap an example list value in another list May 31, 2024
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

Successfully merging this pull request may close these issues.

None yet

1 participant