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: rendering of openapi parameter examples #2509

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion litestar/_openapi/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from litestar._openapi.schema_generation import SchemaCreator
from litestar.di import Provide
from litestar.handlers.base import BaseRouteHandler
from litestar.openapi.spec import Reference
from litestar.openapi.spec import Example, Reference
from litestar.types.internal_types import PathParameterDefinition


Expand Down Expand Up @@ -109,12 +109,17 @@ def create_parameter(

schema = result if isinstance(result, Schema) else schema_creator.schemas[result.value]

examples: dict[str, Example | Reference] = {}
for i, example in enumerate(kwarg_definition.examples or [] if kwarg_definition else []):
examples[f"{field_definition.name}-example-{i}"] = example

return Parameter(
description=schema.description,
name=parameter_name,
param_in=param_in,
required=is_required,
schema=result,
examples=examples or None,
)


Expand Down
21 changes: 20 additions & 1 deletion tests/unit/test_openapi/test_parameters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING, List, Optional, Type, cast

import pytest
from typing_extensions import Annotated

from litestar import Controller, Litestar, Router, get
from litestar._openapi.parameters import create_parameter_for_handler
Expand All @@ -11,9 +12,11 @@
from litestar.di import Provide
from litestar.enums import ParamType
from litestar.exceptions import ImproperlyConfiguredException
from litestar.openapi.spec import OpenAPI
from litestar.openapi import OpenAPIConfig
from litestar.openapi.spec import Example, OpenAPI
from litestar.openapi.spec.enums import OpenAPIType
from litestar.params import Dependency, Parameter
from litestar.testing import create_test_client
from litestar.utils import find_index

if TYPE_CHECKING:
Expand Down Expand Up @@ -306,3 +309,19 @@ def my_handler(
assert local.schema.type == OpenAPIType.INTEGER # type: ignore
assert local.required
assert local.schema.examples # type: ignore


def test_parameter_examples() -> None:
@get(path="/")
async def index(
text: Annotated[str, Parameter(examples=[Example(value="example value", summary="example summary")])]
) -> str:
return text

with create_test_client(
route_handlers=[index], openapi_config=OpenAPIConfig(title="Test API", version="1.0.0")
) as client:
response = client.get("/schema/openapi.json")
assert response.json()["paths"]["/"]["get"]["parameters"][0]["examples"] == {
"text-example-0": {"summary": "example summary", "value": "example value"}
}